8000 Accept compatible dict in place of TypedDict by pkch · Pull Request #3035 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Accept compatible dict in place of TypedDict #3035

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 6 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Suppress second error message when using wrong TypedDict fields
  • Loading branch information
pkch committed Mar 21, 2017
commit eb6914a11e6ab42c53940759f408b2dddd43022d
3 changes: 2 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1695,11 +1695,12 @@ def visit_dict_expr(self, e: DictExpr) -> Type:
Translate it into a call to dict(), with provisions for **expr.
"""
if isinstance(self.type_context[-1], TypedDictType):
return self.check_typeddict_call_with_dict(
self.check_typeddict_call_with_dict(
callee=self.type_context[-1],
kwargs=e,
context=e
)
return self.type_context[-1].copy_modified()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please and a comment here about why you do it like this, and how it works?


# Collect function arguments, watching out for **expr.
args = [] # type: List[Expression] # Regular "key: value"
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ def f(p: Point) -> None:
p = dict(x=2, y=3)

f({'x': 1, 'y': 3})
f({'x': 1, 'y': 'z'}) # E: Argument 1 to "f" has incompatible type "TypedDict(x=int, y=str)"; expected "Point" # E: Incompatible types (expression has type "str", TypedDict item "y" has type "int")
f({'x': 1, 'y': 'z'}) # E: Incompatible types (expression has type "str", TypedDict item "y" has type "int")

f(dict(x=1, y=3))
f(dict(x=1, y=3, z=4)) # E: Expected items ['x', 'y'] but found ['x', 'y', 'z'].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add one more test case with few tests where you assign to variables with explicit types, like:

p: Point = {'x', 'hi'}

and/or

p: Point
p = dict(x='bye')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also maybe a test for

p = Point(x=1, y=2)
p = {x='hi'}

Expand Down
0