From 611ed5868e22ac3ada6467ba25c6dab606f5dee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Fri, 26 May 2023 16:11:12 +0200 Subject: [PATCH] feat: Provide test helpers and pytest fixtures --- src/griffe/agents/visitor.py | 2 ++ tests/helpers.py => src/griffe/tests.py | 25 +++++++++++++++++++++++-- tests/conftest.py | 4 +--- tests/test_dataclasses.py | 2 +- tests/test_diff.py | 2 +- tests/test_expressions.py | 2 +- tests/test_finder.py | 2 +- tests/test_functions.py | 2 +- tests/test_inspector.py | 2 +- tests/test_loader.py | 2 +- tests/test_merger.py | 2 +- tests/test_mixins.py | 2 +- tests/test_nodes.py | 2 +- tests/test_visitor.py | 2 +- 14 files changed, 37 insertions(+), 16 deletions(-) rename tests/helpers.py => src/griffe/tests.py (92%) diff --git a/src/griffe/agents/visitor.py b/src/griffe/agents/visitor.py index b429679d..26a18862 100644 --- a/src/griffe/agents/visitor.py +++ b/src/griffe/agents/visitor.py @@ -326,6 +326,8 @@ def handle_function(self, node: ast.AsyncFunctionDef | ast.FunctionDef, labels: lineno = node.decorator_list[0].lineno for decorator_node in node.decorator_list: decorator_value = safe_get_value(decorator_node, self.filepath) + if decorator_value is None: + continue overload = ( decorator_value in typing_overload or decorator_value == "overload" diff --git a/tests/helpers.py b/src/griffe/tests.py similarity index 92% rename from tests/helpers.py rename to src/griffe/tests.py index acf9a9e9..3009b73f 100644 --- a/tests/helpers.py +++ b/src/griffe/tests.py @@ -1,4 +1,14 @@ -"""General helpers for tests.""" +"""Test helpers and pytest fixtures. + +Load fixtures in your own tests by adding `griffe.tests` +to the [`pytest_plugins`][pytest_plugins] list: + +```python title="conftest.py" +pytest_plugins = ["griffe.tests"] +``` + +[pytest_plugins]: https://docs.pytest.org/en/7.1.x/how-to/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file +""" from __future__ import annotations @@ -12,13 +22,24 @@ from typing import Iterator, Mapping, Sequence from griffe.agents.inspector import inspect -from griffe.agents.visitor import visit +from griffe.agents.visitor import patch_ast, visit from griffe.dataclasses import Module, Object from griffe.loader import GriffeLoader TMPDIR_PREFIX = "griffe_" +try: + import pytest + + @pytest.fixture(scope="session", autouse=True) + def _fixture_patch_ast() -> None: + patch_ast() + +except ImportError: + pass + + @contextmanager def temporary_pyfile(code: str) -> Iterator[tuple[str, Path]]: """Create a module.py file containing the given code in a temporary directory. diff --git a/tests/conftest.py b/tests/conftest.py index e0cd4b29..137d04b7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,4 @@ from __future__ import annotations -from griffe.agents.visitor import patch_ast - -patch_ast() +pytest_plugins = ["griffe.tests"] diff --git a/tests/test_dataclasses.py b/tests/test_dataclasses.py index 37a267c5..ebf245a3 100644 --- a/tests/test_dataclasses.py +++ b/tests/test_dataclasses.py @@ -6,7 +6,7 @@ from griffe.dataclasses import Docstring, Module from griffe.loader import GriffeLoader -from tests.helpers import module_vtree, temporary_pypackage +from griffe.tests import module_vtree, temporary_pypackage def test_submodule_exports() -> None: diff --git a/tests/test_diff.py b/tests/test_diff.py index 99195a17..e05c8134 100644 --- a/tests/test_diff.py +++ b/tests/test_diff.py @@ -7,7 +7,7 @@ import pytest from griffe.diff import Breakage, BreakageKind, find_breaking_changes -from tests.helpers import temporary_visited_module, temporary_visited_package +from griffe.tests import temporary_visited_module, temporary_visited_package @pytest.mark.skipif(sys.version_info < (3, 8), reason="no positional-only parameters on Python 3.7") diff --git a/tests/test_expressions.py b/tests/test_expressions.py index 90c3f370..d5c23065 100644 --- a/tests/test_expressions.py +++ b/tests/test_expressions.py @@ -5,7 +5,7 @@ import pytest from griffe.docstrings.parsers import Parser -from tests.helpers import temporary_visited_module +from griffe.tests import temporary_visited_module @pytest.mark.parametrize( diff --git a/tests/test_finder.py b/tests/test_finder.py index 448abcad..a6d8b7ba 100644 --- a/tests/test_finder.py +++ b/tests/test_finder.py @@ -8,7 +8,7 @@ import pytest from griffe.finder import ModuleFinder, NamespacePackage, _handle_editable_module, _handle_pth_file -from tests.helpers import temporary_pypackage +from griffe.tests import temporary_pypackage @pytest.mark.parametrize( diff --git a/tests/test_functions.py b/tests/test_functions.py index 4dc0feef..75f5c3ff 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -7,7 +7,7 @@ import pytest from griffe.dataclasses import ParameterKind -from tests.helpers import temporary_visited_module +from griffe.tests import temporary_visited_module def test_visit_simple_function() -> None: diff --git a/tests/test_inspector.py b/tests/test_inspector.py index 138052ca..f10aba4c 100644 --- a/tests/test_inspector.py +++ b/tests/test_inspector.py @@ -9,7 +9,7 @@ from griffe.agents.inspector import inspect from griffe.expressions import Name -from tests.helpers import temporary_inspected_module, temporary_pypackage +from griffe.tests import temporary_inspected_module, temporary_pypackage def test_annotations_from_builtin_types() -> None: diff --git a/tests/test_loader.py b/tests/test_loader.py index b56bf01b..74d47252 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -8,7 +8,7 @@ from griffe.expressions import Name from griffe.loader import GriffeLoader -from tests.helpers import temporary_pyfile, temporary_pypackage +from griffe.tests import temporary_pyfile, temporary_pypackage if TYPE_CHECKING: import pytest diff --git a/tests/test_merger.py b/tests/test_merger.py index b4d16dbc..56d4ade9 100644 --- a/tests/test_merger.py +++ b/tests/test_merger.py @@ -5,7 +5,7 @@ from textwrap import dedent from griffe.loader import GriffeLoader -from tests.helpers import temporary_pypackage +from griffe.tests import temporary_pypackage def test_dont_trigger_alias_resolution_when_merging_stubs() -> None: diff --git a/tests/test_mixins.py b/tests/test_mixins.py index a5f918be..f771dd75 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -2,7 +2,7 @@ from __future__ import annotations -from tests.helpers import module_vtree +from griffe.tests import module_vtree def test_access_members_using_string_and_tuples() -> None: diff --git a/tests/test_nodes.py b/tests/test_nodes.py index c83dfb3b..e44f15bb 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -10,7 +10,7 @@ from griffe.agents.nodes import get_value, relative_to_absolute from griffe.expressions import Expression, Name -from tests.helpers import module_vtree, temporary_visited_module +from griffe.tests import module_vtree, temporary_visited_module @pytest.mark.parametrize( diff --git a/tests/test_visitor.py b/tests/test_visitor.py index 95cd5a2f..ef39f0fd 100644 --- a/tests/test_visitor.py +++ b/tests/test_visitor.py @@ -7,7 +7,7 @@ import pytest from griffe.loader import GriffeLoader -from tests.helpers import temporary_pypackage, temporary_visited_module +from griffe.tests import temporary_pypackage, temporary_visited_module # @given(hs.from_node(node=libcst.Module)) # @pytest.mark.skipif(sys.version_info >= (3, 11, 0), reason="Too slow on Python 3.11?")