Skip to content

Commit

Permalink
Generate an error if trying to use @Final with TypedDict (#7865)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TH3CHARLie authored and ilevkivskyi committed Nov 4, 2019
1 parent a4f4ffe commit 87720fe
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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 comments on commit 87720fe

Please sign in to comment.