Skip to content

[mypyc] Document some of our inheritance conventions #19370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions mypyc/ir/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
Opcodes operate on abstract values (Value) in a register machine. Each
value has a type (RType). A value can hold various things, such as:
- local variables (Register)
- local variables or temporaries (Register)
- intermediate values of expressions (RegisterOp subclasses)
- condition flags (true/false)
- literals (integer literals, True, False, etc.)
NOTE: As a convention, we don't create subclasses of concrete Value/Op
subclasses (e.g. you shouldn't define a subclass of Integer, which
is a concrete class).
If you want to introduce a variant of an existing class, you'd
typically add an attribute (e.g. a flag) to an existing concrete
class to enable the new behavior. Sometimes adding a new abstract
base class is also an option, or just creating a new subclass
without any inheritance relationship (some duplication of code
is preferred over introducing complex implementation inheritance).
This makes it possible to use isinstance(x, <concrete Value
subclass>) checks without worrying about potential subclasses.
Comment on lines +22 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be a good idea to mark the concrete classes as @final to document/enforce this?

On a somewhat related noted, can/does mypyc optimize isinstance checks with final types?

"""

from __future__ import annotations
Expand Down Expand Up @@ -257,7 +271,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:


class BaseAssign(Op):
"""Base class for ops that assign to a register."""
"""Abstract base class for ops that assign to a register."""

def __init__(self, dest: Register, line: int = -1) -> None:
super().__init__(line)
Expand Down Expand Up @@ -320,7 +334,7 @@ def accept(self, visitor: OpVisitor[T]) -> T:


class ControlOp(Op):
"""Control flow operation."""
"""Abstract base class for control flow operations."""

def targets(self) -> Sequence[BasicBlock]:
"""Get all basic block targets of the control operation."""
Expand Down
15 changes: 15 additions & 0 deletions mypyc/ir/rtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@

mypyc.irbuild.mapper.Mapper.type_to_rtype converts mypy Types to mypyc
RTypes.

NOTE: As a convention, we don't create subclasses of concrete RType
subclasses (e.g. you shouldn't define a subclass of RTuple, which
is a concrete class). We prefer a flat class hierarchy.

If you want to introduce a variant of an existing class, you'd
typically add an attribute (e.g. a flag) to an existing concrete
class to enable the new behavior. In rare cases, adding a new
abstract base class could also be an option. Adding a completely
separate class and sharing some functionality using module-level
helper functions may also be reasonable.

This makes it possible to use isinstance(x, <concrete RType
subclass>) checks without worrying about potential subclasses
and avoids most trouble caused by implementation inheritance.
"""

from __future__ import annotations
Expand Down