@@ -365,7 +365,7 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
365
365
366
366
@pytest .mark .parametrize (("annotation" , "expected_result" ), _CASES )
367
367
def test_format_annotation (inv : Inventory , annotation : Any , expected_result : str ) -> None :
368
- conf = create_autospec (Config , _annotation_globals = globals ())
368
+ conf = create_autospec (Config , _annotation_globals = globals (), always_use_bars_union = False )
369
369
result = format_annotation (annotation , conf )
370
370
assert result == expected_result
371
371
@@ -377,7 +377,12 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
377
377
# encapsulate Union in typing.Optional
378
378
expected_result_not_simplified = ":py:data:`~typing.Optional`\\ \\ [" + expected_result_not_simplified
379
379
expected_result_not_simplified += "]"
380
- conf = create_autospec (Config , simplify_optional_unions = False , _annotation_globals = globals ())
380
+ conf = create_autospec (
381
+ Config ,
382
+ simplify_optional_unions = False ,
383
+ _annotation_globals = globals (),
384
+ always_use_bars_union = False ,
385
+ )
381
386
assert format_annotation (annotation , conf ) == expected_result_not_simplified
382
387
383
388
# Test with the "fully_qualified" flag turned on
@@ -397,7 +402,12 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
397
402
expected_result = expected_result .replace ("~nptyping" , "nptyping" )
398
403
expected_result = expected_result .replace ("~numpy" , "numpy" )
399
404
expected_result = expected_result .replace ("~" + __name__ , __name__ )
400
- conf = create_autospec (Config , typehints_fully_qualified = True , _annotation_globals = globals ())
405
+ conf = create_autospec (
406
+ Config ,
407
+ typehints_fully_qualified = True ,
408
+ _annotation_globals = globals (),
409
+ always_use_bars_union = False ,
410
+ )
401
411
assert format_annotation (annotation , conf ) == expected_result
402
412
403
413
# Test for the correct role (class vs data) using the official Sphinx inventory
@@ -413,6 +423,26 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
413
423
assert m .group ("role" ) == expected_role
414
424
415
425
426
+ @pytest .mark .parametrize (
427
+ ("annotation" , "expected_result" ),
428
+ [
429
+ ("int | float" , ":py:class:`int` | :py:class:`float`" ),
430
+ ("int | float | None" , ":py:class:`int` | :py:class:`float` | :py:obj:`None`" ),
431
+ ("Union[int, float]" , ":py:class:`int` | :py:class:`float`" ),
432
+ ("Union[int, float, None]" , ":py:class:`int` | :py:class:`float` | :py:obj:`None`" ),
433
+ ("Optional[int | float]" , ":py:class:`int` | :py:class:`float` | :py:obj:`None`" ),
434
+ ("Optional[Union[int, float]]" , ":py:class:`int` | :py:class:`float` | :py:obj:`None`" ),
435
+ ("Union[int | float, str]" , ":py:class:`int` | :py:class:`float` | :py:class:`str`" ),
436
+ ("Union[int, float] | str" , ":py:class:`int` | :py:class:`float` | :py:class:`str`" ),
437
+ ],
438
+ )
439
+ @pytest .mark .skipif (not PY310_PLUS , reason = "| union doesn't work before py310" )
440
+ def test_always_use_bars_union (annotation : str , expected_result : str ) -> None :
441
+ conf = create_autospec (Config , always_use_bars_union = True )
442
+ result = format_annotation (eval (annotation ), conf ) # noqa: S307
443
+ assert result == expected_result
444
+
445
+
416
446
@pytest .mark .parametrize ("library" , [typing , typing_extensions ], ids = ["typing" , "typing_extensions" ])
417
447
@pytest .mark .parametrize (
418
448
("annotation" , "params" , "expected_result" ),
@@ -519,12 +549,13 @@ class dummy_module.DataClass(x)
519
549
520
550
521
551
def maybe_fix_py310 (expected_contents : str ) -> str :
552
+ if sys .version_info >= (3 , 11 ):
553
+ return expected_contents
522
554
if not PY310_PLUS :
523
555
return expected_contents .replace ('"' , "" )
524
556
525
557
for old , new in [
526
- ("bool | None" , '"Optional"["bool"]' ),
527
- ("str | None" , '"Optional"["str"]' ),
558
+ ('"str" | "None"' , '"Optional"["str"]' ),
528
559
]:
529
560
expected_contents = expected_contents .replace (old , new )
530
561
return expected_contents
@@ -550,15 +581,16 @@ def test_sphinx_output_future_annotations(app: SphinxTestApp, status: StringIO)
550
581
Method docstring.
551
582
552
583
Parameters:
553
- * **x** (bool | None) -- foo
584
+ * **x** (" bool" | " None" ) -- foo
554
585
555
586
* **y** ("int" | "str" | "float") -- bar
556
587
557
- * **z** (str | None) -- baz
588
+ * **z** (" str" | " None" ) -- baz
558
589
559
590
Return type:
560
591
"str"
561
592
"""
593
+ expected_contents = dedent (expected_contents )
562
594
expected_contents = maybe_fix_py310 (dedent (expected_contents ))
563
595
assert contents == expected_contents
564
596
0 commit comments