-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathtest_merger.py
More file actions
97 lines (81 loc) · 3.11 KB
/
Copy pathtest_merger.py
File metadata and controls
97 lines (81 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Tests for the `merger` module."""
from __future__ import annotations
from griffe import temporary_visited_package
def test_dont_trigger_alias_resolution_when_merging_stubs() -> None:
"""Assert that we don't trigger alias resolution when merging stubs."""
with temporary_visited_package(
"package",
{
"mod.py": "import pathlib\n\ndef f() -> pathlib.Path:\n return pathlib.Path()",
"mod.pyi": "import pathlib\n\ndef f() -> pathlib.Path: ...",
},
) as pkg:
assert not pkg["mod.pathlib"].resolved
def test_merge_stubs_on_wildcard_imported_objects() -> None:
"""Assert that stubs can be merged on wildcard imported objects."""
with temporary_visited_package(
"package",
{
"mod.py": "class A:\n def hello(value: int | str) -> int | str:\n return value",
"__init__.py": "from .mod import *",
"__init__.pyi": """
from typing import overload
class A:
@overload
def hello(value: int) -> int: ...
@overload
def hello(value: str) -> str: ...
""",
},
) as pkg:
assert pkg["A.hello"].overloads
def test_merge_imports() -> None:
"""Assert that imports are merged correctly."""
with temporary_visited_package(
"package",
{
"mod.py": "import abc",
"mod.pyi": "import collections",
},
) as pkg:
assert set(pkg["mod"].imports) == {"abc", "collections"}
def test_override_exports() -> None:
"""Assert that exports are overridden too (like imports are merged)."""
with temporary_visited_package(
"package",
{
"__init__.py": "import dynamic_all\n__all__ = dynamic_all()",
"__init__.pyi": "from ._hello import hello\n__all__ = ['hello']",
"_hello.py": "def hello() -> None:\n '''Say hello.'''",
},
) as pkg:
assert pkg.exports == ["hello"]
def test_merge_attribute_values() -> None:
"""Assert that attribute values are merged correctly."""
with temporary_visited_package(
"package",
{
"__init__.py": "import dynamic_all\n__all__ = dynamic_all()",
"__init__.pyi": "from ._hello import hello\n__all__ = ['hello']",
"_hello.py": "def hello() -> None:\n '''Say hello.'''",
},
) as pkg:
assert str(pkg["__all__"].value) == "['hello']"
def test_merge_overload_annotations() -> None:
"""Assert that overload annotations are merged correctly."""
with temporary_visited_package(
"package",
{
"mod.py": "def func(x): ...",
"mod.pyi": """
from typing import overload
@overload
def func(x: int) -> int: ...
@overload
def func(x: float) -> float: ...
""",
},
) as pkg:
func = pkg["mod.func"]
assert str(func.parameters["x"].annotation) == "int | float"
assert str(func.returns) == "int | float"