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