10000 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 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
12 changes: 12 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1694,6 +1694,18 @@ def visit_dict_expr(self, e: DictExpr) -> Type:

Translate it into a call to dict(), with provisions for **expr.
"""
# if the dict literal doesn't match TypedDict, check_typeddict_call_with_dict reports
# an error, but returns the TypedDict type that matches the literal it found
# that would cause a second error when that TypedDict type is returned upstream
# to avoid the second error, we always return TypedDict type that was requested
if isinstance(self.type_context[-1], TypedDictType):
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"
stargs = [] # type: List[Expression] # For "**expr"
Expand Down
5 changes: 4 additions & 1 deletion test-data/unit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ Add the test in this format anywhere in the file:
- `# E: abc...` indicates that this line should result in type check error
with text "abc..."
- note a space after `E:` and `flags:`
- lines without `# E: ` should cause no type check errors
- `# E:12` adds column number to the expected error< 10000 /span>
- repeating `# E: ` several times in one line indicates multiple expected errors in one line
- `W: ...` and `N: ...` works exactly like `E:`, but report a warning and a note respectively
- lines that don't contain the above should cause no type check errors
- optional `[builtins fixtures/...]` tells the type checker to use
stubs from the indicated file (see Fixtures section below)
- optional `[out]` is an alternative to the "# E:" notation: it indicates that
Expand Down
39 changes: 39 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -610,3 +610,42 @@ def f() -> None:
A = TypedDict('A', {'x': int})
A # E: Name 'A' is not defined
[builtins fixtures/dict.pyi]


-- Use dict literals

[case testTypedDictDictLiterals]
from mypy_extensions import TypedDict

Point = TypedDict('Point', {'x': int, 'y': int})

def f(p: Point) -> None:
p = {'x': 2, 'y': 3}
p = {'x': 2} # E: Expected items ['x', 'y'] but found ['x'].
p = dict(x=2, y=3)

f({'x': 1, 'y': 3})
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'}


[builtins fixtures/dict.pyi]


[case testTypedDictExplicitTypes]
from mypy_extensions import TypedDict

Point = TypedDict('Point', {'x': int, 'y': int})

p1: Point = {'x': 'hi'} # E: Expected items ['x', 'y'] but found ['x'].

p2: Point
p2 = dict(x='bye') # E: Expected items ['x', 'y'] but found ['x'].

p3 = Point(x=1, y=2)
p3 = {'x': 'hi'} # E: Expected items ['x', 'y'] but found ['x'].

p4: Point = {'x': 1, 'y': 2}

[builtins fixtures/dict.pyi]
0