Skip to content

Commit 1ad9aef

Browse files
JukkaLgvanrossum
authored andcommitted
Reject TypedDict with invalid name argument (#3614)
1 parent 545a8e5 commit 1ad9aef

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

mypy/semanal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,10 @@ def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[Ty
23432343
info = self.build_typeddict_typeinfo('TypedDict', [], [], set())
23442344
else:
23452345
name = cast(StrExpr, call.args[0]).value
2346+
if var_name is not None and name != var_name:
2347+
self.fail(
2348+
"First argument '{}' to TypedDict() does not match variable name '{}'".format(
2349+
name, var_name), node)
23462350
if name != var_name or self.is_func_scope():
23472351
# Give it a unique name derived from the line number.
23482352
name += '@' + str(call.line)

test-data/unit/check-typeddict.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,11 @@ from mypy_extensions import TypedDict
10691069
Point = TypedDict('Point', {'x': 1, 'y': 1}) # E: Invalid field type
10701070
[builtins fixtures/dict.pyi]
10711071

1072+
[case testCannotCreateTypedDictTypeWithInvalidName]
1073+
from mypy_extensions import TypedDict
1074+
X = TypedDict('Y', {'x': int}) # E: First argument 'Y' to TypedDict() does not match variable name 'X'
1075+
[builtins fixtures/dict.pyi]
1076+
10721077

10731078
-- Special cases
10741079

0 commit comments

Comments
 (0)