-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
super().__init__(....) in mixins fails with Too many arguments for "__init__" of "object"
#5887
Comments
Faced with the same issue. |
Mypy shouldn't complain about this idiom, but I'm not sure what exact rules should be. Maybe calls to |
There are lots of problems with mixins. Maybe we should have a way to flag them? |
There is a proposal python/typing#246 (partially based on python/typing#241) |
Hmm... Maybe it’s time to work on both of these. |
It would be great to do it this year, I am not sure however we will have time to do this soon. Since it may be a relatively time-consuming change (taking into account necessary design discussions). |
It would be great if I could somehow specify, with what classes the mixin is going to be mixed up. Then, mypy might be able to judge calls to parent methods (including |
On |
Going from mypy==0.910 to mypy=0.930 this seems to be fixed. Got bunch of type:ignore commments not needed. So I would say this issue is fixed 😄 |
Mypy has been fixed :). See python/mypy#5887
Yes, I can confirm this appears to be fixed in the latest release. |
I'm still getting class ExtraValuemixin:
def __init__(self, value, *args, **kwargs):
super().__init__(value, *args, **kwargs)
def retrieve_extra_value(self):
return self._extra_value
class ParentObj:
def __init__(self, value):
self.value = value
class ChildObj(ExtraValuemixin, ParentObj):
pass
obj = ChildObj(value=5)
print(obj.retrieve_extra_value()) My usecase is adding mixins to classes extending class CustomExceptionMixin:
message = ""
def __init__(
self, message: str, *args, **kwargs
) -> None:
self.message = message
super().__init__(message, **kwargs) # type: ignore |
When I create a mixin class that extends the logic of
__init__
, the regular thing to do is:However this looks wrong to mypy, as it says:
I get it, there's no
*args
or**kwargs
in theobject
's constructor signature; but this is a mixin, and it relies on its childen's constructors. Ho do I make mypy understand this?Full example:
The text was updated successfully, but these errors were encountered: