@@ -153,6 +153,9 @@ def test_restify_type_hints_typevars():
153
153
assert restify (List [T ]) == ":py:class:`~typing.List`\\ [:py:obj:`tests.test_util_typing.T`]"
154
154
assert restify (List [T ], "smart" ) == ":py:class:`~typing.List`\\ [:py:obj:`~tests.test_util_typing.T`]"
155
155
156
+ assert restify (list [T ]) == ":py:class:`list`\\ [:py:obj:`tests.test_util_typing.T`]"
157
+ assert restify (list [T ], "smart" ) == ":py:class:`list`\\ [:py:obj:`~tests.test_util_typing.T`]"
158
+
156
159
if sys .version_info [:2 ] >= (3 , 10 ):
157
160
assert restify (MyInt ) == ":py:class:`tests.test_util_typing.MyInt`"
158
161
assert restify (MyInt , "smart" ) == ":py:class:`~tests.test_util_typing.MyInt`"
@@ -171,14 +174,20 @@ def test_restify_type_hints_custom_class():
171
174
172
175
def test_restify_type_hints_alias ():
173
176
MyStr = str
174
- MyTuple = Tuple [str , str ]
177
+ MyTypingTuple = Tuple [str , str ]
178
+ MyTuple = tuple [str , str ]
175
179
assert restify (MyStr ) == ":py:class:`str`"
176
- assert restify (MyTuple ) == ":py:class:`~typing.Tuple`\\ [:py:class:`str`, :py:class:`str`]"
180
+ assert restify (MyTypingTuple ) == ":py:class:`~typing.Tuple`\\ [:py:class:`str`, :py:class:`str`]"
181
+ assert restify (MyTuple ) == ":py:class:`tuple`\\ [:py:class:`str`, :py:class:`str`]"
177
182
178
183
179
184
def test_restify_type_ForwardRef ():
180
185
from typing import ForwardRef # type: ignore[attr-defined]
181
- assert restify (ForwardRef ("myint" )) == ":py:class:`myint`"
186
+ assert restify (ForwardRef ("MyInt" )) == ":py:class:`MyInt`"
187
+
188
+ assert restify (list [ForwardRef ("MyInt" )]) == ":py:class:`list`\\ [:py:class:`MyInt`]"
189
+
190
+ assert restify (Tuple [dict [ForwardRef ("MyInt" ), str ], list [List [int ]]]) == ":py:class:`~typing.Tuple`\\ [:py:class:`dict`\\ [:py:class:`MyInt`, :py:class:`str`], :py:class:`list`\\ [:py:class:`~typing.List`\\ [:py:class:`int`]]]" # type: ignore[attr-defined]
182
191
183
192
184
193
def test_restify_type_Literal ():
@@ -190,10 +199,26 @@ def test_restify_pep_585():
190
199
assert restify (list [str ]) == ":py:class:`list`\\ [:py:class:`str`]" # type: ignore[attr-defined]
191
200
assert restify (dict [str , str ]) == (":py:class:`dict`\\ " # type: ignore[attr-defined]
192
201
"[:py:class:`str`, :py:class:`str`]" )
202
+ assert restify (tuple [str , ...]) == ":py:class:`tuple`\\ [:py:class:`str`, ...]"
203
+ assert restify (tuple [str , str , str ]) == (":py:class:`tuple`\\ "
204
+ "[:py:class:`str`, :py:class:`str`, "
205
+ ":py:class:`str`]" )
193
206
assert restify (dict [str , tuple [int , ...]]) == (":py:class:`dict`\\ " # type: ignore[attr-defined]
194
207
"[:py:class:`str`, :py:class:`tuple`\\ "
195
208
"[:py:class:`int`, ...]]" )
196
209
210
+ assert restify (tuple [()]) == ":py:class:`tuple`\\ [()]"
211
+
212
+ # Mix old typing with PEP 585
213
+ assert restify (List [dict [str , Tuple [str , ...]]]) == (":py:class:`~typing.List`\\ "
214
+ "[:py:class:`dict`\\ "
215
+ "[:py:class:`str`, :py:class:`~typing.Tuple`\\ "
216
+ "[:py:class:`str`, ...]]]" )
217
+ assert restify (tuple [MyList [list [int ]], int ]) == (":py:class:`tuple`\\ ["
218
+ ":py:class:`tests.test_util_typing.MyList`\\ "
219
+ "[:py:class:`list`\\ [:py:class:`int`]], "
220
+ ":py:class:`int`]" )
221
+
197
222
198
223
@pytest .mark .skipif (sys .version_info [:2 ] <= (3 , 9 ), reason = 'python 3.10+ is required.' )
199
224
def test_restify_type_union_operator ():
@@ -313,9 +338,17 @@ def test_stringify_type_hints_pep_585():
313
338
assert stringify_annotation (list [dict [str , tuple ]], 'fully-qualified-except-typing' ) == "list[dict[str, tuple]]"
314
339
assert stringify_annotation (list [dict [str , tuple ]], "smart" ) == "list[dict[str, tuple]]"
315
340
341
+ assert stringify_annotation (MyList [tuple [int , int ]], 'fully-qualified-except-typing' ) == "tests.test_util_typing.MyList[tuple[int, int]]"
342
+ assert stringify_annotation (MyList [tuple [int , int ]], "fully-qualified" ) == "tests.test_util_typing.MyList[tuple[int, int]]"
343
+ assert stringify_annotation (MyList [tuple [int , int ]], "smart" ) == "~tests.test_util_typing.MyList[tuple[int, int]]"
344
+
316
345
assert stringify_annotation (type [int ], 'fully-qualified-except-typing' ) == "type[int]"
317
346
assert stringify_annotation (type [int ], "smart" ) == "type[int]"
318
347
348
+ # Mix typing and pep 585
349
+ assert stringify_annotation (tuple [List [dict [int , str ]], str , ...], 'fully-qualified-except-typing' ) == "tuple[List[dict[int, str]], str, ...]"
350
+ assert stringify_annotation (tuple [List [dict [int , str ]], str , ...], "smart" ) == "tuple[~typing.List[dict[int, str]], str, ...]"
351
+
319
352
320
353
def test_stringify_Annotated ():
321
354
from typing import Annotated # type: ignore[attr-defined]
@@ -336,10 +369,18 @@ def test_stringify_type_hints_string():
336
369
assert stringify_annotation (List ["int" ], 'fully-qualified' ) == "typing.List[int]"
337
370
assert stringify_annotation (List ["int" ], "smart" ) == "~typing.List[int]"
338
371
372
+ assert stringify_annotation (list ["int" ], 'fully-qualified-except-typing' ) == "list[int]"
373
+ assert stringify_annotation (list ["int" ], 'fully-qualified' ) == "list[int]"
374
+ assert stringify_annotation (list ["int" ], "smart" ) == "list[int]"
375
+
339
376
assert stringify_annotation ("Tuple[str]" , 'fully-qualified-except-typing' ) == "Tuple[str]"
340
377
assert stringify_annotation ("Tuple[str]" , 'fully-qualified' ) == "Tuple[str]"
341
378
assert stringify_annotation ("Tuple[str]" , "smart" ) == "Tuple[str]"
342
379
380
+ assert stringify_annotation ("tuple[str]" , 'fully-qualified-except-typing' ) == "tuple[str]"
381
+ assert stringify_annotation ("tuple[str]" , 'fully-qualified' ) == "tuple[str]"
382
+ assert stringify_annotation ("tuple[str]" , "smart" ) == "tuple[str]"
383
+
343
384
assert stringify_annotation ("unknown" , 'fully-qualified-except-typing' ) == "unknown"
344
385
assert stringify_annotation ("unknown" , 'fully-qualified' ) == "unknown"
345
386
assert stringify_annotation ("unknown" , "smart" ) == "unknown"
@@ -401,6 +442,9 @@ def test_stringify_type_hints_typevars():
401
442
assert stringify_annotation (List [T ], 'fully-qualified-except-typing' ) == "List[tests.test_util_typing.T]"
402
443
assert stringify_annotation (List [T ], "smart" ) == "~typing.List[~tests.test_util_typing.T]"
403
444
445
+ assert stringify_annotation (list [T ], 'fully-qualified-except-typing' ) == "list[tests.test_util_typing.T]"
446
+ assert stringify_annotation (list [T ], "smart" ) == "list[~tests.test_util_typing.T]"
447
+
404
448
if sys .version_info [:2 ] >= (3 , 10 ):
405
449
assert stringify_annotation (MyInt , 'fully-qualified-except-typing' ) == "tests.test_util_typing.MyInt"
406
450
assert stringify_annotation (MyInt , "smart" ) == "~tests.test_util_typing.MyInt"
@@ -446,6 +490,9 @@ def test_stringify_type_union_operator():
446
490
assert stringify_annotation (int | str | None ) == "int | str | None" # type: ignore[attr-defined]
447
491
assert stringify_annotation (int | str | None , "smart" ) == "int | str | None" # type: ignore[attr-defined]
448
492
493
+ assert stringify_annotation (int | tuple [dict [str , int | None ], list [int | str ]] | None ) == "int | tuple[dict[str, int | None], list[int | str]] | None" # type: ignore[attr-defined]
494
+ assert stringify_annotation (int | tuple [dict [str , int | None ], list [int | str ]] | None , "smart" ) == "int | tuple[dict[str, int | None], list[int | str]] | None" # type: ignore[attr-defined]
495
+
449
496
assert stringify_annotation (int | Struct ) == "int | struct.Struct" # type: ignore[attr-defined]
450
497
assert stringify_annotation (int | Struct , "smart" ) == "int | ~struct.Struct" # type: ignore[attr-defined]
451
498
@@ -461,3 +508,17 @@ def test_stringify_mock():
461
508
assert stringify_annotation (unknown , 'fully-qualified-except-typing' ) == 'unknown'
462
509
assert stringify_annotation (unknown .secret .Class , 'fully-qualified-except-typing' ) == 'unknown.secret.Class'
463
510
assert stringify_annotation (unknown .secret .Class , "smart" ) == 'unknown.secret.Class'
511
+
512
+
513
+ def test_stringify_type_ForwardRef ():
514
+ from typing import ForwardRef # type: ignore[attr-defined]
515
+
516
+ assert stringify_annotation (ForwardRef ("MyInt" )) == "MyInt"
517
+ assert stringify_annotation (ForwardRef ("MyInt" ), 'smart' ) == "MyInt"
518
+
519
+ assert stringify_annotation (list [ForwardRef ("MyInt" )]) == "list[MyInt]"
520
+ assert stringify_annotation (list [ForwardRef ("MyInt" )], 'smart' ) == "list[MyInt]"
521
+
522
+ assert stringify_annotation (Tuple [dict [ForwardRef ("MyInt" ), str ], list [List [int ]]]) == "Tuple[dict[MyInt, str], list[List[int]]]" # type: ignore[attr-defined]
523
+ assert stringify_annotation (Tuple [dict [ForwardRef ("MyInt" ), str ], list [List [int ]]], 'fully-qualified-except-typing' ) == "Tuple[dict[MyInt, str], list[List[int]]]" # type: ignore[attr-defined]
524
+ assert stringify_annotation (Tuple [dict [ForwardRef ("MyInt" ), str ], list [List [int ]]], 'smart' ) == "~typing.Tuple[dict[MyInt, str], list[~typing.List[int]]]" # type: ignore[attr-defined]
0 commit comments