File: test_merger.py

package info (click to toggle)
python-griffe 1.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,092 kB
  • sloc: python: 14,305; javascript: 84; makefile: 41; sh: 23
file content (75 lines) | stat: -rw-r--r-- 2,545 bytes parent folder | download
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
"""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']"