Skip to content

Fix another "sloppily cast to StrExpr" mypyc bug with TypedDicts #7369

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 2 commits into from
Aug 19, 2019
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
13 changes: 6 additions & 7 deletions mypy/semanal_typeddict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Semantic analysis of TypedDict definitions."""

from collections import OrderedDict
from typing import Optional, List, Set, Tuple, cast
from typing import Optional, List, Set, Tuple
from typing_extensions import Final

from mypy.types import Type, AnyType, TypeOfAny, TypedDictType, TPDICT_NAMES
Expand Down Expand Up @@ -191,12 +191,11 @@ def check_typeddict(self,
# This is a valid typed dict, but some type is not ready.
# The caller should defer this until next iteration.
return True, None
items, types, total, ok = res
name, items, types, total, ok = res
if not ok:
# Error. Construct dummy return value.
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(
Expand All @@ -216,7 +215,7 @@ def check_typeddict(self,
call.analyzed.set_line(call.line, call.column)
return True, info

def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[List[str], List[Type],
def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[str, List[str], List[Type],
bool, bool]]:
"""Parse typed dict call expression.

Expand Down Expand Up @@ -262,7 +261,7 @@ def parse_typeddict_args(self, call: CallExpr) -> Optional[Tuple[List[str], List
if has_any_from_unimported_type(t):
self.msg.unimported_type_becomes_any("Type of a TypedDict key", t, dictexpr)
assert total is not None
return items, types, total, ok
return args[0].value, items, types, total, ok

def parse_typeddict_fields_with_types(
self,
Expand Down Expand Up @@ -293,9 +292,9 @@ def parse_typeddict_fields_with_types(
return items, types, True

def fail_typeddict_arg(self, message: str,
context: Context) -> Tuple[List[str], List[Type], bool, bool]:
context: Context) -> Tuple[str, List[str], List[Type], bool, bool]:
self.fail(message, context)
return [], [], True, False
return '', [], [], True, False

def build_typeddict_typeinfo(self, name: str, items: List[str],
types: List[Type],
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,12 @@ p: TaggedPoint
p['z'] # E: TypedDict "TaggedPoint" has no key 'z'
[builtins fixtures/dict.pyi]

[case testTypedDictWithUnicodeName]
# flags: --python-version 2.7
from mypy_extensions import TypedDict
TaggedPoint = TypedDict(u'TaggedPoint', {'type': str, 'x': int, 'y': int})
[builtins fixtures/dict.pyi]

[case testCannotGetItemOfAnonymousTypedDictWithInvalidStringLiteralKey]
from typing import TypeVar
from mypy_extensions import TypedDict
Expand Down
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