@@ -201,87 +201,87 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
201
201
(type , ":py:class:`type`" ),
202
202
(collections .abc .Callable , ":py:class:`~collections.abc.Callable`" ),
203
203
(Type , ":py:class:`~typing.Type`" ),
204
- (Type [A ], ":py:class:`~typing.Type`\\ [:py:class:`~%s.A`]" % __name__ ),
204
+ (Type [A ], ":py:class:`~typing.Type`\\ \\ [:py:class:`~%s.A`]" % __name__ ),
205
205
(Any , ":py:data:`~typing.Any`" ),
206
206
(AnyStr , ":py:data:`~typing.AnyStr`" ),
207
- (Generic [T ], ":py:class:`~typing.Generic`\\ [:py:class:`~typing.TypeVar`\\ (``T``)]" ),
207
+ (Generic [T ], ":py:class:`~typing.Generic`\\ \\ [:py:class:`~typing.TypeVar`\\ (``T``)]" ),
208
208
(Mapping , ":py:class:`~typing.Mapping`" ),
209
209
(
210
210
Mapping [T , int ], # type: ignore[valid-type]
211
- ":py:class:`~typing.Mapping`\\ [:py:class:`~typing.TypeVar`\\ (``T``), :py:class:`int`]" ,
211
+ ":py:class:`~typing.Mapping`\\ \\ [:py:class:`~typing.TypeVar`\\ (``T``), :py:class:`int`]" ,
212
212
),
213
213
(
214
214
Mapping [str , V_contra ], # type: ignore[valid-type]
215
- ":py:class:`~typing.Mapping`\\ [:py:class:`str`, :py:class:`~typing.TypeVar`\\ ("
215
+ ":py:class:`~typing.Mapping`\\ \\ [:py:class:`str`, :py:class:`~typing.TypeVar`\\ ("
216
216
"``V_contra``, contravariant=True)]" ,
217
217
),
218
218
(
219
219
Mapping [T , U_co ], # type: ignore[valid-type]
220
- ":py:class:`~typing.Mapping`\\ [:py:class:`~typing.TypeVar`\\ (``T``), "
220
+ ":py:class:`~typing.Mapping`\\ \\ [:py:class:`~typing.TypeVar`\\ (``T``), "
221
221
":py:class:`~typing.TypeVar`\\ (``U_co``, covariant=True)]" ,
222
222
),
223
- (Mapping [str , bool ], ":py:class:`~typing.Mapping`\\ [:py:class:`str`, :py:class:`bool`]" ),
223
+ (Mapping [str , bool ], ":py:class:`~typing.Mapping`\\ \\ [:py:class:`str`, :py:class:`bool`]" ),
224
224
(Dict , ":py:class:`~typing.Dict`" ),
225
225
(
226
226
Dict [T , int ], # type: ignore[valid-type]
227
- ":py:class:`~typing.Dict`\\ [:py:class:`~typing.TypeVar`\\ (``T``), :py:class:`int`]" ,
227
+ ":py:class:`~typing.Dict`\\ \\ [:py:class:`~typing.TypeVar`\\ (``T``), :py:class:`int`]" ,
228
228
),
229
229
(
230
230
Dict [str , V_contra ], # type: ignore[valid-type]
231
- ":py:class:`~typing.Dict`\\ [:py:class:`str`, :py:class:`~typing.TypeVar`\\ (``V_contra``, contravariant=True)]" ,
231
+ ":py:class:`~typing.Dict`\\ \\ [:py:class:`str`, :py:class:`~typing.TypeVar`\\ (``V_contra``, contravariant=True)]" , # noqa: E501
232
232
),
233
233
(
234
234
Dict [T , U_co ], # type: ignore[valid-type]
235
- ":py:class:`~typing.Dict`\\ [:py:class:`~typing.TypeVar`\\ (``T``),"
235
+ ":py:class:`~typing.Dict`\\ \\ [:py:class:`~typing.TypeVar`\\ (``T``),"
236
236
" :py:class:`~typing.TypeVar`\\ (``U_co``, covariant=True)]" ,
237
237
),
238
- (Dict [str , bool ], ":py:class:`~typing.Dict`\\ [:py:class:`str`, :py:class:`bool`]" ),
238
+ (Dict [str , bool ], ":py:class:`~typing.Dict`\\ \\ [:py:class:`str`, :py:class:`bool`]" ),
239
239
(Tuple , ":py:data:`~typing.Tuple`" ),
240
- (Tuple [str , bool ], ":py:data:`~typing.Tuple`\\ [:py:class:`str`, :py:class:`bool`]" ),
241
- (Tuple [int , int , int ], ":py:data:`~typing.Tuple`\\ [:py:class:`int`, :py:class:`int`, :py:class:`int`]" ),
242
- (Tuple [str , ...], ":py:data:`~typing.Tuple`\\ [:py:class:`str`, :py:data:`...<Ellipsis>`]" ),
240
+ (Tuple [str , bool ], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`str`, :py:class:`bool`]" ),
241
+ (Tuple [int , int , int ], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`int`, :py:class:`int`, :py:class:`int`]" ),
242
+ (Tuple [str , ...], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`str`, :py:data:`...<Ellipsis>`]" ),
243
243
(Union , ":py:data:`~typing.Union`" ),
244
- (Union [str , bool ], ":py:data:`~typing.Union`\\ [:py:class:`str`, :py:class:`bool`]" ),
245
- (Union [str , bool , None ], ":py:data:`~typing.Union`\\ [:py:class:`str`, :py:class:`bool`, :py:obj:`None`]" ),
246
- pytest .param (Union [str , Any ], ":py:data:`~typing.Union`\\ [:py:class:`str`, :py:data:`~typing.Any`]" ),
247
- (Optional [str ], ":py:data:`~typing.Optional`\\ [:py:class:`str`]" ),
248
- (Union [str , None ], ":py:data:`~typing.Optional`\\ [:py:class:`str`]" ),
244
+ (Union [str , bool ], ":py:data:`~typing.Union`\\ \\ [:py:class:`str`, :py:class:`bool`]" ),
245
+ (Union [str , bool , None ], ":py:data:`~typing.Union`\\ \\ [:py:class:`str`, :py:class:`bool`, :py:obj:`None`]" ),
246
+ pytest .param (Union [str , Any ], ":py:data:`~typing.Union`\\ \\ [:py:class:`str`, :py:data:`~typing.Any`]" ),
247
+ (Optional [str ], ":py:data:`~typing.Optional`\\ \\ [:py:class:`str`]" ),
248
+ (Union [str , None ], ":py:data:`~typing.Optional`\\ \\ [:py:class:`str`]" ),
249
249
(
250
250
Optional [Union [str , bool ]],
251
- ":py:data:`~typing.Union`\\ [:py:class:`str`, :py:class:`bool`, :py:obj:`None`]" ,
251
+ ":py:data:`~typing.Union`\\ \\ [:py:class:`str`, :py:class:`bool`, :py:obj:`None`]" ,
252
252
),
253
253
(Callable , ":py:data:`~typing.Callable`" ),
254
- (Callable [..., int ], ":py:data:`~typing.Callable`\\ [:py:data:`...<Ellipsis>`, :py:class:`int`]" ),
255
- (Callable [[int ], int ], ":py:data:`~typing.Callable`\\ [\\ [:py:class:`int`], :py:class:`int`]" ),
254
+ (Callable [..., int ], ":py:data:`~typing.Callable`\\ \\ [:py:data:`...<Ellipsis>`, :py:class:`int`]" ),
255
+ (Callable [[int ], int ], ":py:data:`~typing.Callable`\\ \\ [\\ [:py:class:`int`], :py:class:`int`]" ),
256
256
(
257
257
Callable [[int , str ], bool ],
258
- ":py:data:`~typing.Callable`\\ [\\ [:py:class:`int`, :py:class:`str`], :py:class:`bool`]" ,
258
+ ":py:data:`~typing.Callable`\\ \\ [\\ [:py:class:`int`, :py:class:`str`], :py:class:`bool`]" ,
259
259
),
260
260
(
261
261
Callable [[int , str ], None ],
262
- ":py:data:`~typing.Callable`\\ [\\ [:py:class:`int`, :py:class:`str`], :py:obj:`None`]" ,
262
+ ":py:data:`~typing.Callable`\\ \\ [\\ [:py:class:`int`, :py:class:`str`], :py:obj:`None`]" ,
263
263
),
264
264
(
265
265
Callable [[T ], T ],
266
- ":py:data:`~typing.Callable`\\ [\\ [:py:class:`~typing.TypeVar`\\ (``T``)],"
266
+ ":py:data:`~typing.Callable`\\ \\ [\\ [:py:class:`~typing.TypeVar`\\ (``T``)],"
267
267
" :py:class:`~typing.TypeVar`\\ (``T``)]" ,
268
268
),
269
269
(
270
270
AbcCallable [[int , str ], bool ], # type: ignore[valid-type,misc,type-arg]
271
- ":py:class:`~collections.abc.Callable`\\ [\\ [:py:class:`int`, :py:class:`str`], :py:class:`bool`]" ,
271
+ ":py:class:`~collections.abc.Callable`\\ \\ [\\ [:py:class:`int`, :py:class:`str`], :py:class:`bool`]" ,
272
272
),
273
273
(Pattern , ":py:class:`~typing.Pattern`" ),
274
- (Pattern [str ], ":py:class:`~typing.Pattern`\\ [:py:class:`str`]" ),
274
+ (Pattern [str ], ":py:class:`~typing.Pattern`\\ \\ [:py:class:`str`]" ),
275
275
(IO , ":py:class:`~typing.IO`" ),
276
- (IO [str ], ":py:class:`~typing.IO`\\ [:py:class:`str`]" ),
276
+ (IO [str ], ":py:class:`~typing.IO`\\ \\ [:py:class:`str`]" ),
277
277
(Metaclass , ":py:class:`~%s.Metaclass`" % __name__ ),
278
278
(A , ":py:class:`~%s.A`" % __name__ ),
279
279
(B , ":py:class:`~%s.B`" % __name__ ),
280
- (B [int ], ":py:class:`~%s.B`\\ [:py:class:`int`]" % __name__ ),
280
+ (B [int ], ":py:class:`~%s.B`\\ \\ [:py:class:`int`]" % __name__ ),
281
281
(C , ":py:class:`~%s.C`" % __name__ ),
282
282
(D , ":py:class:`~%s.D`" % __name__ ),
283
283
(E , ":py:class:`~%s.E`" % __name__ ),
284
- (E [int ], ":py:class:`~%s.E`\\ [:py:class:`int`]" % __name__ ),
284
+ (E [int ], ":py:class:`~%s.E`\\ \\ [:py:class:`int`]" % __name__ ),
285
285
(W , f":py:{ 'class' if PY310_PLUS else 'func' } :`~typing.NewType`\\ (``W``, :py:class:`str`)" ),
286
286
(T , ":py:class:`~typing.TypeVar`\\ (``T``)" ),
287
287
(U_co , ":py:class:`~typing.TypeVar`\\ (``U_co``, covariant=True)" ),
@@ -306,17 +306,17 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
306
306
# Zero-length tuple remains
307
307
(Tuple [()], ":py:data:`~typing.Tuple`" ),
308
308
# Internal single tuple with simple types is flattened in the output
309
- (Tuple [(int ,)], ":py:data:`~typing.Tuple`\\ [:py:class:`int`]" ),
310
- (Tuple [(int , int )], ":py:data:`~typing.Tuple`\\ [:py:class:`int`, :py:class:`int`]" ),
309
+ (Tuple [(int ,)], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`int`]" ),
310
+ (Tuple [(int , int )], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`int`, :py:class:`int`]" ),
311
311
# Ellipsis in single tuple also gets flattened
312
- (Tuple [(int , ...)], ":py:data:`~typing.Tuple`\\ [:py:class:`int`, :py:data:`...<Ellipsis>`]" ),
312
+ (Tuple [(int , ...)], ":py:data:`~typing.Tuple`\\ \\ [:py:class:`int`, :py:data:`...<Ellipsis>`]" ),
313
313
(
314
314
RecList ,
315
- ":py:data:`~typing.Union`\\ [:py:class:`int`, :py:class:`~typing.List`\\ [RecList]]" ,
315
+ ":py:data:`~typing.Union`\\ \\ [:py:class:`int`, :py:class:`~typing.List`\\ \\ [RecList]]" ,
316
316
),
317
317
(
318
318
MutualRecA ,
319
- ":py:data:`~typing.Union`\\ [:py:class:`bool`, :py:class:`~typing.List`\\ [MutualRecB]]" ,
319
+ ":py:data:`~typing.Union`\\ \\ [:py:class:`bool`, :py:class:`~typing.List`\\ \\ [MutualRecB]]" ,
320
320
),
321
321
]
322
322
@@ -327,39 +327,39 @@ def test_parse_annotation(annotation: Any, module: str, class_name: str, args: t
327
327
(
328
328
nptyping .NDArray [nptyping .Shape ["*" ], nptyping .Float ],
329
329
(
330
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ [*], "
330
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ \\ [*], "
331
331
":py:class:`~numpy.float64`]"
332
332
),
333
333
),
334
334
(
335
335
nptyping .NDArray [nptyping .Shape ["64" ], nptyping .Float ],
336
336
(
337
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ [64],"
337
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ \\ [64],"
338
338
" :py:class:`~numpy.float64`]"
339
339
),
340
340
),
341
341
(
342
342
nptyping .NDArray [nptyping .Shape ["*, *" ], nptyping .Float ],
343
343
(
344
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ [*, "
344
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ \\ [*, "
345
345
"*], :py:class:`~numpy.float64`]"
346
346
),
347
347
),
348
348
(
349
349
nptyping .NDArray [nptyping .Shape ["*, ..." ], nptyping .Float ],
350
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:data:`~typing.Any`, :py:class:`~numpy.float64`]" ,
350
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:data:`~typing.Any`, :py:class:`~numpy.float64`]" ,
351
351
),
352
352
(
353
353
nptyping .NDArray [nptyping .Shape ["*, 3" ], nptyping .Float ],
354
354
(
355
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ [*, 3"
355
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ \\ [*, 3"
356
356
"], :py:class:`~numpy.float64`]"
357
357
),
358
358
),
359
359
(
360
360
nptyping .NDArray [nptyping .Shape ["3, ..." ], nptyping .Float ],
361
361
(
362
- ":py:class:`~nptyping.ndarray.NDArray`\\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ [3, "
362
+ ":py:class:`~nptyping.ndarray.NDArray`\\ \\ [:py:class:`~nptyping.base_meta_classes.Shape`\\ \\ [3, "
363
363
"...], :py:class:`~numpy.float64`]"
364
364
),
365
365
),
@@ -379,7 +379,7 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
379
379
# subsequent tests
380
380
expected_result_not_simplified = expected_result .replace (", ``None``" , "" )
381
381
# encapsulate Union in typing.Optional
382
- expected_result_not_simplified = ":py:data:`~typing.Optional`\\ [" + expected_result_not_simplified
382
+ expected_result_not_simplified = ":py:data:`~typing.Optional`\\ \\ [" + expected_result_not_simplified
383
383
expected_result_not_simplified += "]"
384
384
conf = create_autospec (Config , simplify_optional_unions = False , _annotation_globals = globals ())
385
385
assert format_annotation (annotation , conf ) == expected_result_not_simplified
@@ -421,11 +421,11 @@ def test_format_annotation(inv: Inventory, annotation: Any, expected_result: str
421
421
@pytest .mark .parametrize (
422
422
("annotation" , "params" , "expected_result" ),
423
423
[
424
- ("ClassVar" , int , ":py:data:`~typing.ClassVar`\\ [:py:class:`int`]" ),
424
+ ("ClassVar" , int , ":py:data:`~typing.ClassVar`\\ \\ [:py:class:`int`]" ),
425
425
("NoReturn" , None , ":py:data:`~typing.NoReturn`" ),
426
- ("Literal" , ("a" , 1 ), ":py:data:`~typing.Literal`\\ [``'a'``, ``1``]" ),
426
+ ("Literal" , ("a" , 1 ), ":py:data:`~typing.Literal`\\ \\ [``'a'``, ``1``]" ),
427
427
("Type" , None , ":py:class:`~typing.Type`" ),
428
- ("Type" , (A ,), f":py:class:`~typing.Type`\\ [:py:class:`~{ __name__ } .A`]" ),
428
+ ("Type" , (A ,), f":py:class:`~typing.Type`\\ \\ [:py:class:`~{ __name__ } .A`]" ),
429
429
],
430
430
)
431
431
def test_format_annotation_both_libs (library : ModuleType , annotation : str , params : Any , expected_result : str ) -> None :
@@ -524,16 +524,11 @@ class dummy_module.DataClass(x)
524
524
525
525
def maybe_fix_py310 (expected_contents : str ) -> str :
526
526
if not PY310_PLUS :
527
- return expected_contents
527
+ return expected_contents .replace ('"' , "" )
528
+
528
529
for old , new in [
529
- ("*bool** | **None*" , '"Optional"["bool"]' ),
530
- ("*int** | **str** | **float*" , '"int" | "str" | "float"' ),
531
- ("*str** | **None*" , '"Optional"["str"]' ),
532
- ("(*bool*)" , '("bool")' ),
533
- ("(*int*" , '("int"' ),
534
- (" str" , ' "str"' ),
535
- ('"Optional"["str"]' , '"Optional"["str"]' ),
536
- ('"Optional"["Callable"[["int", "bytes"], "int"]]' , '"Optional"["Callable"[["int", "bytes"], "int"]]' ),
530
+ ("bool | None" , '"Optional"["bool"]' ),
531
+ ("str | None" , '"Optional"["str"]' ),
537
532
]:
538
533
expected_contents = expected_contents .replace (old , new )
539
534
return expected_contents
@@ -559,14 +554,14 @@ def test_sphinx_output_future_annotations(app: SphinxTestApp, status: StringIO)
559
554
Method docstring.
560
555
561
556
Parameters:
562
- * **x** (* bool** | ** None* ) -- foo
557
+ * **x** (bool | None) -- foo
563
558
564
- * **y** (* int** | ** str** | ** float* ) -- bar
559
+ * **y** (" int" | " str" | " float" ) -- bar
565
560
566
- * **z** (* str** | ** None* ) -- baz
561
+ * **z** (str | None) -- baz
567
562
568
563
Return type:
569
- str
564
+ " str"
570
565
"""
571
566
expected_contents = maybe_fix_py310 (dedent (expected_contents ))
572
567
assert contents == expected_contents
@@ -625,7 +620,7 @@ def test_sphinx_output_defaults(
625
620
("formatter_config_val" , "expected" ),
626
621
[
627
622
(None , ['("bool") -- foo' , '("int") -- bar' , '"str"' ]),
628
- (lambda ann , conf : "Test" , ["(* Test* ) -- foo" , "(* Test* ) -- bar" , "Test" ]), # noqa: ARG005
623
+ (lambda ann , conf : "Test" , ["(Test) -- foo" , "(Test) -- bar" , "Test" ]), # noqa: ARG005
629
624
("some string" , Exception ("needs to be callable or `None`" )),
630
625
],
631
626
)
0 commit comments