Skip to content

False positive when calling object.__new__ inside __new__ with args #4190

Closed

Description

@li-dan

Consider:

class A:
    def __new__(cls, x: int) -> 'A':
        return object.__new__(cls) # Argument 1 to "__new__" of "object" has incompatible type "Type[A]"; expected "Type[object]"

class B:
    def __new__(cls) -> 'B':
        return object.__new__(cls)

The object.__new__(cls) call type checks when inside a __new__ method with a single argument, but not when there are extra arguments.

The code for A is taken from testSuperWithNew, but because object.__new__ is unannotated in fixtures/__new__.pyi, the type error doesn't show up in the unit test. The discrepancy between the fixture and typeshed was noted in #3111.

Activity

  1. ilevkivskyi commented on Nov 1, 2017

    @ilevkivskyi
    Member

    I think #1021 is closely related.

  2. msullivan commented on Sep 19, 2018

    @msullivan
    Collaborator

    Interestingly, the following works:

    
    class A:
        def __new__(cls, x: int) -> 'A':
            cls2: Type[object] = cls
            return object.__new__(cls2)
    
  3. self-assigned this
    on Sep 19, 2018
  4. msullivan commented on Sep 19, 2018

    @msullivan
    Collaborator

    I think the fix for this is to make the type of cls be Type[A].

  5. gvanrossum commented on Sep 19, 2018

    @gvanrossum
    Member

    I think the fix for this is to make the type of cls be Type[A].

    (By which you mean, mypy should set it to that, right?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions