bpo-46195: Do not add Optional in get_type_hints (GH-30304)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Nikita Sobolev 2022-03-02 08:29:46 +03:00 committed by GitHub
parent 6ddb09f35b
commit 20a1c8ee4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 34 deletions

View file

@ -2828,16 +2828,15 @@ class ForwardRefTests(BaseTestCase):
t = Node[int]
both_hints = get_type_hints(t.add_both, globals(), locals())
self.assertEqual(both_hints['left'], Optional[Node[T]])
self.assertEqual(both_hints['right'], Optional[Node[T]])
self.assertEqual(both_hints['left'], both_hints['right'])
self.assertEqual(both_hints['stuff'], Optional[int])
self.assertEqual(both_hints['right'], Node[T])
self.assertEqual(both_hints['stuff'], int)
self.assertNotIn('blah', both_hints)
left_hints = get_type_hints(t.add_left, globals(), locals())
self.assertEqual(left_hints['node'], Optional[Node[T]])
right_hints = get_type_hints(t.add_right, globals(), locals())
self.assertEqual(right_hints['node'], Optional[Node[T]])
self.assertEqual(right_hints['node'], Node[T])
def test_forwardref_instance_type_error(self):
fr = typing.ForwardRef('int')
@ -3630,6 +3629,18 @@ class GetTypeHintTests(BaseTestCase):
{'other': MySet[T], 'return': MySet[T]}
)
def test_get_type_hints_annotated_with_none_default(self):
# See: https://bugs.python.org/issue46195
def annotated_with_none_default(x: Annotated[int, 'data'] = None): ...
self.assertEqual(
get_type_hints(annotated_with_none_default),
{'x': int},
)
self.assertEqual(
get_type_hints(annotated_with_none_default, include_extras=True),
{'x': Annotated[int, 'data']},
)
def test_get_type_hints_classes_str_annotations(self):
class Foo:
y = str