8000 Generate an error if trying to use @final with TypedDict by TH3CHARLie · Pull Request #7865 · python/mypy · GitHub
[go: up one dir, main page]

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
6 changes: 6 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,12 @@ def analyze_class(self, defn: ClassDef) -> None:

is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
if is_typeddict:
for decorator in defn.decorators:
decorator.accept(self)
if isinstance(decorator, RefExpr):
if decorator.fullname in ('typing.final',
'typing_extensions.final'):
self.fail("@final cannot be used with TypedDict", decorator)
if info is None:
self.mark_incomplete(defn.name, defn)
else:
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -1924,3 +1924,16 @@ v = {bad2: 2} # E: Extra key 'bad' for TypedDict "Value"

[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testCannotUseFinalDecoratorWithTypedDict]
from typing import TypedDict
from typing_extensions import final

@final # E: @final cannot be used with TypedDict
class DummyTypedDict(TypedDict):
int_val: int
float_val: float
str_val: str

[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]
0