File: test_visitor.py

package info (click to toggle)
python-griffe 1.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,292 kB
  • sloc: python: 17,202; makefile: 47; sh: 24; javascript: 13
file content (499 lines) | stat: -rw-r--r-- 18,070 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
"""Test visit mechanisms."""

from __future__ import annotations

import sys
from textwrap import dedent

import pytest

from griffe import (
    Expr,
    GriffeLoader,
    TypeParameterKind,
    temporary_pypackage,
    temporary_visited_module,
    temporary_visited_package,
)


def test_not_defined_at_runtime() -> None:
    """Assert that objects not defined at runtime are not added to wildcards expansions."""
    with temporary_pypackage("package", ["module_a.py", "module_b.py", "module_c.py"]) as tmp_package:
        tmp_package.path.joinpath("__init__.py").write_text("from package.module_a import *", encoding="utf8")
        tmp_package.path.joinpath("module_a.py").write_text(
            dedent(
                """
                import typing
                from typing import TYPE_CHECKING

                from package.module_b import CONST_B
                from package.module_c import CONST_C

                if typing.TYPE_CHECKING:  # Always false.
                    from package.module_b import TYPE_B
                if TYPE_CHECKING:  # Always false.
                    from package.module_c import TYPE_C
                """,
            ),
            encoding="utf8",
        )
        tmp_package.path.joinpath("module_b.py").write_text("CONST_B = 'hi'\nTYPE_B = str", encoding="utf8")
        tmp_package.path.joinpath("module_c.py").write_text("CONST_C = 'ho'\nTYPE_C = str", encoding="utf8")
        loader = GriffeLoader(search_paths=[tmp_package.tmpdir])
        package = loader.load(tmp_package.name)
        loader.resolve_aliases()
        assert "CONST_B" in package.members
        assert "CONST_C" in package.members
        assert "TYPE_B" not in package.members
        assert "TYPE_C" not in package.members


@pytest.mark.parametrize(
    ("decorator", "labels"),
    [
        ("property", ("property",)),
        ("staticmethod", ("staticmethod",)),
        ("classmethod", ("classmethod",)),
        ("functools.cache", ("cached",)),
        ("cache", ("cached",)),
        ("functools.cached_property", ("cached", "property")),
        ("cached_property", ("cached", "property")),
        ("functools.lru_cache", ("cached",)),
        ("functools.lru_cache(maxsize=8)", ("cached",)),
        ("lru_cache", ("cached",)),
        ("lru_cache(maxsize=8)", ("cached",)),
        ("abc.abstractmethod", ("abstractmethod",)),
        ("abstractmethod", ("abstractmethod",)),
        ("dataclasses.dataclass", ("dataclass",)),
        ("dataclass", ("dataclass",)),
    ],
)
def test_set_function_labels_using_decorators(decorator: str, labels: tuple[str, ...]) -> None:
    """Assert decorators are used to set labels on functions.

    Parameters:
        decorator: A parametrized decorator.
        labels: The parametrized tuple of expected labels.
    """
    code = f"""
        import abc
        import dataclasses
        import functools
        from abc import abstractmethod
        from dataclasses import dataclass
        from functools import cache, cached_property, lru_cache

        class A:
            @{decorator}
            def f(self):
                return 0
    """
    with temporary_visited_module(code) as module:
        assert module["A.f"].has_labels(*labels)


@pytest.mark.parametrize(
    ("decorator", "labels"),
    [
        ("dataclasses.dataclass", ("dataclass",)),
        ("dataclass", ("dataclass",)),
    ],
)
def test_set_class_labels_using_decorators(decorator: str, labels: tuple[str, ...]) -> None:
    """Assert decorators are used to set labels on classes.

    Parameters:
        decorator: A parametrized decorator.
        labels: The parametrized tuple of expected labels.
    """
    code = f"""
        import dataclasses
        from dataclasses import dataclass

        @{decorator}
        class A: ...
    """
    with temporary_visited_module(code) as module:
        assert module["A"].has_labels(*labels)


def test_handle_property_setter_and_deleter() -> None:
    """Assert property setters and deleters are supported."""
    code = """
        class A:
            def __init__(self): self._thing = 0

            @property
            def thing(self): return self._thing

            @thing.setter
            def thing(self, value): self._thing = value

            @thing.deleter
            def thing(self): del self._thing
    """
    with temporary_visited_module(code) as module:
        assert module["A.thing"].has_labels("property", "writable", "deletable")
        assert module["A.thing"].setter.is_function
        assert module["A.thing"].deleter.is_function


