You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class C(object):
def __new__(cls, foo=None):
obj = object.__new__(cls)
obj.foo = foo
return obj
x = C(foo=12)
print(x.foo)
y = C(12)
This elicits an error complaining about the foo keyword to C, another about the foo attribute, and then one about the positional argument:
mypy_new.py:7: error: Unexpected keyword argument "foo" for "C"
mypy_new.py:8: error: "C" has no attribute "foo"
mypy_new.py:9: error: Too many arguments for "C"
This may be related to #794 -- feel free to close as a dupe if fixing that would automatically fix this. (There seem to be two separate issues here -- it doesn't turn the __new__ signature into the class signature, and it doesn't believe attributes set by __new__ become attributes of the object.)
#794 is a third issue -- classes should be able to override __new__ with a different signature.
Attribute assignment can be worked around by defining attributes in the class body. Inferring attributes in __new__ is hard to do in general, but we can probably support at least the most common idioms.
Closing this as the immediate issue is resolved, but see also the follow-up issues linked to above and #794 -- they are needed for more complete __new__ support.
Activity
JukkaL commentedon Nov 22, 2015
#794 is a third issue -- classes should be able to override
__new__with a different signature.Attribute assignment can be worked around by defining attributes in the class body. Inferring attributes in
__new__is hard to do in general, but we can probably support at least the most common idioms.JukkaL commentedon Nov 22, 2015
Marking as priority as this is a fundamental language feature that mypy doesn't support yet.
JukkaL commentedon Nov 22, 2015
#794 actually covers an additional issue -- using
super()with__new__.Merge branch 'call-type-with-__new__'
JukkaL commentedon Nov 29, 2015
Inferring signature from
__new__now works in the most common cases. I'm going to create separate issues for remaining limitations:__new____new__JukkaL commentedon Nov 29, 2015
Closing this as the immediate issue is resolved, but see also the follow-up issues linked to above and #794 -- they are needed for more complete
__new__support.