Skip to content
Merged
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
18 changes: 18 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about also testing a generic class that subclasses Any?


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]"