Skip to content
Merged
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
Test get_annotations(format=Format.VALUE) for stringized annotation…
…s on wrapped partial functions
  • Loading branch information
dr-carlos committed Nov 9, 2025
commit cf735258dea1b3551e3045ac3d89d8481ea6397a
39 changes: 39 additions & 0 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pickle
from string.templatelib import Template, Interpolation
import typing
import sys
import unittest
from annotationlib import (
Format,
Expand Down Expand Up @@ -811,6 +812,44 @@ def test_stringized_annotations_on_wrapper(self):
{"a": "int", "b": "str", "return": "MyClass"},
)

def test_stringized_annotations_on_partial_wrapper(self):
isa = inspect_stringized_annotations

def times_three_str(fn: typing.Callable[[str], isa.MyClass]):
@functools.wraps(fn)
def wrapper(b: "str") -> "MyClass":
return fn(b * 3)

return wrapper

wrapped = times_three_str(functools.partial(isa.function, 1))
self.assertEqual(wrapped("x"), isa.MyClass(1, "xxx"))
self.assertIsNot(wrapped.__globals__, isa.function.__globals__)
self.assertEqual(
get_annotations(wrapped, eval_str=True),
{"b": str, "return": isa.MyClass},
)
self.assertEqual(
get_annotations(wrapped, eval_str=False),
{"b": "str", "return": "MyClass"},
)

# If functools is not loaded, names will be evaluated in the current
# module instead of being unwrapped to the original.
functools_mod = sys.modules["functools"]
del sys.modules["functools"]

self.assertEqual(
get_annotations(wrapped, eval_str=True),
{"b": str, "return": MyClass},
)
self.assertEqual(
get_annotations(wrapped, eval_str=False),
{"b": "str", "return": "MyClass"},
)

sys.modules["functools"] = functools_mod

def test_stringized_annotations_on_class(self):
isa = inspect_stringized_annotations
# test that local namespace lookups work
Expand Down