Skip to content

Commit 16667f3

Browse files
authored
[mypyc] Support unpacking mappings in dict display (#15203)
Fixes mypyc/mypyc#994.
1 parent acce270 commit 16667f3

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

mypyc/primitives/dict_ops.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
# Operation used for **value in dict displays.
114114
# This is mostly like dict.update(obj), but has customized error handling.
115115
dict_update_in_display_op = custom_op(
116-
arg_types=[dict_rprimitive, dict_rprimitive],
116+
arg_types=[dict_rprimitive, object_rprimitive],
117117
return_type=c_int_rprimitive,
118118
c_function_name="CPyDict_UpdateInDisplay",
119119
error_kind=ERR_NEG_INT,

mypyc/test-data/run-misc.test

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ exit! a ohno
158158
caught
159159

160160
[case testDisplays]
161-
from typing import List, Set, Tuple, Sequence, Dict, Any
161+
from typing import List, Set, Tuple, Sequence, Dict, Any, Mapping
162162

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

175+
def dictDisplayUnpackMapping(obj: Mapping[str, str]) -> Dict[str, str]:
176+
return {**obj, "env": "value"}
177+
175178
[file driver.py]
176-
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay
179+
import os
180+
from native import listDisplay, setDisplay, tupleDisplay, dictDisplay, dictDisplayUnpackMapping
177181
assert listDisplay([4], [5, 6]) == [1, 2, 4, 5, 6, 3]
178182
assert setDisplay({4}, {5}) == {1, 2, 3, 4, 5}
179183
assert tupleDisplay(['4', '5'], ['6']) == ('1', '2', '4', '5', '6', '3')
180184
assert dictDisplay('x', {'y1': 1}, {'y2': 2, 'z': 5}) == {'x': 2, 'y1': 1, 'y2': 2, 'z': 5}
185+
assert dictDisplayUnpackMapping(os.environ) == {**os.environ, "env": "value"}
181186

182187
[case testArbitraryLvalues]
183188
from typing import List, Dict, Any

0 commit comments

Comments
 (0)