mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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:
parent
6ddb09f35b
commit
20a1c8ee4b
4 changed files with 26 additions and 34 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue