@@ -244,7 +244,10 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
244244 r":py:data:`~typing.Tuple`\ \[:py:class:`str`, :py:data:`...<Ellipsis>`]" ,
245245 id = "Tuple-str-Ellipsis" ,
246246 ),
247- pytest .param (Union , "" if sys .version_info >= (3 , 14 ) else ":py:data:`~typing.Union`" , id = "Union" ),
247+ pytest .param (Union , f":py:{ 'class' if sys .version_info >= (3 , 14 ) else 'data' } :`~typing.Union`" , id = "Union" ),
248+ pytest .param (
249+ types .UnionType , f":py:{ 'class' if sys .version_info >= (3 , 14 ) else 'data' } :`~typing.Union`" , id = "UnionType"
250+ ),
248251 pytest .param (
249252 Union [str , bool ],
250253 ":py:class:`str` | :py:class:`bool`"
@@ -280,6 +283,12 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
280283 else r":py:data:`~typing.Optional`\ \[:py:class:`str`]" ,
281284 id = "Optional-str-None" ,
282285 ),
286+ pytest .param (
287+ type [T ] | types .UnionType ,
288+ ":py:class:`type`\\ \\ [:py:class:`~typing.TypeVar`\\ \\ (``T``)] | "
289+ f":py:{ 'class' if sys .version_info >= (3 , 14 ) else 'data' } :`~typing.Union`" ,
290+ id = "typevar union bar uniontype" ,
291+ ),
283292 pytest .param (
284293 Optional [str | bool ],
285294 ":py:class:`str` | :py:class:`bool` | :py:obj:`None`"
@@ -436,18 +445,16 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
436445 assert format_annotation (annotation , conf ) == expected_result
437446
438447 # Test for the correct role (class vs data) using the official Sphinx inventory
439- if any (modname in expected_result for modname in ("typing" , "types" )) and not (
440- sys .version_info >= (3 , 14 ) and isinstance (annotation , Union )
448+ if (
449+ result .count (":py:" ) == 1
450+ and ("typing" in result or "types" in result )
451+ and (match := re .match (r"^:py:(?P<role>class|data|func):`~(?P<name>[^`]+)`" , result ))
441452 ):
442- m = re .match (r"^:py:(?P<role>class|data|func):`~(?P<name>[^`]+)`" , result )
443- assert m , "No match"
444- name = m .group ("name" )
453+ name = match .group ("name" )
445454 expected_role = next ((o .role for o in inv .objects if o .name == name ), None )
446- if expected_role :
447- if expected_role == "function" :
448- expected_role = "func"
449-
450- assert m .group ("role" ) == expected_role
455+ if expected_role and expected_role == "function" :
456+ expected_role = "func"
457+ assert match .group ("role" ) == expected_role
451458
452459
453460@pytest .mark .parametrize (
0 commit comments