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

Only return a declared type from __new__ if it is a subtype of the class #7656

Merged
merged 1 commit into from Oct 9, 2019

Conversation

msullivan
Copy link
Collaborator

Fixes #7597.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

I think this kind of implicit logic should be documented somewhere in mypy docs, otherwise LGTM.

@@ -6031,7 +6031,7 @@ class A:
def __new__(cls) -> int: # E: Incompatible return type for "__new__" (returns "int", but must return a subtype of "A")
pass

reveal_type(A()) # N: Revealed type is 'builtins.int'
reveal_type(A()) # N: Revealed type is '__main__.A'
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep the declared return type for the class itself. The original PR closed an issue called "Honor return type of __new__", so this change would be a step back.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We report an error at the __new__ declaration site, and there are a bunch of other situations where we wouldn't honor it before (like if it is a union or something other than an instance or tuple), so I think this actually makes thing a little more consistent.

@msullivan msullivan merged commit c95ecc1 into master Oct 9, 2019
@msullivan msullivan deleted the __new__child branch October 9, 2019 20:18
@actionless
Copy link

thanks! it worked however i found another problem with popen inheritance, i am posting an issue right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected error when inheriting a class from Popen and overriding some method in it
3 participants