@pytest.mark.parametrize(
    "decorator",
    [
        "overload",
        "typing.overload",
    ],
)
def test_handle_typing_overaload(decorator: str) -> None:
    """Assert `typing.overload` is supported.

    Parameters:
        decorator: A parametrized overload decorator.
    """
    code = f"""
        import typing
        from typing import overload
        from pathlib import Path

        class A:
            @{decorator}
            def absolute(self, path: str) -> str:
                ...

            @{decorator}
            def absolute(self, path: Path) -> Path:
                ...

            def absolute(self, path: str | Path) -> str | Path:
                ...
    """
    with temporary_visited_module(code) as module:
        overloads = module["A.absolute"].overloads
        assert len(overloads) == 2
        assert overloads[0].parameters["path"].annotation.name == "str"
        assert overloads[1].parameters["path"].annotation.name == "Path"
        assert overloads[0].returns.name == "str"
        assert overloads[1].returns.name == "Path"


@pytest.mark.parametrize(
    "statements",
    [
        """__all__ = ["CONST_INIT"] + moda_all + modb_all + modc_all""",
        """__all__ = ["CONST_INIT", *moda_all, *modb_all, *modc_all]""",
        """
        __all__ = ["CONST_INIT"]
        __all__ += moda_all + modb_all + modc_all
        """,
        """
        __all__ = ["CONST_INIT"] + moda_all + modb_all
        __all__ += modc_all
        """,
        """
        __all__ = ["CONST_INIT"] + moda_all + modb_all
        __all__ += [*modc_all]
        """,
        """
        __all__ = ["CONST_INIT"]
        __all__ += moda_all
        __all__ += modb_all + modc_all
        """,
    ],
)
def test_parse_complex__all__assignments(statements: str) -> None:
    """Check our ability to expand exports based on `__all__` [augmented] assignments.

    Parameters:
        statements: Parametrized text containing `__all__` [augmented] assignments.
    """
    with temporary_pypackage("package", ["moda.py", "modb.py", "modc.py"]) as tmp_package:
        tmp_package.path.joinpath("moda.py").write_text("CONST_A = 1\n\n__all__ = ['CONST_A']", encoding="utf8")
        tmp_package.path.joinpath("modb.py").write_text("CONST_B = 1\n\n__all__ = ['CONST_B']", encoding="utf8")
        tmp_package.path.joinpath("modc.py").write_text("CONST_C = 2\n\n__all__ = ['CONST_C']", encoding="utf8")
        code = """
            from package.moda import *
            from package.moda import __all__ as moda_all
            from package.modb import *
            from package.modb import __all__ as modb_all
            from package.modc import *
            from package.modc import __all__ as modc_all

            CONST_INIT = 0
        """
        tmp_package.path.joinpath("__init__.py").write_text(dedent(code) + dedent(statements), encoding="utf8")

        loader = GriffeLoader(search_paths=[tmp_package.tmpdir])
        package = loader.load(tmp_package.name)
        loader.resolve_aliases()

        assert package.exports == ["CONST_INIT", "CONST_A", "CONST_B", "CONST_C"]


def test_dont_crash_on_nested_functions_in_init() -> None:
    """Assert we don't crash when visiting a nested function in `__init__` methods."""
    with temporary_visited_module(
        """
        class C:
            def __init__(self):
                def pl(i: int):
                    return i + 1
        """,
    ) as module:
        assert module


def test_get_correct_docstring_starting_line_number() -> None:
    """Assert we get the correct line numbers for docstring."""
    with temporary_visited_module(
        """
        '''
        Module docstring.
        '''
        class C:
            '''
            Class docstring.
            '''
            def method(self):
                '''
                Method docstring.
                '''
        """,
    ) as module:
        assert module.docstring.lineno == 2  # type: ignore[union-attr]
        assert module["C"].docstring.lineno == 6
        assert module["C.method"].docstring.lineno == 10


