Skip to content

Commit afb1284

Browse files
authored
Fix another "sloppily cast to StrExpr" mypyc bug with TypedDicts (python#7369)
I fix this one with a refactoring to extract the name in the function that does the original check on what type the first argument is.
1 parent e9cf4f2 commit afb1284

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

mypy/semanal_typeddict.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Semantic analysis of TypedDict definitions."""
22

33
from collections import OrderedDict
4-
from typing import Optional, List, Set, Tuple, cast
4+
from typing import Optional, List, Set, Tuple
55
from typing_extensions import Final
66

77
from mypy.types import Type, AnyType, TypeOfAny, TypedDictType, TPDICT_NAMES
@@ -191,12 +191,11 @@ def check_typeddict(self,
191191
# This is a valid typed dict, but some type is not ready.
192192
# The caller should defer this until next iteration.
193193
return True, None
194-
items, types, total, ok = res
194+
name, items, types, total, ok = res
195195
if not ok:
196196
# Error. Construct dummy return value.
197197
info = self.build_typeddict_typeinfo('TypedDict', [], [], set())
198198
else:
199-
name = cast(StrExpr, call.args[0]).value
200199
if var_name is not None and name != var_name:
201200
self.fail(
202201
"First argument '{}' to TypedDict() does not match variable name '{}'".format(
@@ -216,7 +215,7 @@ def check_typeddict(self,
216215
call.analyzed.set_line(call.line, call.column)
217216
return True, info
218217

219-
def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[List[str], List[Type],
218+
def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[str, List[str], List[Type],
220219
bool, bool]]:
221220
"""Parse typed dict call expression.
222221
@@ -262,7 +261,7 @@ def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[List[str], List
262261
if has_any_from_unimported_type(t):
263262
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, dictexpr)
264263
assert total is not None
265-
return items, types, total, ok
264+
return args[0].value, items, types, total, ok
266265

267266
def parse_typeddict_fields_with_types(
268267
self,
@@ -293,9 +292,9 @@ def parse_typeddict_fields_with_types(
293292
return items, types, True
294293

295294
def fail_typeddict_arg(self, message: str,
296-
context: Context) -> Tuple[List[str], List[Type], bool, bool]:
295+
context: Context) -> Tuple[str, List[str], List[Type], bool, bool]:
297296
self.fail(message, context)
298-
return [], [], True, False
297+
return '', [], [], True, False
299298

300299
def build_typeddict_typeinfo(self, name: str, items: List[str],
301300
types: List[Type],

test-data/unit/check-typeddict.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,12 @@ p: TaggedPoint
670670
p['z'] # E: TypedDict "TaggedPoint" has no key 'z'
671671
[builtins fixtures/dict.pyi]
672672

673+
[case testTypedDictWithUnicodeName]
674+
# flags: --python-version 2.7
675+
from mypy_extensions import TypedDict
676+
TaggedPoint = TypedDict(u'TaggedPoint', {'type': str, 'x': int, 'y': int})
677+
[builtins fixtures/dict.pyi]
678+
673679
[case testCannotGetItemOfAnonymousTypedDictWithInvalidStringLiteralKey]
674680
from typing import TypeVar
675681
from mypy_extensions import TypedDict

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy