Skip to content

Fix constructor type for subclasses of Any #19295

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -8779,3 +8779,10 @@ 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]
class B(Unknown): # type: ignore
def __init__(self) -> None: ...
class C(B): ...

reveal_type(C) # N: Revealed type is "def () -> __main__.C"