def test_visit_properties_as_attributes() -> None:
    """Assert properties are created as attributes and not functions."""
    with temporary_visited_module(
        """
        from functools import cached_property

        class C:
            @property
            def prop(self) -> bool:
                return True
            @cached_property
            def cached_prop(self) -> int:
                return 0
        """,
    ) as module:
        assert module["C.prop"].is_attribute
        assert "property" in module["C.prop"].labels
        assert module["C.cached_prop"].is_attribute
        assert "cached" in module["C.cached_prop"].labels


def test_forward_docstrings() -> None:
    """Assert docstrings of class attributes are forwarded to instance assignments.

    This is a regression test for https://github.com/mkdocstrings/griffe/issues/128.
    """
    with temporary_visited_module(
        '''
        class C:
            attr: int
            """This is a non-empty docstring."""

            def __init__(self, attr: int) -> None:
                self.attr = attr
        ''',
    ) as module:
        assert module["C.attr"].docstring


def test_classvar_annotations() -> None:
    """Assert class variable and instance variable annotations are correctly parsed and merged."""
    with temporary_visited_module(
        """
        from typing import ClassVar

        class C:
            w: ClassVar[str] = "foo"
            x: ClassVar[int]
            y: str
            z: int = 5

            def __init__(self) -> None:
                self.a: ClassVar[float]
                self.y = ""
                self.b: bytes
        """,
    ) as module:
        assert module["C.w"].annotation.canonical_path == "str"
        assert module["C.w"].labels == {"class-attribute"}
        assert module["C.w"].value == "'foo'"

        assert module["C.x"].annotation.canonical_path == "int"
        assert module["C.x"].labels == {"class-attribute"}

        assert module["C.y"].annotation.canonical_path == "str"
        assert module["C.y"].labels == {"instance-attribute"}
        assert module["C.y"].value == "''"

        assert module["C.z"].annotation.canonical_path == "int"
        assert module["C.z"].labels == {"class-attribute", "instance-attribute"}
        assert module["C.z"].value == "5"

        # This is syntactically valid, but semantically invalid
        assert module["C.a"].annotation.canonical_path == "typing.ClassVar"
        assert module["C.a"].annotation.slice.canonical_path == "float"
        assert module["C.a"].labels == {"instance-attribute"}

        assert module["C.b"].annotation.canonical_path == "bytes"
        assert module["C.b"].labels == {"instance-attribute"}


def test_visiting_if_statement_in_class_for_type_guards() -> None:
    """Don't fail on various if statements when checking for type-guards."""
    with temporary_visited_module(
        """
        class A:
            if something("string1 string2"):
                class B:
                    pass
        """,
    ) as module:
        assert module["A.B"].runtime


def test_visiting_relative_imports_triggering_cyclic_aliases() -> None:
    """Skip specific imports to avoid cyclic aliases."""
    with temporary_visited_package(
        "pkg",
        {
            "__init__.py": "from . import a",
            "a.py": "from . import b",
            "b.py": "",
        },
    ) as pkg:
        assert "a" not in pkg.imports
        assert "b" in pkg["a"].imports
        assert pkg["a"].imports["b"] == "pkg.b"


def test_parse_attributes_in__all__() -> None:
    """Parse attributes in `__all__`."""
    with temporary_visited_package(
        "package",
        {
            "__init__.py": "from package import module\n__all__ = module.__all__",
            "module.py": "def hello(): ...\n__all__ = ['hello']",
        },
    ) as package:
        assert "hello" in package.exports  # type: ignore[operator]


def test_parse_deep_attributes_in__all__() -> None:
    """Parse deep attributes in `__all__`."""
    with temporary_visited_package(
        "package",
        {
            "__init__.py": "from package import subpackage\n__all__ = subpackage.module.__all__",
            "subpackage/__init__.py": "from package.subpackage import module",
            "subpackage/module.py": "def hello(): ...\n__all__ = ['hello']",
        },
    ) as package:
        assert "hello" in package.exports  # type: ignore[operator]


