Skip to content

Commit 7916bf1

Browse files
committed
Try alternative implementation of exceptions
1 parent e356348 commit 7916bf1

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

mypy/typeops.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,13 @@ def class_callable(
329329
variables.extend(info.defn.type_vars)
330330
variables.extend(init_type.variables)
331331

332-
from mypy.subtypes import is_subtype
332+
from mypy.subtypes import is_equivalent, is_subtype
333333

334334
init_ret_type = get_proper_type(init_type.ret_type)
335335
orig_self_type = get_proper_type(orig_self_type)
336336
default_ret_type = fill_typevars(info)
337+
# Default return type in the class where constructor method was defined.
338+
default_def_ret_type = fill_typevars(def_info) if def_info is not None else default_ret_type
337339
explicit_type = init_ret_type if is_new else orig_self_type
338340
if (
339341
is_new
@@ -345,9 +347,9 @@ def class_callable(
345347
# class D(C): ...
346348
# So we need to ignore the explicit annotation when creating constructor type for D.
347349
and (
348-
def_info is info
349-
and not isinstance(explicit_type, AnyType)
350-
or not is_subtype(default_ret_type, explicit_type, ignore_type_params=True)
350+
isinstance(explicit_type, AnyType)
351+
and explicit_type.type_of_any != TypeOfAny.unannotated
352+
or not is_equivalent(default_def_ret_type, explicit_type, ignore_type_params=True)
351353
)
352354
):
353355
ret_type = explicit_type

test-data/unit/check-classes.test

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7317,7 +7317,7 @@ reveal_type(B()) # N: Revealed type is "__main__.B"
73177317
[case testNewReturnType2]
73187318
from typing import Any
73197319

7320-
# make sure that __new__ method that return Any are ignored when
7320+
# make sure that __new__ method that return implicit Any are ignored when
73217321
# determining the return type
73227322
class A:
73237323
def __new__(cls):
@@ -7328,7 +7328,7 @@ class B:
73287328
pass
73297329

73307330
reveal_type(A()) # N: Revealed type is "__main__.A"
7331-
reveal_type(B()) # N: Revealed type is "__main__.B"
7331+
reveal_type(B()) # N: Revealed type is "Any"
73327332

73337333
[case testNewReturnType3]
73347334

@@ -7536,9 +7536,11 @@ class C(B): ...
75367536

75377537
# Always respect explicit return type after giving an error.
75387538
reveal_type(B()) # N: Revealed type is "__main__.A"
7539+
reveal_type(C()) # N: Revealed type is "__main__.A"
75397540

75407541
# Ignore "implicit" return type to preserve backwards compatibility.
7541-
reveal_type(C()) # N: Revealed type is "__main__.C"
7542+
class D(A): ...
7543+
reveal_type(D()) # N: Revealed type is "__main__.D"
75427544

75437545
[case testNewReturnType19]
75447546
from typing import TypeVar

0 commit comments

Comments
 (0)