diff --git a/mypy/typeops.py b/mypy/typeops.py index e8087a1713ff..1558661cba42 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -125,7 +125,8 @@ def tuple_fallback(typ: TupleType) -> Instance: ) -def get_self_type(func: CallableType, default_self: Instance | TupleType) -> Type | None: +def get_self_type(func: CallableType, def_info: TypeInfo) -> Type | None: + default_self = fill_typevars(def_info) if isinstance(get_proper_type(func.ret_type), UninhabitedType): return func.ret_type elif func.arg_types and func.arg_types[0] != default_self and func.arg_kinds[0] == ARG_POS: @@ -227,9 +228,8 @@ def type_object_type_from_function( # self-types only in the defining class, similar to __new__ (but not exactly the same, # see comment in class_callable below). This is mostly useful for annotating library # classes such as subprocess.Popen. - default_self = fill_typevars(info) if not is_new and not info.is_newtype: - orig_self_types = [get_self_type(it, default_self) for it in signature.items] + orig_self_types = [get_self_type(it, def_info) for it in signature.items] else: orig_self_types = [None] * len(signature.items) @@ -245,7 +245,7 @@ def type_object_type_from_function( # We need to map B's __init__ to the type (List[T]) -> None. signature = bind_self( signature, - original_type=default_self, + original_type=fill_typevars(info), is_classmethod=is_new, # Explicit instance self annotations have special handling in class_callable(), # we don't need to bind any type variables in them if they are generic. diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index f4bbaf41dc47..c0102f1c3e2c 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -8779,3 +8779,21 @@ class C: C().foo = "no" # E: Incompatible types in assignment (expression has type "str", variable has type "int") C().bar = "fine" [builtins fixtures/property.pyi] + +[case testCorrectConstructorTypeWithAnyFallback] +from typing import Generic, TypeVar + +class B(Unknown): # type: ignore + def __init__(self) -> None: ... +class C(B): ... + +reveal_type(C) # N: Revealed type is "def () -> __main__.C" + +T = TypeVar("T") +class BG(Generic[T], Unknown): # type: ignore + def __init__(self) -> None: ... +class CGI(BG[int]): ... +class CGT(BG[T]): ... + +reveal_type(CGI) # N: Revealed type is "def () -> __main__.CGI" +reveal_type(CGT) # N: Revealed type is "def [T] () -> __main__.CGT[T`1]"