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

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

Closed
li-dan opened this issue Nov 1, 2017 · 4 comments · Fixed by #5646
Closed

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

li-dan opened this issue Nov 1, 2017 · 4 comments · Fixed by #5646
Assignees
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high

Comments

@li-dan
Copy link
Contributor

li-dan commented Nov 1, 2017

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.

@ilevkivskyi
Copy link
Member

I think #1021 is closely related.

@ilevkivskyi ilevkivskyi added bug mypy got something wrong priority-0-high false-positive mypy gave an error on correct code labels May 18, 2018
@msullivan
Copy link
Collaborator

Interestingly, the following works:


class A:
    def __new__(cls, x: int) -> 'A':
        cls2: Type[object] = cls
        return object.__new__(cls2)

@msullivan msullivan self-assigned this Sep 19, 2018
@msullivan
Copy link
Collaborator

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

msullivan added a commit that referenced this issue Sep 19, 2018
Currently the first argument to `__new__` and classmethods is a
callable type that is constructed during semantic analysis by
typechecker code (!) that looks for the `__init__`/`__new__` methods.

This causes a number of problems, including not being able to call
`object.__new__` in a subclass's `__new__` if it took arguments
(#4190) and giving the wrong type if `__init__` appeared after the
class method (#1727).

Taking a `Type` instead lets us solve those problems, and postpone
computing the callable version of the type until typechecking if it is
needed.

This also lets us drop a bunch of plugin code that tries to fix up the
types of its cls arguments post-hoc, sometimes incorrectly (#5263).

Fixes #1727.
Fixes #4190.
Fixes #5263.
@gvanrossum
Copy link
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?)

msullivan added a commit that referenced this issue Sep 21, 2018
Currently the first argument to `__new__` and classmethods is a
callable type that is constructed during semantic analysis by
typechecker code (!) that looks for the `__init__`/`__new__` methods.

This causes a number of problems, including not being able to call
`object.__new__` in a subclass's `__new__` if it took arguments
(#4190) and giving the wrong type if `__init__` appeared after the
class method (#1727).

Taking a `Type` instead lets us solve those problems, and postpone
computing the callable version of the type until typechecking if it is
needed.

This also lets us drop a bunch of plugin code that tries to fix up the
types of its cls arguments post-hoc, sometimes incorrectly (#5263).

Fixes #1727.
Fixes #4190.
Fixes #5263.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-0-high
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants