Skip to content

Args to __new__ not recognized #982

Closed

Description

@gvanrossum

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

Activity

  1. JukkaL commented on Nov 22, 2015

    @JukkaL
    Collaborator

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

  2. JukkaL commented on Nov 22, 2015

    @JukkaL
    Collaborator

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

  3. JukkaL commented on Nov 22, 2015

    @JukkaL
    Collaborator

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

  4. added a commit that references this issue on Nov 29, 2015
  5. JukkaL commented on Nov 29, 2015

    @JukkaL
    Collaborator

    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__
  6. JukkaL commented on Nov 29, 2015

    @JukkaL
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions