Skip to content

Commit

Permalink
[mypyc] Support unpacking mappings in dict display (#15203)
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 authored May 16, 2023
1 parent acce270 commit 16667f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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

0 comments on commit 16667f3

Please sign in to comment.