-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Adding @abstractmethod __init__ to ABC breaks staticmethods #1706
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
Comments
In fact mypy thinks the expression class WidgetWithInit(object):
@abstractmethod
def __init__(self, foo, bar):
# type: (int, int) -> None
pass |
In fact init_method = info.get_method('__init__')
if not init_method:
# Must be an invalid class definition.
return AnyType()
else: ... and |
Here's a possible test case [case testAbstractInit]
from abc import abstractmethod, ABCMeta
class A(metaclass=ABCMeta):
@abstractmethod
def __init__(self, a: int) -> None:
pass
class B(A):
pass
class C(B):
def __init__(self, a: int) -> None:
self.c = a
a = A(1) # E: Cannot instantiate abstract class 'A' with abstract attribute '__init__'
A.c # E: "A" has no attribute "c"
b = B(2) # E: Cannot instantiate abstract class 'B' with abstract attribute '__init__'
B.c # E: "B" has no attribute "c"
c = C(3)
c.c
C.c But I gave up on trying to fix this because the logic around recognizing decorators is too hairy. |
I think this bug is deep and important. I've encountered something similar in #3227. The logic that handles decorators seems to be flawed in a way which is difficult to fix. |
True, decorators are one of mypy's weak spots. We're slowly chipping away at it (e.g. #3918) but it's not easy because of special-casing in various places (both in Python and in mypy). |
I was trying to create an ABC that also worked as a factory.
MyPy will respond:
So adding the
def __init__
seems to make the staticmethod and classmethod have a type of Any.Fortunately I can work around it by not defining init in the ABC, and having my
def create
enforce the signature.The text was updated successfully, but these errors were encountered: