From 87720fe4ec57e7912ca8d58989952a99c8751bf7 Mon Sep 17 00:00:00 2001 From: TH3CHARLie Date: Tue, 5 Nov 2019 01:51:05 +0800 Subject: [PATCH] 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. --- mypy/semanal.py | 6 ++++++ test-data/unit/check-typeddict.test | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 379f15648c89..1975f2704c70 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -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: diff --git a/test-data/unit/check-typeddict.test b/test-data/unit/check-typeddict.test index 267eca43ea7a..a11564080819 100644 --- a/test-data/unit/check-typeddict.test +++ b/test-data/unit/check-typeddict.test @@ -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]