Skip to content
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

Args to __new__ not recognized #982

Closed
gvanrossum opened this issue Nov 22, 2015 · 5 comments
Closed

Args to __new__ not recognized #982

gvanrossum opened this issue Nov 22, 2015 · 5 comments
Labels

Comments

@gvanrossum
Copy link
Member

I have this code:

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

@JukkaL JukkaL added the feature label Nov 22, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented 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
Copy link
Collaborator

JukkaL commented Nov 22, 2015

Marking as priority as this is a fundamental language feature that mypy doesn't support yet.

@JukkaL
Copy link
Collaborator

JukkaL commented Nov 22, 2015

#794 actually covers an additional issue -- using super() with __new__.

JukkaL added a commit that referenced this issue Nov 29, 2015
@JukkaL
Copy link
Collaborator

JukkaL commented Nov 29, 2015

Inferring signature from __new__ now works in the most common cases. I'm going to create separate issues for remaining limitations:

  • Inferring attributes from __new__
  • Honoring return type of __new__

@JukkaL
Copy link
Collaborator

JukkaL commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants