Skip to content
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
25 changes: 13 additions & 12 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2112,18 +2112,20 @@ def analyze_typeddict_classdef(self, defn: ClassDef) -> bool:
and defn.info.typeddict_type
and not has_placeholder(defn.info.typeddict_type)
):
# This is a valid TypedDict, and it is fully analyzed.
return True
is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
# Don't reprocess everything
is_typeddict = True
info = defn.info
else:
is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
if is_typeddict:
for decorator in defn.decorators:
decorator.accept(self)
if info is not None:
self.analyze_class_decorator_common(defn, info, decorator)
if info is None:
self.mark_incomplete(defn.name, defn)
else:
self.prepare_class_def(defn, info, custom_names=True)
for decorator in defn.decorators:
decorator.accept(self)
if defn.info:
self.analyze_class_decorator_common(defn, decorator)
return True
return False

Expand Down Expand Up @@ -2155,7 +2157,7 @@ def analyze_namedtuple_classdef(
with self.scope.class_scope(defn.info):
for deco in defn.decorators:
deco.accept(self)
self.analyze_class_decorator_common(defn, defn.info, deco)
self.analyze_class_decorator_common(defn, deco)
with self.named_tuple_analyzer.save_namedtuple_body(info):
self.analyze_class_body_common(defn)
return True
Expand Down Expand Up @@ -2237,7 +2239,7 @@ def leave_class(self) -> None:

def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
decorator.accept(self)
self.analyze_class_decorator_common(defn, defn.info, decorator)
self.analyze_class_decorator_common(defn, decorator)
if isinstance(decorator, RefExpr):
if decorator.fullname in RUNTIME_PROTOCOL_DECOS:
if defn.info.is_protocol:
Expand All @@ -2249,13 +2251,12 @@ def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None
):
defn.info.dataclass_transform_spec = self.parse_dataclass_transform_spec(decorator)

def analyze_class_decorator_common(
self, defn: ClassDef, info: TypeInfo, decorator: Expression
) -> None:
def analyze_class_decorator_common(self, defn: ClassDef, decorator: Expression) -> None:
"""Common method for applying class decorators.

Called on regular classes, typeddicts, and namedtuples.
"""
info = defn.info
if refers_to_fullname(decorator, FINAL_DECORATOR_NAMES):
info.is_final = True
elif refers_to_fullname(decorator, DISJOINT_BASE_DECORATOR_NAMES):
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,20 @@ reveal_type(d) # N: Revealed type is "TypedDict('__main__.D', {})"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictDecoratorUndefinedNames]
from typing import TypedDict

@abc # E: Name "abc" is not defined
@efg.hij # E: Name "efg" is not defined
@klm[nop] # E: Name "klm" is not defined \
# E: Name "nop" is not defined
@qrs.tuv[wxy] # E: Name "qrs" is not defined \
# E: Name "wxy" is not defined
class A(TypedDict):
x: int
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictWithClassmethodAlternativeConstructorDoesNotCrash]
# https://github.com/python/mypy/issues/5653
from typing import TypedDict
Expand Down
Loading