diff --git a/mypy/typeops.py b/mypy/typeops.py index f9c1914cc9a8..ad7ca1f18f1b 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -197,9 +197,9 @@ def class_callable( # by accident. Like `Hashable` is a subtype of `object`. See #11799 and isinstance(default_ret_type, Instance) and not default_ret_type.type.is_protocol - # Only use the declared return type from __new__ or declared self in __init__ + # Only use the declared return type from declared self in __init__ # if it is actually returning a subtype of what we would return otherwise. - and is_subtype(explicit_type, default_ret_type, ignore_type_params=True) + and (is_new or is_subtype(explicit_type, default_ret_type, ignore_type_params=True)) ): ret_type: Type = explicit_type else: diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 4bc1e50f7be9..8babe401afb3 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -6883,7 +6883,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 "__main__.A" +reveal_type(A()) # N: Revealed type is "builtins.int" [case testNewReturnType4] from typing import TypeVar, Type @@ -6960,7 +6960,7 @@ class A: class B(A): pass -reveal_type(B()) # N: Revealed type is "__main__.B" +reveal_type(B()) # N: Revealed type is "__main__.A" [case testNewReturnType10] # https://github.com/python/mypy/issues/11398 diff --git a/test-data/unit/check-enum.test b/test-data/unit/check-enum.test index 6779ae266454..307b26cba483 100644 --- a/test-data/unit/check-enum.test +++ b/test-data/unit/check-enum.test @@ -1360,7 +1360,7 @@ class Bar(Foo): A = 1 B = 2 -a = Bar.A +a = Bar.A # E: "Type[Foo]" has no attribute "A" reveal_type(a.value) # N: Revealed type is "Any" reveal_type(a._value_) # N: Revealed type is "Any" [builtins fixtures/primitives.pyi]