@@ -1649,32 +1649,47 @@ def test_annotation_and_optional_default(self):
16491649 annotation = Annotated [Union [int , None ], "data" ]
16501650 optional_annotation = Optional [annotation ]
16511651
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- )
1652+ cases = {
1653+ ((), False ): {},
1654+ ((), True ): {},
1655+ (int , False ): {"x" : int },
1656+ (int , True ): {"x" : int },
1657+ (Optional [int ], False ): {"x" : Optional [int ]},
1658+ (Optional [int ], True ): {"x" : Optional [int ]},
1659+ (optional_annotation , False ): {"x" : optional_annotation },
1660+ (optional_annotation , True ): {"x" : optional_annotation },
1661+ (str (optional_annotation ), True ): {"x" : optional_annotation },
1662+ (annotation , False ): {"x" : annotation },
1663+ (annotation , True ): {"x" : annotation },
1664+ (Union [annotation , T ], False ): {"x" : Union [annotation , T ]},
1665+ (Union [annotation , T ], True ): {"x" : Union [annotation , T ]},
1666+ (Union [str , None , "str" ], False ): {"x" : Optional [str ]},
1667+ (Union [str , None , "str" ], True ): {"x" : Optional [str ]},
1668+ (Union [str , "str" ], False ): {
1669+ "x" : str
1670+ if sys .version_info >= (3 , 9 )
1671+ # _eval_type does not resolve correctly to str in 3.8
1672+ else typing ._eval_type (Union [str , "str" ], None , None ),
1673+ },
1674+ (Union [str , "str" ], True ): {"x" : str },
1675+ (List ["str" ], False ): {"x" : List [str ]},
1676+ (List ["str" ], True ): {"x" : List [str ]},
1677+ (Optional [List [str ]], False ): {"x" : Optional [List [str ]]},
1678+ (Optional [List [str ]], True ): {"x" : Optional [List [str ]]},
1679+ }
16721680
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- )
1681+ for (annot , none_default ), expected in cases .items ():
1682+ with self .subTest (annotation = annot , none_default = none_default , expected_type_hints = expected ):
1683+ if annot == ():
1684+ if none_default :
1685+ def func (x = None ): pass
1686+ else :
1687+ def func (x ): pass
1688+ elif none_default :
1689+ def func (x : annot = None ): pass
1690+ else :
1691+ def func (x : annot ): pass
1692+ self .assertEqual (get_type_hints (func , include_extras = True ), expected )
16781693
16791694
16801695class GetUtilitiesTestCase (TestCase ):
0 commit comments