Skip to content

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

Merged
merged 2 commits into from
Dec 4, 2018
Merged

Improve documentation of ABCs #6004

merged 2 commits into from
Dec 4, 2018

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Dec 4, 2018

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.
@JukkaL JukkaL requested a review from gvanrossum December 4, 2018 16:27
Copy link
Member

@gvanrossum gvanrossum left a 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
...
Copy link
Member

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).

``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:
Copy link
Member

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.)


class Cat(Animal):
def eat(self, food: str) -> None:
... # Implementation omitted
Copy link
Member

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
Copy link
Member

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).

@JukkaL JukkaL merged commit 2b1bb6e into master Dec 4, 2018
JukkaL added a commit that referenced this pull request Dec 4, 2018
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.
@JukkaL JukkaL mentioned this pull request Dec 4, 2018
@gvanrossum gvanrossum deleted the doc-abstract branch December 5, 2018 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants