Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3d7fec8

Browse filesBrowse files
committed
Handle displays
1 parent 1c629dc commit 3d7fec8
Copy full SHA for 3d7fec8

File tree

Expand file treeCollapse file tree

2 files changed

+49
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+49
-0
lines changed

‎Lib/annotationlib.py

Copy file name to clipboardExpand all lines: Lib/annotationlib.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,30 @@ def __convert_to_ast(self, other):
307307
return other.__ast_node__, other.__extra_names__
308308
elif other is None or type(other) in (str, int, float, bool, complex):
309309
return ast.Constant(value=other), None
310+
elif type(other) is dict:
311+
extra_names = {}
312+
keys = []
313+
values = []
314+
for key, value in other.items():
315+
new_key, new_extra_names = self.__convert_to_ast(key)
316+
if new_extra_names is not None:
317+
extra_names.update(new_extra_names)
318+
keys.append(new_key)
319+
new_value, new_extra_names = self.__convert_to_ast(value)
320+
if new_extra_names is not None:
321+
extra_names.update(new_extra_names)
322+
values.append(new_value)
323+
return ast.Dict(keys, values), extra_names
324+
elif type(other) in (list, tuple, set):
325+
extra_names = {}
326+
elts = []
327+
for elt in other:
328+
new_elt, new_extra_names = self.__convert_to_ast(elt)
329+
if new_extra_names is not None:
330+
extra_names.update(new_extra_names)
331+
elts.append(new_elt)
332+
ast_class = {list: ast.List, tuple: ast.Tuple, set: ast.Set}[type(other)]
333+
return ast_class(elts), extra_names
310334
else:
311335
name = self.__stringifier_dict__.create_unique_name()
312336
return ast.Name(id=name), {name: other}

‎Lib/test/test_annotationlib.py

Copy file name to clipboardExpand all lines: Lib/test/test_annotationlib.py
+25Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,31 @@ def f(
336336
},
337337
)
338338

339+
def test_displays(self):
340+
# Simple case first
341+
def f(x: a[[int, str], float]):
342+
pass
343+
anno = annotationlib.get_annotations(f, format=Format.STRING)
344+
self.assertEqual(anno, {"x": "a[[int, str], float]"})
345+
346+
def g(
347+
w: a[[int, str], float],
348+
x: a[{int, str}, 3],
349+
y: a[{int: str}, 4],
350+
z: a[(int, str), 5],
351+
):
352+
pass
353+
anno = annotationlib.get_annotations(g, format=Format.STRING)
354+
self.assertEqual(
355+
anno,
356+
{
357+
"w": "a[[int, str], float]",
358+
"x": "a[{int, str}, 3]",
359+
"y": "a[{int: str}, 4]",
360+
"z": "a[(int, str), 5]",
361+
},
362+
)
363+
339364
def test_nested_expressions(self):
340365
def f(
341366
nested: list[Annotated[set[int], "set of ints", 4j]],

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.