-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Improve documentation of ABCs #6004
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
Conversation
It's a common error to forget to implement an abstract method, and mypy doesn't immediately generate an error. Explain this explicitly in the documentation, since this behavior can be confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits. Feel free to merge after fixing those without waiting for further approval. (I'm going to be in an out due to a dental appt.)
def f(self, x: int, y: int) -> None: # Error: too many arguments | ||
... | ||
|
||
class D(A): | ||
class Derived3(Base): | ||
def f(self, x: int) -> None: # OK | ||
... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also add an example that shows you can change the signature contravariantly, e.g.
class D4(Base):
def f(self, x: float) -> None: # OK because mypy treats int as a subtype of float
class D5(Base):
def f(self, x: int, *args: Any) -> None: # OK because it accepts more than the base class method
Also, the note about covariantly overriding the return type below ought to get some examples (and object
vs. int
feels a bit abstract).
docs/source/class_basics.rst
Outdated
``abc.abstractproperty`` function decorators. Example: | ||
by any *concrete* (non-abstract) subclass. You can define abstract base | ||
classes using the ``abc.ABCMeta`` metaclass, and the ``abc.abstractmethod`` | ||
and ``abc.abstractproperty`` function decorators. Example: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually @abstractproperty
has been deprecated since 3.3 -- just put@property
above @abstractmethod
: https://docs.python.org/3/library/abc.html?highlight=abstractproperty#abc.abstractproperty . (However, in 2.7 you still need @abstractproperty
.)
docs/source/class_basics.rst
Outdated
|
||
class Cat(Animal): | ||
def eat(self, food: str) -> None: | ||
... # Implementation omitted |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is confusing, since the point of this example is about whether a method is implemented in a subclass or not. If you think a comment is needed, you could use "Body omitted".
As shown above, the class definition will not generate an error | ||
in this case, but any attempt to construct an instance will be | ||
flagged as an error. | ||
|
||
A class can inherit any number of classes, both abstract and | ||
concrete. As with normal overrides, a dynamically typed method can |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point (not now) I would like to overhaul the docs and somehow change the terminology regarding statically typed and dynamically typed functions and methods. There are flags (e.g. --check-untyped-defs
) that blur the difference, and when we talk amongst ourselves or with users we never describe the distinction -- instead we'll use "unannotated" (even though my spell checker doesn't think that's a word :-). It's also not clear to me whether an unannotated method can still override an annotated method if the signatures differ wildly (e.g. no correspondence in argument count).
It's a common error to forget to implement an abstract method, and mypy doesn't immediately generate an error. Explain this explicitly in the documentation, since this behavior can be confusing.
It's a common error to forget to implement an abstract
method, and mypy doesn't immediately generate an error.
Explain this explicitly in the documentation, since this
behavior can be confusing.