8000 Generate an error if trying to use @final with TypedDict (#7865) · python/mypy@87720fe · GitHub
[go: up one dir, main page]

Skip to content

Commit 87720fe

Browse files
TH3CHARLieilevkivskyi
authored andcommitted
Generate an error if trying to use @Final with TypedDict (#7865)
Fixes #7849 This fix modifies function `analyze_class` in `mypy/semanal.py`, if the analyzer identifies a `TypedDict`, it then traverses all decorators and checks if `@final` exists, and if so, generates an error.
1 parent a4f4ffe commit 87720fe

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

mypy/semanal.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,12 @@ def analyze_class(self, defn: ClassDef) -> None:
10671067

10681068
is_typeddict, info = self.typed_dict_analyzer.analyze_typeddict_classdef(defn)
10691069
if is_typeddict:
1070+
for decorator in defn.decorators:
1071+
decorator.accept(self)
1072+
if isinstance(decorator, RefExpr):
1073+
if decorator.fullname in ('typing.final',
1074+
'typing_extensions.final'):
1075+
self.fail("@final cannot be used with TypedDict", decorator)
10701076
if info is None:
10711077
self.mark_incomplete(defn.name, defn)
10721078
else:

test-data/unit/check-typeddict.test

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1924,3 +1924,16 @@ v = {bad2: 2} # E: Extra key 'bad' for TypedDict "Value"
19241924

19251925
[builtins fixtures/dict.pyi]
19261926
[typing fixtures/typing-full.pyi]
1927+
1928+
[case testCannotUseFinalDecoratorWithTypedDict]
1929+
from typing import TypedDict
1930+
from typing_extensions import final
1931+
1932+
@final # E: @final cannot be used with TypedDict
1933+
class DummyTypedDict(TypedDict):
1934+
int_val: int
1935+
float_val: float
1936+
str_val: str
1937+
1938+
[builtins fixtures/dict.pyi]
1939+
[typing fixtures/typing-full.pyi]

0 commit comments

Comments
 (0)
0