@@ -1645,6 +1645,37 @@ def test_final_forward_ref(self):
16451645 self .assertNotEqual (gth (Loop , globals ())['attr' ], Final [int ])
16461646 self .assertNotEqual (gth (Loop , globals ())['attr' ], Final )
16471647
1648+ def test_annotation_and_optional_default (self ):
1649+ annotation = Annotated [Union [int , None ], "data" ]
1650+ optional_annotation = Optional [annotation ]
1651+
1652+ def wanted_optional (bar : optional_annotation ): ...
1653+ def wanted_optional_default (bar : optional_annotation = None ): ...
1654+ def wanted_optional_ref (bar : 'Optional[Annotated[Union[int, None], "data"]]' ): ...
1655+
1656+ def no_optional (bar : annotation ): ...
1657+ def no_optional_default (bar : annotation = None ): ...
1658+ def no_optional_defaultT (bar : Union [annotation , T ] = None ): ...
1659+ def no_optional_defaultT_ref (bar : "Union[annotation, T]" = None ): ...
1660+
1661+ for func in (wanted_optional , wanted_optional_default , wanted_optional_ref ):
1662+ self .assertEqual (
1663+ get_type_hints (func , include_extras = True ),
1664+ {"bar" : optional_annotation }
1665+ )
1666+
1667+ for func in (no_optional , no_optional_default ):
1668+ self .assertEqual (
1669+ get_type_hints (func , include_extras = True ),
1670+ {"bar" : annotation }
1671+ )
1672+
1673+ for func in (no_optional_defaultT , no_optional_defaultT_ref ):
1674+ self .assertEqual (
1675+ get_type_hints (func , globals (), locals (), include_extras = True ),
1676+ {"bar" : Union [annotation , T ]}
1677+ )
1678+
16481679
16491680class GetUtilitiesTestCase (TestCase ):
16501681 def test_get_origin (self ):
@@ -4993,36 +5024,6 @@ def test_nested_annotated_with_unhashable_metadata(self):
49935024 self .assertEqual (X .__origin__ , List [Annotated [str , {"unhashable_metadata" }]])
49945025 self .assertEqual (X .__metadata__ , ("metadata" ,))
49955026
4996- def test_get_type_hints (self ):
4997- annotation = Annotated [Union [int , None ], "data" ]
4998- optional_annotation = Optional [annotation ]
4999-
5000- def wanted_optional (bar : optional_annotation ): ...
5001- def wanted_optional_default (bar : optional_annotation = None ): ...
5002- def wanted_optional_ref (bar : 'Optional[Annotated[Union[int, None], "data"]]' ): ...
5003-
5004- def no_optional (bar : annotation ): ...
5005- def no_optional_default (bar : annotation = None ): ...
5006- def no_optional_defaultT (bar : Union [annotation , T ] = None ): ...
5007- def no_optional_defaultT_ref (bar : "Union[annotation, T]" = None ): ...
5008-
5009- for func in (wanted_optional , wanted_optional_default , wanted_optional_ref ):
5010- self .assertEqual (
5011- get_type_hints (func , include_extras = True ),
5012- {"bar" : optional_annotation }
5013- )
5014-
5015- for func in (no_optional , no_optional_default ):
5016- self .assertEqual (
5017- get_type_hints (func , include_extras = True ),
5018- {"bar" : annotation }
5019- )
5020-
5021- for func in (no_optional_defaultT , no_optional_defaultT_ref ):
5022- self .assertEqual (
5023- get_type_hints (func , globals (), locals (), include_extras = True ),
5024- {"bar" : Union [annotation , T ]}
5025- )
50265027
50275028
50285029class GetTypeHintsTests (BaseTestCase ):
0 commit comments