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

[mypyc] Support unpacking mappings in dict display #15203

Merged
merged 1 commit into from
May 16, 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
2 changes: 1 addition & 1 deletion mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
# Operation used for **value in dict displays.
# This is mostly like dict.update(obj), but has customized error handling.
dict_update_in_display_op = custom_op(
arg_types=[dict_rprimitive, dict_rprimitive],
arg_types=[dict_rprimitive, object_rprimitive],
return_type=c_int_rprimitive,
c_function_name="CPyDict_UpdateInDisplay",
error_kind=ERR_NEG_INT,
Expand Down
9 changes: 7 additions & 2 deletions mypyc/test-data/run-misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ exit! a ohno
caught

[case testDisplays]
from typing import List, Set, Tuple, Sequence, Dict, Any
from typing import List, Set, Tuple, Sequence, Dict, Any, Mapping

def listDisplay(x: List[int], y: List[int]) -> List[int]:
return [1, 2, *x, *y, 3]
Expand All @@ -172,12 +172,17 @@ def tupleDisplay(x: Sequence[str], y: Sequence[str]) -> Tuple[str, ...]:
def dictDisplay(x: str, y1: Dict[str, int], y2: Dict[str, int]) -> Dict[str, int]:
return {x: 2, **y1, 'z': 3, **y2}

def dictDisplayUnpackMapping(obj: Mapping[str, str]) -> Dict[str, str]:
return {**obj, "env": "value"}

[file driver.py]
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay
import os
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay, dictDisplayUnpackMapping
assert listDisplay([4], [5, 6]) == [1, 2, 4, 5, 6, 3]
assert setDisplay({4}, {5}) == {1, 2, 3, 4, 5}
assert tupleDisplay(['4', '5'], ['6']) == ('1', '2', '4', '5', '6', '3')
assert dictDisplay('x', {'y1': 1}, {'y2': 2, 'z': 5}) == {'x': 2, 'y1': 1, 'y2': 2, 'z': 5}
assert dictDisplayUnpackMapping(os.environ) == {**os.environ, "env": "value"}

[case testArbitraryLvalues]
from typing import List, Dict, Any
Expand Down