# YORE: EOL 3.12: Remove block.
# YORE: EOL 3.11: Remove line.
@pytest.mark.skipif(sys.version_info < (3, 12), reason="Python less than 3.12 does not have PEP 695 generics")
def test_parse_pep695_generics_without_defaults() -> None:
    """Assert PEP 695 generics are correctly inspected."""
    with temporary_visited_module(
        """
        class Class[X: Exception]: pass
        def func[**P, T, *R](arg: T, *args: P.args, **kwargs: P.kwargs) -> tuple[*R]: pass
        type TA[T: (int, str)] = dict[str, T]
        """,
    ) as module:
        class_ = module["Class"]
        assert class_.is_class
        assert class_.type_parameters[0].name == "X"
        assert class_.type_parameters[0].kind == TypeParameterKind.type_var
        assert class_.type_parameters[0].bound.name == "Exception"
        assert not class_.type_parameters[0].constraints
        assert class_.type_parameters[0].default is None

        func = module["func"]
        assert func.is_function
        assert func.type_parameters[0].name == "P"
        assert func.type_parameters[0].kind == TypeParameterKind.param_spec
        assert func.type_parameters[0].bound is None
        assert not func.type_parameters[0].constraints
        assert func.type_parameters[0].default is None
        assert func.type_parameters[1].name == "T"
        assert func.type_parameters[1].kind == TypeParameterKind.type_var
        assert func.type_parameters[1].bound is None
        assert not func.type_parameters[1].constraints
        assert func.type_parameters[1].default is None
        assert func.type_parameters[2].name == "R"
        assert func.type_parameters[2].kind == TypeParameterKind.type_var_tuple
        assert func.type_parameters[2].bound is None
        assert not func.type_parameters[2].constraints
        assert func.type_parameters[2].default is None

        type_alias = module["TA"]
        assert type_alias.is_type_alias
        assert type_alias.type_parameters[0].name == "T"
        assert type_alias.type_parameters[0].kind == TypeParameterKind.type_var
        assert type_alias.type_parameters[0].bound is None
        assert type_alias.type_parameters[0].constraints[0].name == "int"
        assert type_alias.type_parameters[0].constraints[1].name == "str"
        assert type_alias.type_parameters[0].default is None
        assert isinstance(type_alias.value, Expr)
        assert str(type_alias.value) == "dict[str, T]"


# YORE: EOL 3.12: Remove line.
@pytest.mark.skipif(sys.version_info < (3, 13), reason="Python less than 3.13 does not have defaults in PEP 695 generics")  # fmt: skip
def test_parse_pep695_generics() -> None:
    """Assert PEP 695 generics are correctly parsed."""
    with temporary_visited_module(
        """
        class Class[X: Exception = OSError]: pass
        def func[**P, T, *R](arg: T, *args: P.args, **kwargs: P.kwargs) -> tuple[*R]: pass
        type TA[T: (int, str) = str] = dict[str, T]
        """,
    ) as module:
        class_ = module["Class"]
        assert class_.is_class
        assert class_.type_parameters[0].name == "X"
        assert class_.type_parameters[0].kind == TypeParameterKind.type_var
        assert class_.type_parameters[0].bound.name == "Exception"
        assert not class_.type_parameters[0].constraints
        assert class_.type_parameters[0].default.name == "OSError"

        func = module["func"]
        assert func.is_function
        assert func.type_parameters[0].name == "P"
        assert func.type_parameters[0].kind == TypeParameterKind.param_spec
        assert func.type_parameters[0].bound is None
        assert not func.type_parameters[0].constraints
        assert func.type_parameters[0].default is None
        assert func.type_parameters[1].name == "T"
        assert func.type_parameters[1].kind == TypeParameterKind.type_var
        assert func.type_parameters[1].bound is None
        assert not func.type_parameters[1].constraints
        assert func.type_parameters[1].default is None
        assert func.type_parameters[2].name == "R"
        assert func.type_parameters[2].kind == TypeParameterKind.type_var_tuple
        assert func.type_parameters[2].bound is None
        assert not func.type_parameters[2].constraints
        assert func.type_parameters[2].default is None

        type_alias = module["TA"]
        assert type_alias.is_type_alias
        assert type_alias.type_parameters[0].name == "T"
        assert type_alias.type_parameters[0].kind == TypeParameterKind.type_var
        assert type_alias.type_parameters[0].bound is None
        assert type_alias.type_parameters[0].constraints[0].name == "int"
        assert type_alias.type_parameters[0].constraints[1].name == "str"
        assert type_alias.type_parameters[0].default.name == "str"
        assert isinstance(type_alias.value, Expr)
        assert str(type_alias.value) == "dict[str, T]"