Skip to content
Draft
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
10 changes: 10 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
function_type,
get_all_type_vars,
get_type_vars,
has_deferred_constructor,
is_literal_type_like,
make_simplified_union,
simple_literal_type,
Expand Down Expand Up @@ -400,6 +401,15 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
result = node.type
elif isinstance(node, (FuncDef, TypeInfo, TypeAlias, MypyFile, TypeVarLikeExpr)):
result = self.analyze_static_reference(node, e, e.is_alias_rvalue or lvalue)
if (
isinstance(node, TypeInfo)
and isinstance(result, ProperType)
and isinstance(result, AnyType)
and has_deferred_constructor(node)
):
# When __init__ or __new__ is wrapped in a custom decorator, we need to defer.
# analyze_static_reference guarantees that it never defers, so play along.
self.chk.handle_cannot_determine_type(node.name, e)
else:
if isinstance(node, PlaceholderNode):
assert False, f"PlaceholderNode {node.fullname!r} leaked to checker"
Expand Down
15 changes: 15 additions & 0 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,21 @@ def type_object_type(info: TypeInfo, named_type: Callable[[str], Instance]) -> P
return result


def has_deferred_constructor(info: TypeInfo) -> bool:
init_method = info.get("__init__")
new_method = info.get("__new__") or init_method
return (
init_method is not None
and _is_deferred_decorator(init_method.node)
or new_method is not None
and _is_deferred_decorator(new_method.node)
)


def _is_deferred_decorator(n: SymbolNode | None) -> bool:
return isinstance(n, Decorator) and n.type is None and not n.var.is_ready


def is_valid_constructor(n: SymbolNode | None) -> bool:
"""Does this node represents a valid constructor method?

Expand Down
31 changes: 31 additions & 0 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -9289,3 +9289,34 @@ from typ import NT
def f() -> NT:
return NT(x='')
[builtins fixtures/tuple.pyi]

[case testDecoratedConstructorDeferral]
from typing import Any, Callable, TypeVar

Tc = TypeVar('Tc', bound=Callable[..., Any])

def any_decorator_factory() -> Callable[[Tc], Tc]:
def inner(func: Tc) -> Tc:
return func
return inner


def function_pre() -> None:
reveal_type(GoodClass()) # N: Revealed type is "__main__.GoodClass"
reveal_type(BadClass()) # N: Revealed type is "Any"


class GoodClass:
@any_decorator_factory()
def __init__(self):
pass

class BadClass:
@unknown() # E: Name "unknown" is not defined
def __init__(self):
pass


def function_post() -> None:
reveal_type(GoodClass()) # N: Revealed type is "__main__.GoodClass"
reveal_type(BadClass()) # N: Revealed type is "Any"
Loading