gh-119180: Improvements to ForwardRef.evaluate (#122210)

Noticed some issues while writing documentation for this method.
This commit is contained in:
Jelle Zijlstra 2024-08-11 16:42:57 -07:00 committed by GitHub
parent a6644d4464
commit 016f4b5975
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 60 additions and 11 deletions

View file

@ -5,7 +5,7 @@ import functools
import itertools
import pickle
import unittest
from annotationlib import Format, get_annotations, get_annotate_function
from annotationlib import Format, ForwardRef, get_annotations, get_annotate_function
from typing import Unpack
from test.test_inspect import inspect_stock_annotations
@ -250,6 +250,46 @@ class TestForwardRefClass(unittest.TestCase):
with self.assertRaises(TypeError):
pickle.dumps(fr, proto)
def test_evaluate_with_type_params(self):
class Gen[T]:
alias = int
with self.assertRaises(NameError):
ForwardRef("T").evaluate()
with self.assertRaises(NameError):
ForwardRef("T").evaluate(type_params=())
with self.assertRaises(NameError):
ForwardRef("T").evaluate(owner=int)
T, = Gen.__type_params__
self.assertIs(ForwardRef("T").evaluate(type_params=Gen.__type_params__), T)
self.assertIs(ForwardRef("T").evaluate(owner=Gen), T)
with self.assertRaises(NameError):
ForwardRef("alias").evaluate(type_params=Gen.__type_params__)
self.assertIs(ForwardRef("alias").evaluate(owner=Gen), int)
# If you pass custom locals, we don't look at the owner's locals
with self.assertRaises(NameError):
ForwardRef("alias").evaluate(owner=Gen, locals={})
# But if the name exists in the locals, it works
self.assertIs(
ForwardRef("alias").evaluate(owner=Gen, locals={"alias": str}), str
)
def test_fwdref_with_module(self):
self.assertIs(ForwardRef("Format", module=annotationlib).evaluate(), Format)
with self.assertRaises(NameError):
# If globals are passed explicitly, we don't look at the module dict
ForwardRef("Format", module=annotationlib).evaluate(globals={})
def test_fwdref_value_is_cached(self):
fr = ForwardRef("hello")
with self.assertRaises(NameError):
fr.evaluate()
self.assertIs(fr.evaluate(globals={"hello": str}), str)
self.assertIs(fr.evaluate(), str)
class TestGetAnnotations(unittest.TestCase):
def test_builtin_type(self):