|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import sys |
| 6 | + |
5 | 7 | import pytest |
6 | 8 |
|
7 | 9 | from griffe import inspect, temporary_inspected_module, temporary_inspected_package, temporary_pypackage |
@@ -30,6 +32,29 @@ def test_annotations_from_classes() -> None: |
30 | 32 | assert returns.canonical_path == f"{module.name}.A" |
31 | 33 |
|
32 | 34 |
|
| 35 | +# YORE: EOL 3.9: Remove line. |
| 36 | +@pytest.mark.skipif(sys.version_info < (3, 10), reason="Type unions not supported on 3.9") |
| 37 | +@pytest.mark.parametrize( |
| 38 | + ("annotation", "expected"), |
| 39 | + [ |
| 40 | + ("tuple[int, str]", "tuple[int, str]"), |
| 41 | + ("Union[int, str]", "typing.Union[int, str]"), |
| 42 | + ("int | str", "int | str"), |
| 43 | + ("int | Literal[1]", "typing.Union[int, typing.Literal[1]]"), |
| 44 | + ], |
| 45 | +) |
| 46 | +def test_annotations_from_types(annotation: str, expected: str) -> None: |
| 47 | + """Assert annotations are correctly converted to string.""" |
| 48 | + with temporary_inspected_module( |
| 49 | + f""" |
| 50 | + from typing import Literal, Union |
| 51 | + def func(param: {annotation}): ... |
| 52 | + """, |
| 53 | + ) as module: |
| 54 | + param = module["func"].parameters["param"] |
| 55 | + assert str(param.annotation) == expected |
| 56 | + |
| 57 | + |
33 | 58 | def test_class_level_imports() -> None: |
34 | 59 | """Assert annotations using class-level imports are resolved.""" |
35 | 60 | with temporary_inspected_module( |
|
0 commit comments