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

__new__ and subclassing is wrongly interpreted by mypy #13440

Closed
socketpair opened this issue Aug 17, 2022 · 3 comments
Closed

__new__ and subclassing is wrongly interpreted by mypy #13440

socketpair opened this issue Aug 17, 2022 · 3 comments
Labels

Comments

@socketpair
Copy link

class Base:
    def __new__(cls):
        return 42

    def get(self) -> str:
        return 'hello'

class Child(Base):
    pass


print(Child())
print(Child().get())

This code obviously will fail at runtime:

42
Traceback (most recent call last):
  File "/tmp/qwe2.py", line 13, in <module>
    print(Child().get())
AttributeError: 'int' object has no attribute 'get'

But MyPy sees nothing wrong.

@socketpair socketpair added the bug mypy got something wrong label Aug 17, 2022
@AlexWaygood AlexWaygood added feature and removed bug mypy got something wrong labels Aug 17, 2022
@hauntsaninja
Copy link
Collaborator

That's because mypy sees def __new__(cls): as def __new__(cls) -> Any:. If you annotate it as def __new__(cls) -> int:, you'll get an error

@socketpair
Copy link
Author

@hauntsaninja why mypy does not infer return type of __new__ ? I think it should

@hauntsaninja
Copy link
Collaborator

mypy does not infer unannotated types in general. I agree that it would be useful if mypy was capable of that, see e.g. #4409

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

3 participants