Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): read python_classes from the pytest section only, accept TOML lists (#3358)

nosmile9982f4c242113b
diff --git a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
index 9d46ff8ad..7e6e773c7 100644
--- a/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
+++ b/packages/core/src/repowise/core/ingestion/framework_edges/pytest_edges.py
@@ -32,7 +32,7 @@ from fnmatch import fnmatch
from pathlib import Path
from typing import TYPE_CHECKING, Any
-from ...pytest_roots import DEFAULT_PYTHON_FUNCTIONS
+from ...pytest_roots import DEFAULT_PYTHON_FUNCTIONS, _words, pytest_options
from ..resolvers import ResolverContext
from ..source_text import decode_source, source_text
from ..type_names import strip_type_arguments
@@ -110,13 +110,17 @@ _TEST_FILE_RE = re.compile(r"(?:^|/)(?:test_[^/]*|[^/]*_test)\.py$")
# rather than assumed: celery configures `test_*`, and assuming the default
# refused 346 of its 359 bindings.
_DEFAULT_TEST_CLASS_GLOBS = ("Test*",)
-_PYTHON_CLASSES_RE = re.compile(
- r"^\s*python_classes\s*=\s*(.+?)\s*$", re.MULTILINE
-)
def _test_class_globs(repo_path: Path | None) -> tuple[str, ...]:
- """The ``python_classes`` globs this project collects test classes by."""
+ """The ``python_classes`` globs this project collects test classes by.
+
+ Read from the pytest table or section only (see :func:`pytest_options`):
+ a ``python_classes`` line under another tool's section is that tool's, not
+ pytest's. A TOML list is accepted, as pytest does in ``pyproject.toml``;
+ a file that does not parse, or a pytest section without the key, falls
+ through to the next file and finally to the default.
+ """
if repo_path is None:
return _DEFAULT_TEST_CLASS_GLOBS
for name in ("pyproject.toml", "pytest.ini", "tox.ini", "setup.cfg"):
@@ -124,10 +128,17 @@ def _test_class_globs(repo_path: Path | None) -> tuple[str, ...]:
text = (repo_path / name).read_text(encoding="utf-8", errors="ignore")
except OSError:
continue
- match = _PYTHON_CLASSES_RE.search(text)
- if not match:
+ options = pytest_options(name, text)
+ if not options:
continue
- globs = tuple(match.group(1).strip().strip("\"'").split())
+ value = options.get("python_classes")
+ if isinstance(value, str):
+ value = value.strip().strip("\"'")
+ elif not isinstance(value, (list, tuple)):
+ # A TOML scalar that is not a string or list is not a glob setting
+ # pytest would use; fall through instead of crashing on it.
+ value = None
+ globs = _words(value)
if globs:
return globs
return _DEFAULT_TEST_CLASS_GLOBS
diff --git a/tests/unit/ingestion/test_pytest_fixture_edges.py b/tests/unit/ingestion/test_pytest_fixture_edges.py
index e99371a52..fe7c13249 100644
--- a/tests/unit/ingestion/test_pytest_fixture_edges.py
+++ b/tests/unit/ingestion/test_pytest_fixture_edges.py
@@ -14,6 +14,7 @@ import pytest
from repowise.core.ingestion import GraphBuilder
from repowise.core.ingestion.framework_edges import add_framework_edges
+from repowise.core.ingestion.framework_edges.pytest_edges import _test_class_globs
from repowise.core.ingestion.models import FileInfo, ParsedFile
from repowise.core.ingestion.parser import ASTParser
from repowise.core.ingestion.resolvers.context import ResolverContext
@@ -725,3 +726,70 @@ def test_unreadable_test_text_and_unheld_helpers_count_as_hidden(tmp_path: Path)
_add_fixture_injection_edges(graph, parsed, tmp_path, text, None, lambda p: None)
hint = graph["tests/test_a.py"]["tests/conftest.py"]["hint_source"]
assert hint == "pytest_conftest_unrecorded"
+
+
+class TestTestClassGlobs:
+ def test_a_python_classes_key_in_a_foreign_setup_cfg_section_is_ignored(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "setup.cfg").write_text("[tool:other]\npython_classes = Widget*\n")
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_a_python_classes_key_in_a_foreign_tox_ini_section_is_ignored(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "tox.ini").write_text("[flake8]\npython_classes = Foo\n")
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_a_python_classes_key_in_a_foreign_pyproject_table_is_ignored(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pyproject.toml").write_text(
+ '[tool.other]\npython_classes = "Bogus"\n'
+ '[tool.pytest.ini_options]\npython_classes = "Check*"\n'
+ )
+ assert _test_class_globs(tmp_path) == ("Check*",)
+
+ def test_a_python_classes_key_in_a_foreign_pytest_ini_section_is_ignored(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pytest.ini").write_text("[other]\npython_classes = Widget*\n")
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_a_string_value_in_the_pytest_section_is_read(self, tmp_path: Path) -> None:
+ (tmp_path / "pytest.ini").write_text("[pytest]\npython_classes = Widget*\n")
+ assert _test_class_globs(tmp_path) == ("Widget*",)
+
+ def test_a_toml_list_value_in_the_pytest_table_is_read(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pyproject.toml").write_text(
+ '[tool.pytest.ini_options]\npython_classes = ["Check*", "Test*"]\n'
+ )
+ assert _test_class_globs(tmp_path) == ("Check*", "Test*")
+
+ def test_a_pytest_section_without_the_key_falls_through(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pytest.ini").write_text("[pytest]\ntestpaths = tests\n")
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_an_unparseable_file_falls_through_to_the_default(
+ self, tmp_path: Path
+ ) -> None:
+ (tmp_path / "pyproject.toml").write_text("[tool.pytest.ini_options\n")
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_a_non_string_non_list_toml_value_falls_through(
+ self, tmp_path: Path
+ ) -> None:
+ # python_classes = 42 used to crash ingestion with TypeError; a value
+ # pytest itself could not use is ignored instead of breaking the graph.
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.pytest.ini_options]\npython_classes = 42\n"
+ )
+ assert _test_class_globs(tmp_path) == ("Test*",)
+
+ def test_no_config_file_yields_the_default(self, tmp_path: Path) -> None:
+ assert _test_class_globs(tmp_path) == ("Test*",)
+ assert _test_class_globs(None) == ("Test*",)