Skip to content

Commit 2285e91

Browse files
committed
Fix constructor type for subclasses of Any
1 parent 5081c59 commit 2285e91

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

mypy/typeops.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def tuple_fallback(typ: TupleType) -> Instance:
125125
)
126126

127127

128-
def get_self_type(func: CallableType, default_self: Instance | TupleType) -> Type | None:
128+
def get_self_type(func: CallableType, def_info: TypeInfo) -> Type | None:
129+
default_self = fill_typevars(def_info)
129130
if isinstance(get_proper_type(func.ret_type), UninhabitedType):
130131
return func.ret_type
131132
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(
227228
# self-types only in the defining class, similar to __new__ (but not exactly the same,
228229
# see comment in class_callable below). This is mostly useful for annotating library
229230
# classes such as subprocess.Popen.
230-
default_self = fill_typevars(info)
231231
if not is_new and not info.is_newtype:
232-
orig_self_types = [get_self_type(it, default_self) for it in signature.items]
232+
orig_self_types = [get_self_type(it, def_info) for it in signature.items]
233233
else:
234234
orig_self_types = [None] * len(signature.items)
235235

@@ -245,7 +245,7 @@ def type_object_type_from_function(
245245
# We need to map B's __init__ to the type (List[T]) -> None.
246246
signature = bind_self(
247247
signature,
248-
original_type=default_self,
248+
original_type=fill_typevars(info),
249249
is_classmethod=is_new,
250250
# Explicit instance self annotations have special handling in class_callable(),
251251
# we don't need to bind any type variables in them if they are generic.

test-data/unit/check-classes.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8779,3 +8779,10 @@ class C:
87798779
C().foo = "no" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
87808780
C().bar = "fine"
87818781
[builtins fixtures/property.pyi]
8782+
8783+
[case testCorrectConstructorTypeWithAnyFallback]
8784+
class B(Unknown): # type: ignore
8785+
def __init__(self) -> None: ...
8786+
class C(B): ...
8787+
8788+
reveal_type(C) # N: Revealed type is "def () -> __main__.C"

0 commit comments

Comments
 (0)