8000 Fix type object signature when both __new__ and __init__ present by msullivan · Pull Request #5642 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Fix type object signature when both __new__ and __init__ present #5642

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

Merged
merged 7 commits into from
Sep 20, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
tweak the test
  • Loading branch information
msullivan committed Sep 19, 2018
commit 162e7e7fdf3090898086d49b21690769c7cbc5e9
5 changes: 2 additions & 3 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -5124,11 +5124,10 @@ class A:
pass

class B(A):
def __new__(cls) -> 'B':
...
def __new__(cls) -> int:
return 10

B()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at runtime in CPython this is an error:

$ Traceback (most recent call last):
  File "_.py", line 9, in <module>
    b = B()
TypeError: __init__() missing 1 required positional argument: 'x'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, hm:
The original bug example had the __new__ call return an int, which works because __init__ isn't called if it isn't a subclass.

reveal_type(B) # E: Revealed type is 'def () -> __main__.B'

[case testNewAndInit2]
from typing import Any
Expand Down
0