Skip to content

Reject TypedDict with invalid name argument #3614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2017
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
4 changes: 4 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,10 @@ def check_typeddict(self, node: Expression, var_name: str = None) -> Optional[Ty
info = self.build_typeddict_typeinfo('TypedDict', [], [], set())
else:
name = cast(StrExpr, call.args[0]).value
if var_name is not None and name != var_name:
self.fail(
"First argument '{}' to TypedDict() does not match variable name '{}'".format(
name, var_name), node)
if name != var_name or self.is_func_scope():
# Give it a unique name derived from the line number.
name += '@' + str(call.line)
Expand Down
5 changes: 5 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,11 @@ from mypy_extensions import TypedDict
Point = TypedDict('Point', {'x': 1, 'y': 1}) # E: Invalid field type
[builtins fixtures/dict.pyi]

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


-- Special cases

Expand Down