Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Do not use special names in STRING format
  • Loading branch information
JelleZijlstra committed Apr 23, 2025
commit 525a4e499c68177b418f3dabe983dc91ab7e4028
20 changes: 14 additions & 6 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,13 @@ def __convert_to_ast(self, other):
if isinstance(other.__ast_node__, str):
return ast.Name(id=other.__ast_node__), other.__extra_names__
return other.__ast_node__, other.__extra_names__
elif other is None or type(other) in (str, int, float, bool, complex):
elif (
# In STRING format we don't bother with the create_unique_name() dance;
# it's better to emit the repr() of the object instead of an opaque name.
self.__stringifier_dict__.format == Format.STRING
or other is None
or type(other) in (str, int, float, bool, complex)
):
return ast.Constant(value=other), None
elif type(other) is dict:
extra_names = {}
Expand Down Expand Up @@ -519,14 +525,15 @@ def unary_op(self):


class _StringifierDict(dict):
def __init__(self, namespace, globals=None, owner=None, is_class=False):
def __init__(self, namespace, *, globals=None, owner=None, is_class=False, format):
super().__init__(namespace)
self.namespace = namespace
self.globals = globals
self.owner = owner
self.is_class = is_class
self.stringifiers = []
self.next_id = 1
self.format = format

def __missing__(self, key):
fwdref = _Stringifier(
Expand Down Expand Up @@ -587,7 +594,7 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
# possibly constants if the annotate function uses them directly). We then
# convert each of those into a string to get an approximation of the
# original source.
globals = _StringifierDict({})
globals = _StringifierDict({}, format=format)
if annotate.__closure__:
freevars = annotate.__code__.co_freevars
new_closure = []
Expand Down Expand Up @@ -637,9 +644,10 @@ def call_annotate_function(annotate, format, *, owner=None, _is_evaluate=False):
is_class = isinstance(owner, type)
globals = _StringifierDict(
namespace,
annotate.__globals__,
owner,
is_class,
globals=annotate.__globals__,
owner=owner,
is_class=is_class,
format=format,
)
if annotate.__closure__:
freevars = annotate.__code__.co_freevars
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,17 @@ def f(fstring_format: f"{a:02d}"):
with self.assertRaisesRegex(TypeError, format_msg):
annotationlib.get_annotations(f, format=Format.STRING)

def test_shenanigans(self):
# In cases like this we can't reconstruct the source; test that we do something
# halfway reasonable.
def f(x: x | (1).__class__, y: (1).__class__):
pass

self.assertEqual(
annotationlib.get_annotations(f, format=Format.STRING),
{"x": "x | <class 'int'>", "y": "<class 'int'>"},
)


class TestForwardRefClass(unittest.TestCase):
def test_special_attrs(self):
Expand Down
Loading