Skip to content
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

Fix crash on star unpack in TypedDict #16116

Merged
merged 1 commit into from
Sep 15, 2023
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
3 changes: 1 addition & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,7 @@ def validate_star_typeddict_item(
Note `result` and `always_present_keys` are updated in place. Return true if the
expression `item_arg` may valid in `callee` TypedDict context.
"""
with self.chk.local_type_map(), self.msg.filter_errors():
inferred = get_proper_type(self.accept(item_arg, type_context=callee))
inferred = get_proper_type(self.accept(item_arg, type_context=callee))
possible_tds = []
if isinstance(inferred, TypedDictType):
possible_tds = [inferred]
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -3224,3 +3224,15 @@ t2: Foo = {**y} # E: Missing key "a" for TypedDict "Foo"
t3: Foo = {**z} # E: Missing key "a" for TypedDict "Foo"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictUnpackError]
from typing import TypedDict

class Foo(TypedDict):
a: int

def foo(x: int) -> Foo: ...

f: Foo = {**foo("no")} # E: Argument 1 to "foo" has incompatible type "str"; expected "int"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]
34 changes: 34 additions & 0 deletions test-data/unit/reports.test
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ def untyped_function():
</packages>
</coverage>

[case testCoberturaStarUnpacking]
# cmd: mypy --cobertura-xml-report build a.py
[file a.py]
from typing import TypedDict

class MyDict(TypedDict):
a: int

def foo(a: int) -> MyDict:
return {"a": a}
md: MyDict = MyDict(**foo(42))
[outfile build/cobertura.xml]
<coverage timestamp="$TIMESTAMP" version="$VERSION" line-rate="0.8333" branch-rate="0">
<sources>
<source>$PWD</source>
</sources>
<packages>
<package complexity="1.0" name="a" branch-rate="0" line-rate="0.8333">
<classes>
<class complexity="1.0" filename="a.py" name="a.py" branch-rate="0" line-rate="0.8333">
<methods/>
<lines>
<line branch="false" hits="1" number="1" precision="precise"/>
<line branch="false" hits="1" number="3" precision="precise"/>
<line branch="false" hits="0" number="4" precision="any"/>
<line branch="false" hits="1" number="6" precision="precise"/>
<line branch="false" hits="1" number="7" precision="precise"/>
<line branch="false" hits="1" number="8" precision="precise"/>
</lines>
</class>
</classes>
</package>
</packages>
</coverage>

[case testAnyExprReportDivisionByZero]
# cmd: mypy --any-exprs-report=out -c 'pass'
Expand Down