Executing the body of an abstractmethod
#1908
decorator-factory
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When using
abstractmethod
, the body of an abstract method is allowed to be executed through asuper()
call. (or by simply callingDuck.quack(some_duck)
here)Leaving the body as just
pass
is very common (see flink, standard library), even if the return type of the method doesn't allowNone
:A mixin class like this will not work safely with
Duck
, because if someone eventually callsDuck.quack
, they're going to get a surprisingNone
value:Mypy does complain about the
LoudDuck
class:Of course, this safeguard will not work if the type checker only considers
pyi
files for the library, so this is fine with mypy:pyright
does not report an error with eitherDuck
,LoudDuck
orIncrement
.My question is: leaving the body of a method that is not allowed to return
None
aspass
is typically not allowed by type checkers. Abstract method bodies can be executed, so why are they exempt from this rule?Beta Was this translation helpful? Give feedback.
All reactions