mirror of
https://github.com/python/cpython.git
synced 2025-12-02 15:48:58 +00:00
gh-137706: make typing._is_unpacked_typevartuple check for True instead of truthy (#137712)
This commit is contained in:
parent
bde1291952
commit
7e652f402f
3 changed files with 17 additions and 1 deletions
|
|
@ -9788,6 +9788,19 @@ class AnnotatedTests(BaseTestCase):
|
|||
self.assertIs(type(field_c2.__metadata__[0]), float)
|
||||
self.assertIs(type(field_c3.__metadata__[0]), bool)
|
||||
|
||||
def test_forwardref_partial_evaluation(self):
|
||||
# Test that Annotated partially evaluates if it contains a ForwardRef
|
||||
# See: https://github.com/python/cpython/issues/137706
|
||||
def f(x: Annotated[undefined, '']): pass
|
||||
|
||||
ann = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)
|
||||
|
||||
# Test that the attributes are retrievable from the partially evaluated annotation
|
||||
x_ann = ann['x']
|
||||
self.assertIs(get_origin(x_ann), Annotated)
|
||||
self.assertEqual(x_ann.__origin__, EqualToForwardRef('undefined', owner=f))
|
||||
self.assertEqual(x_ann.__metadata__, ('',))
|
||||
|
||||
|
||||
class TypeAliasTests(BaseTestCase):
|
||||
def test_canonical_usage_with_variable_annotation(self):
|
||||
|
|
|
|||
|
|
@ -1027,8 +1027,10 @@ def evaluate_forward_ref(
|
|||
|
||||
|
||||
def _is_unpacked_typevartuple(x: Any) -> bool:
|
||||
# Need to check 'is True' here
|
||||
# See: https://github.com/python/cpython/issues/137706
|
||||
return ((not isinstance(x, type)) and
|
||||
getattr(x, '__typing_is_unpacked_typevartuple__', False))
|
||||
getattr(x, '__typing_is_unpacked_typevartuple__', False) is True)
|
||||
|
||||
|
||||
def _is_typevar_like(x: Any) -> bool:
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fix the partial evaluation of annotations that use ``typing.Annotated[T, x]`` where ``T`` is a forward reference.
|
||||
Loading…
Add table
Add a link
Reference in a new issue