Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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 1 commit into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 12 additions & 3 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
variables.extend(info.defn.type_vars)
variables.extend(init_type.variables)

from mypy.subtypes import is_subtype

init_ret_type = get_proper_type(init_type.ret_type)
if is_new and isinstance(init_ret_type, (Instance, TupleType)):
ret_type = init_type.ret_type # type: Type
default_ret_type = fill_typevars(info)
if (
is_new
and isinstance(init_ret_type, (Instance, TupleType))
# Only use the return type from __new__ if it is actually returning
# a subtype of what we would return otherwise.
and is_subtype(init_ret_type, default_ret_type, ignore_type_params=True)
):
ret_type = init_ret_type # type: Type
else:
ret_type = fill_typevars(info)
ret_type = default_ret_type

callable_type = init_type.copy_modified(
ret_type=ret_type, fallback=type_type, name=None, variables=variables,
Expand Down
12 changes: 11 additions & 1 deletion test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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.


[case testNewReturnType4]
from typing import TypeVar, Type
Expand Down Expand Up @@ -6099,6 +6099,16 @@ class X:
def __new__(cls, x: TX) -> TX: # E: "__new__" must return a class instance (got "TX")
pass

[case testNewReturnType9]
class A:
def __new__(cls) -> A:
pass

class B(A):
pass

reveal_type(B()) # N: Revealed type is '__main__.B'

[case testGenericOverride]
from typing import Generic, TypeVar, Any

Expand Down