|
1 | 1 | """Tests util.typing functions.""" |
2 | 2 |
|
3 | 3 | import sys |
| 4 | +import typing as t |
| 5 | +from collections import abc |
4 | 6 | from contextvars import Context, ContextVar, Token |
5 | 7 | from enum import Enum |
6 | 8 | from numbers import Integral |
|
29 | 31 | ) |
30 | 32 | from typing import ( |
31 | 33 | Any, |
32 | | - Callable, |
33 | 34 | Dict, |
34 | | - Generator, |
35 | | - Iterator, |
36 | 35 | List, |
37 | 36 | NewType, |
38 | 37 | Optional, |
@@ -173,20 +172,29 @@ def test_restify_type_hints_containers(): |
173 | 172 | assert restify(MyList[Tuple[int, int]]) == (":py:class:`tests.test_util.test_util_typing.MyList`\\ " |
174 | 173 | "[:py:class:`~typing.Tuple`\\ " |
175 | 174 | "[:py:class:`int`, :py:class:`int`]]") |
176 | | - assert restify(Generator[None, None, None]) == (":py:class:`~typing.Generator`\\ " |
177 | | - "[:py:obj:`None`, :py:obj:`None`, " |
178 | | - ":py:obj:`None`]") |
179 | | - assert restify(Iterator[None]) == (":py:class:`~typing.Iterator`\\ " |
180 | | - "[:py:obj:`None`]") |
| 175 | + assert restify(t.Generator[None, None, None]) == (":py:class:`~typing.Generator`\\ " |
| 176 | + "[:py:obj:`None`, :py:obj:`None`, " |
| 177 | + ":py:obj:`None`]") |
| 178 | + assert restify(abc.Generator[None, None, None]) == (":py:class:`collections.abc.Generator`\\ " |
| 179 | + "[:py:obj:`None`, :py:obj:`None`, " |
| 180 | + ":py:obj:`None`]") |
| 181 | + assert restify(t.Iterator[None]) == (":py:class:`~typing.Iterator`\\ " |
| 182 | + "[:py:obj:`None`]") |
| 183 | + assert restify(abc.Iterator[None]) == (":py:class:`collections.abc.Iterator`\\ " |
| 184 | + "[:py:obj:`None`]") |
181 | 185 |
|
182 | 186 |
|
183 | 187 | def test_restify_type_hints_Callable(): |
184 | | - assert restify(Callable) == ":py:class:`~typing.Callable`" |
185 | | - |
186 | | - assert restify(Callable[[str], int]) == (":py:class:`~typing.Callable`\\ " |
187 | | - "[[:py:class:`str`], :py:class:`int`]") |
188 | | - assert restify(Callable[..., int]) == (":py:class:`~typing.Callable`\\ " |
189 | | - "[[...], :py:class:`int`]") |
| 188 | + assert restify(t.Callable) == ":py:class:`~typing.Callable`" |
| 189 | + assert restify(t.Callable[[str], int]) == (":py:class:`~typing.Callable`\\ " |
| 190 | + "[[:py:class:`str`], :py:class:`int`]") |
| 191 | + assert restify(t.Callable[..., int]) == (":py:class:`~typing.Callable`\\ " |
| 192 | + "[[...], :py:class:`int`]") |
| 193 | + assert restify(abc.Callable) == ":py:class:`collections.abc.Callable`" |
| 194 | + assert restify(abc.Callable[[str], int]) == (":py:class:`collections.abc.Callable`\\ " |
| 195 | + "[[:py:class:`str`], :py:class:`int`]") |
| 196 | + assert restify(abc.Callable[..., int]) == (":py:class:`collections.abc.Callable`\\ " |
| 197 | + "[[...], :py:class:`int`]") |
190 | 198 |
|
191 | 199 |
|
192 | 200 | def test_restify_type_hints_Union(): |
@@ -409,13 +417,21 @@ def test_stringify_type_hints_containers(): |
409 | 417 | assert stringify_annotation(MyList[Tuple[int, int]], "fully-qualified") == "tests.test_util.test_util_typing.MyList[typing.Tuple[int, int]]" |
410 | 418 | assert stringify_annotation(MyList[Tuple[int, int]], "smart") == "~tests.test_util.test_util_typing.MyList[~typing.Tuple[int, int]]" |
411 | 419 |
|
412 | | - assert stringify_annotation(Generator[None, None, None], 'fully-qualified-except-typing') == "Generator[None, None, None]" |
413 | | - assert stringify_annotation(Generator[None, None, None], "fully-qualified") == "typing.Generator[None, None, None]" |
414 | | - assert stringify_annotation(Generator[None, None, None], "smart") == "~typing.Generator[None, None, None]" |
| 420 | + assert stringify_annotation(t.Generator[None, None, None], 'fully-qualified-except-typing') == "Generator[None, None, None]" |
| 421 | + assert stringify_annotation(t.Generator[None, None, None], "fully-qualified") == "typing.Generator[None, None, None]" |
| 422 | + assert stringify_annotation(t.Generator[None, None, None], "smart") == "~typing.Generator[None, None, None]" |
| 423 | + |
| 424 | + assert stringify_annotation(abc.Generator[None, None, None], 'fully-qualified-except-typing') == "collections.abc.Generator[None, None, None]" |
| 425 | + assert stringify_annotation(abc.Generator[None, None, None], "fully-qualified") == "collections.abc.Generator[None, None, None]" |
| 426 | + assert stringify_annotation(abc.Generator[None, None, None], "smart") == "~collections.abc.Generator[None, None, None]" |
| 427 | + |
| 428 | + assert stringify_annotation(t.Iterator[None], 'fully-qualified-except-typing') == "Iterator[None]" |
| 429 | + assert stringify_annotation(t.Iterator[None], "fully-qualified") == "typing.Iterator[None]" |
| 430 | + assert stringify_annotation(t.Iterator[None], "smart") == "~typing.Iterator[None]" |
415 | 431 |
|
416 | | - assert stringify_annotation(Iterator[None], 'fully-qualified-except-typing') == "Iterator[None]" |
417 | | - assert stringify_annotation(Iterator[None], "fully-qualified") == "typing.Iterator[None]" |
418 | | - assert stringify_annotation(Iterator[None], "smart") == "~typing.Iterator[None]" |
| 432 | + assert stringify_annotation(abc.Iterator[None], 'fully-qualified-except-typing') == "collections.abc.Iterator[None]" |
| 433 | + assert stringify_annotation(abc.Iterator[None], "fully-qualified") == "collections.abc.Iterator[None]" |
| 434 | + assert stringify_annotation(abc.Iterator[None], "smart") == "~collections.abc.Iterator[None]" |
419 | 435 |
|
420 | 436 |
|
421 | 437 | def test_stringify_type_hints_pep_585(): |
@@ -489,17 +505,29 @@ def test_stringify_type_hints_string(): |
489 | 505 |
|
490 | 506 |
|
491 | 507 | def test_stringify_type_hints_Callable(): |
492 | | - assert stringify_annotation(Callable, 'fully-qualified-except-typing') == "Callable" |
493 | | - assert stringify_annotation(Callable, "fully-qualified") == "typing.Callable" |
494 | | - assert stringify_annotation(Callable, "smart") == "~typing.Callable" |
| 508 | + assert stringify_annotation(t.Callable, 'fully-qualified-except-typing') == "Callable" |
| 509 | + assert stringify_annotation(t.Callable, "fully-qualified") == "typing.Callable" |
| 510 | + assert stringify_annotation(t.Callable, "smart") == "~typing.Callable" |
| 511 | + |
| 512 | + assert stringify_annotation(t.Callable[[str], int], 'fully-qualified-except-typing') == "Callable[[str], int]" |
| 513 | + assert stringify_annotation(t.Callable[[str], int], "fully-qualified") == "typing.Callable[[str], int]" |
| 514 | + assert stringify_annotation(t.Callable[[str], int], "smart") == "~typing.Callable[[str], int]" |
| 515 | + |
| 516 | + assert stringify_annotation(t.Callable[..., int], 'fully-qualified-except-typing') == "Callable[[...], int]" |
| 517 | + assert stringify_annotation(t.Callable[..., int], "fully-qualified") == "typing.Callable[[...], int]" |
| 518 | + assert stringify_annotation(t.Callable[..., int], "smart") == "~typing.Callable[[...], int]" |
| 519 | + |
| 520 | + assert stringify_annotation(abc.Callable, 'fully-qualified-except-typing') == "collections.abc.Callable" |
| 521 | + assert stringify_annotation(abc.Callable, "fully-qualified") == "collections.abc.Callable" |
| 522 | + assert stringify_annotation(abc.Callable, "smart") == "~collections.abc.Callable" |
495 | 523 |
|
496 | | - assert stringify_annotation(Callable[[str], int], 'fully-qualified-except-typing') == "Callable[[str], int]" |
497 | | - assert stringify_annotation(Callable[[str], int], "fully-qualified") == "typing.Callable[[str], int]" |
498 | | - assert stringify_annotation(Callable[[str], int], "smart") == "~typing.Callable[[str], int]" |
| 524 | + assert stringify_annotation(abc.Callable[[str], int], 'fully-qualified-except-typing') == "collections.abc.Callable[[str], int]" |
| 525 | + assert stringify_annotation(abc.Callable[[str], int], "fully-qualified") == "collections.abc.Callable[[str], int]" |
| 526 | + assert stringify_annotation(abc.Callable[[str], int], "smart") == "~collections.abc.Callable[[str], int]" |
499 | 527 |
|
500 | | - assert stringify_annotation(Callable[..., int], 'fully-qualified-except-typing') == "Callable[[...], int]" |
501 | | - assert stringify_annotation(Callable[..., int], "fully-qualified") == "typing.Callable[[...], int]" |
502 | | - assert stringify_annotation(Callable[..., int], "smart") == "~typing.Callable[[...], int]" |
| 528 | + assert stringify_annotation(abc.Callable[..., int], 'fully-qualified-except-typing') == "collections.abc.Callable[[...], int]" |
| 529 | + assert stringify_annotation(abc.Callable[..., int], "fully-qualified") == "collections.abc.Callable[[...], int]" |
| 530 | + assert stringify_annotation(abc.Callable[..., int], "smart") == "~collections.abc.Callable[[...], int]" |
503 | 531 |
|
504 | 532 |
|
505 | 533 | def test_stringify_type_hints_Union(): |
|
0 commit comments