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(impacted-tests): package inits reach every module under them, and record more fixture request forms (#4632)

Raghav Chamadiyaeeb5dcda7f56
diff --git a/docs/start/CI.md b/docs/start/CI.md
index cfac2bd9f..1e7228bb6 100644
--- a/docs/start/CI.md
+++ b/docs/start/CI.md
@@ -798,6 +798,9 @@ These run, but the summary counts them apart and JSON lists them:
| `selected.basis` | what drove each selection |
| `selected.why` | the `--explain` answer per selected test |
+- Importing `pkg.a.b` counts as importing `pkg/__init__.py` and
+ `pkg/a/__init__.py`, since Python runs them first (basis `package-init`).
+
### Conftest changes
A change reaching a `conftest.py` only through imports selects tests asking for
@@ -808,13 +811,21 @@ a fixture that uses it (directly, via other fixtures, `usefixtures` or
module-level code uses the change, or the conftest imports it only for side
effects (`selected.broadcast`).
- Everything runs when a fixture request is unreadable (computed name, mark in
- a variable, config `usefixtures`), the conftest loads plugins or is
+ a variable, a request name in a string, `exec`/`eval`/`compile`, config
+ `usefixtures`), the conftest loads plugins or is
unreadable, or the index predates this; the reasons name the cause.
<details><summary>Edge cases and ceilings</summary>
- `monkeypatch.setattr(mod.Cls, "name", ...)` on an imported module or its
class in an autouse fixture is left to the import check.
+- So is a bare cache reset there (`clear_*cache*()`, `reset_*cache*()`,
+ `invalidate_*cache*()`, no arguments, result unused). Ceiling: the name is
+ trusted, so a "reset" that also reconfigures shared state reaches tests
+ without their own import of it only through the import check.
+- `request.getfixturevalue(name)` is read when `name` is a test parameter that
+ one `parametrize` supplies from a literal list of names, or from a module
+ constant bound once to one.
- Reading an imported module's literal constant, or calling its function that
uses nothing it imports beyond standard modules that cannot load code by name
(emptying a `functools` cache or standard container counts), reaches only a
diff --git a/packages/core/src/repowise/core/analysis/conftest_routes.py b/packages/core/src/repowise/core/analysis/conftest_routes.py
index 8f2521558..b77ea25d9 100644
--- a/packages/core/src/repowise/core/analysis/conftest_routes.py
+++ b/packages/core/src/repowise/core/analysis/conftest_routes.py
@@ -15,11 +15,12 @@ through its imports can break a test through either:
selected already, chosen as the first by path (an import check). Autouse
fixtures run for that test too, so a patch an autouse fixture applies to a
module on the route (``monkeypatch.setattr(mod, "x", ...)``) that stops
- working fails there as well.
+ working fails there as well. So does a cache reset an autouse fixture runs
+ (below) that starts raising.
2. **What the conftest does with the module.** A fixture, a hook or
module-level code that names something from a module on the route, in any
- way but as the ``setattr`` target (the module or an attribute of it) in an
- autouse fixture or hook,
+ way but as the ``setattr`` target (the module or an attribute of it) or a
+ cache reset (:func:`_cache_reset`) in an autouse fixture or hook,
can change what a test sees: calling it, reading a constant, comparing,
iterating or formatting it all do. An autouse fixture, a ``pytest_*`` hook,
module-level code (class bodies included) and a fixture or hook defined in
@@ -55,7 +56,8 @@ import-time side effect (plugin registration, a patch) elsewhere in the closure
of a module whose local name the conftest reads, since that read still
imports it. A local function that reads or runs repo files by path (``open``,
``Path.read_text``, ``os.system``) is judged by its imports only, as the module
-grain already was. Upgrade path: a per-test map built from a coverage run,
+grain already was. A function named as a cache reset that does more than
+empty a cache is trusted by its name (:func:`_cache_reset`). Upgrade path: a per-test map built from a coverage run,
which records what each test executed.
"""
@@ -63,6 +65,7 @@ from __future__ import annotations
import ast
import builtins
+import re
import symtable
from collections import Counter
from collections.abc import Callable, Collection, Iterator, Mapping
@@ -125,6 +128,8 @@ _RUNS_BY_ATTRIBUTE = frozenset(
) # fmt: skip
# The ``functools`` decorators whose ``cache_clear`` empties a function's cache.
_CACHE_DECORATORS = frozenset({"cache", "lru_cache"})
+# Callee names :func:`_cache_reset` reads as emptying a cache.
+_CACHE_RESET_RE = re.compile(r"(?:clear|reset|invalidate)_\w*cache\w*")
@dataclass(frozen=True)
@@ -250,6 +255,33 @@ def _patch_target(name: ast.Name, parents: Mapping[int, ast.AST]) -> bool:
return called in _TARGET_CALLS
+def _cache_reset(name: ast.Name, parents: Mapping[int, ast.AST]) -> bool:
+ """Whether the name, or an attribute chain read from it, is called as a cache reset.
+
+ The call is a statement of its own (its result unused) with no arguments,
+ and the callee is named ``clear_*cache*``, ``reset_*cache*`` or
+ ``invalidate_*cache*`` (``cache_clear`` keeps its :func:`local_names`
+ rule). Emptying a cache changes no value its owner serves, only how
+ fresh it is, so a test that can see the
+ difference reaches the owner through its own imports and is selected for
+ it; a reset that starts raising fails the import check. Ceiling: the name
+ is trusted, so a "reset" that also rebuilds or reconfigures state other
+ modules read reaches tests without an import route of their own only
+ through the import check.
+ """
+ node, attrs = _dotted_read(name, parents)
+ call = parents.get(id(node))
+ return (
+ _CACHE_RESET_RE.fullmatch(attrs[-1] if attrs else name.id) is not None
+ and not any(_runs_by_attribute(a) for a in attrs)
+ and isinstance(call, ast.Call)
+ and call.func is node
+ and not call.args
+ and not call.keywords
+ and isinstance(parents.get(id(call)), ast.Expr)
+ )
+
+
def _is_literal(node: ast.AST, bound: Collection[str]) -> bool:
"""Whether *node* builds an immutable value from literals alone (``frozenset`` the one call)."""
if isinstance(node, ast.Constant):
@@ -849,7 +881,7 @@ class ConftestFacts:
path = local.get(load.id) or hot.get(load.id)
if path is None:
continue
- if _patch_target(load, self.parents):
+ if _patch_target(load, self.parents) or _cache_reset(load, self.parents):
r.patch = r.patch or load.id
elif not self._reads_local(load, path, stable):
r.run = r.run or load.id
diff --git a/packages/core/src/repowise/core/analysis/test_collection.py b/packages/core/src/repowise/core/analysis/test_collection.py
index 9a06e1c8c..a48e24cd8 100644
--- a/packages/core/src/repowise/core/analysis/test_collection.py
+++ b/packages/core/src/repowise/core/analysis/test_collection.py
@@ -627,7 +627,10 @@ async def _graph_candidates(
texts = pytest_texts or {}
test_files = {f for f in await load_test_files(session, repo_id) if is_code_file(f)}
graph = await load_import_graph(session, repo_id)
- found, entries, calls = await _tier_picks(session, repo_id, targets, test_files, roots, graph)
+ wide = graph.with_package_inits()
+ found, entries, calls = await _tier_picks(
+ session, repo_id, targets, test_files, roots, graph, wide
+ )
seeds = {t for picks in found.values() for t in picks}
parents, parent_entries = await _test_importers(session, repo_id, seeds, test_files, graph)
out, notes = await scoped_candidates(
@@ -644,7 +647,7 @@ async def _graph_candidates(
)
importers = {test: sorted(found_by) for test, found_by in parents.items()}
sources = targets if changed is None else [t for t in targets if t in changed]
- hops = _test_hops(out, calls, graph.hops(sources), set(sources))
+ hops = _test_hops(out, calls, wide.hops(sources), set(sources))
return GraphCandidates(out, importers, notes, hops)
@@ -689,35 +692,44 @@ async def _tier_picks(
test_files: set[str],
roots: PytestRoots | None,
graph=None,
+ wide=None,
) -> tuple[dict[str, dict[str, str]], dict[str, dict], dict[str, Mapping[str, Any]]]:
"""``{target: {test file: via}}``: a changed test itself, then the call and import walks.
Also ``{target: {test file: the files it was reached through}}`` from the
- import walk, and the call walk's distances per target.
+ import walk, and the call walk's distances per target. *wide* is *graph*
+ with the package ``__init__.py`` edges (:meth:`ImportGraph.with_package_inits`):
+ a test only its walk finds is a ``package-init`` pick.
"""
from .test_reachability import tests_reaching_by_tier
- from .test_selection import is_runnable_test
+ from .test_selection import PACKAGE_INIT_VIA, is_runnable_test
reaching = await tests_reaching_by_tier(
session, repo_id, targets, test_files=test_files, import_graph=graph
)
- importers = await tests_reaching_by_tier(
- session,
- repo_id,
- targets,
- call_depth=0,
- import_depth=_IMPORT_CLOSURE_DEPTH,
- test_files=test_files,
- import_graph=graph,
- )
+ walks = [(reaching, None)]
+ imports = [(graph, "import-graph")] + ([(wide, PACKAGE_INIT_VIA)] if wide is not None else [])
+ for g, via in imports:
+ walked = await tests_reaching_by_tier(
+ session,
+ repo_id,
+ targets,
+ call_depth=0,
+ import_depth=_IMPORT_CLOSURE_DEPTH,
+ test_files=test_files,
+ import_graph=g,
+ )
+ walks.append((walked, via))
+ # The widest import walk's routes: a superset of the plain one's.
+ entries = {t: dict(r.entries or {}) for t, r in walks[-1][0].items()}
found: dict[str, dict[str, str]] = {}
- entries = {t: dict(r.entries or {}) for t, r in importers.items()}
for target in targets:
picks = found.setdefault(target, {})
# A test the index has not seen yet (new in this change) is still its own pick.
if target in test_files or is_runnable_test(target, roots):
picks[target] = "changed-test"
- for reached, via in ((reaching.get(target), None), (importers.get(target), "import-graph")):
+ for walked, via in walks:
+ reached = walked.get(target)
for t in _all_tests(reached) if reached else ():
picks.setdefault(t, via or reached.via)
calls = {t: r.reach for t, r in reaching.items() if r.reach}
diff --git a/packages/core/src/repowise/core/analysis/test_reach/maps.py b/packages/core/src/repowise/core/analysis/test_reach/maps.py
index 68f0dc0a7..7a2aa5a50 100644
--- a/packages/core/src/repowise/core/analysis/test_reach/maps.py
+++ b/packages/core/src/repowise/core/analysis/test_reach/maps.py
@@ -11,9 +11,11 @@ from __future__ import annotations
from collections.abc import Collection, Generator, Mapping
from dataclasses import dataclass
+from pathlib import PurePosixPath
from typing import Any, Literal, NamedTuple, TypeVar
from repowise.core.ingestion.models import EXECUTION_EDGE_TYPES, FILE_DEPENDENCY_EDGE_TYPES
+from repowise.core.test_paths import is_test_related_path
from .hubs import call_hub_files
from .model import UNRELIABLE_CALL_ORIGINS
@@ -83,6 +85,36 @@ class ImportGraph:
frontier = fresh
return best
+ def with_package_inits(self) -> ImportGraph:
+ """This graph plus the edge every Python import adds: a module depends on each
+ production ``__init__.py`` above it.
+
+ Importing ``pkg.a.b`` runs ``pkg/__init__.py`` and ``pkg/a/__init__.py``
+ first, so whatever those import reaches every test importing anything
+ under ``pkg``. No import statement records that, so the graph holds no
+ such edge. Every ancestor counts, whether or not the directories between
+ are regular packages (a namespace subpackage still runs the regular
+ package above it): over-selection, never a missed test. A test
+ package's ``__init__.py`` is left out; selection already stands it for
+ every test under its directory.
+ """
+ files = set(self.importers) | {s for srcs in self.importers.values() for s in srcs}
+ inits = {
+ f for f in files
+ if PurePosixPath(f).name == "__init__.py" and not is_test_related_path(f)
+ } # fmt: skip
+ if not inits:
+ return self
+ out = {t: list(s) for t, s in self.importers.items()}
+ for f in files:
+ if not f.endswith(".py"):
+ continue
+ for parent in PurePosixPath(f).parents:
+ init = (parent / "__init__.py").as_posix()
+ if init in inits and init != f:
+ out.setdefault(init, []).append(f)
+ return ImportGraph({t: tuple(sorted(set(s))) for t, s in out.items()})
+
@dataclass(frozen=True)
class WalkMaps:
diff --git a/packages/core/src/repowise/core/analysis/test_selection.py b/packages/core/src/repowise/core/analysis/test_selection.py
index 00421693c..2a9b8c2ab 100644
--- a/packages/core/src/repowise/core/analysis/test_selection.py
+++ b/packages/core/src/repowise/core/analysis/test_selection.py
@@ -29,7 +29,9 @@ is chosen when any of these hold:
missing from the checkout.
Otherwise the subset is the covering tests, the tests the graph shows reaching
-the changed files (a changed test, the call graph, the import graph) and
+the changed files (a changed test, the call graph, the import graph, and a
+test importing any module under a package whose ``__init__.py`` reaches them,
+which Python runs first: ``package-init``) and
``tests.always_run``, plus every test the graph cannot see into: one not
indexed or with no resolved edge, and one the indexer found listing and reading
files under a source directory or running the project's own command or module
@@ -279,7 +281,9 @@ class Selection:
names, per changed or deleted file, what decided it: ``full-run``,
``no-tests-needed``, ``deleted-test``, ``test-tree``, ``test-package``,
``conftest``, ``helper-importers``, ``coverage``,
- ``changed-test``, ``call-graph``, ``import-graph``, ``conftest-fixture``
+ ``changed-test``, ``call-graph``, ``import-graph``, ``package-init`` (a test
+ importing a module under a package whose ``__init__.py`` reaches the change),
+ ``conftest-fixture``
(a test using a conftest fixture that reaches the change),
``conftest-import-check`` (one test that loads a conftest the change reaches
only through its imports), ``conftest-broadcast`` (a test only a conftest
@@ -765,6 +769,9 @@ def _why(
#: The via of a test that runs only because a conftest on the route keeps every
#: test under it (``conftest_routes``): selected, not reached by the change.
BROADCAST_VIA = "conftest-broadcast"
+#: The via of a test that imports a module under a package whose ``__init__.py``
+#: reaches the change: Python runs that file first (``ImportGraph.with_package_inits``).
+PACKAGE_INIT_VIA = "package-init"
# Past this many hops, a test the change reaches only through imports is counted
# apart from the headline; it still runs (an import error breaks it, though
@@ -778,7 +785,7 @@ IMPORT_CLOSURE_HOPS = 1
#: How :attr:`Selection.why` names a test counted apart by :data:`IMPORT_CLOSURE_HOPS`.
IMPORT_CLOSURE_VIA = "import-closure"
# The vias that put a test in by imports alone (a conftest broadcast included).
-_IMPORT_ONLY = frozenset({"import-graph", BROADCAST_VIA})
+_IMPORT_ONLY = frozenset({"import-graph", PACKAGE_INIT_VIA, BROADCAST_VIA})
# The vias a conftest route adds (``conftest_routes``), spelled out.
_VIA_WHY = {
@@ -792,6 +799,10 @@ _VIA_WHY = {
"conftest-import-check: the change reaches a conftest above it only through the "
"conftest's imports, and this test is that conftest's import check"
),
+ PACKAGE_INIT_VIA: (
+ f"{PACKAGE_INIT_VIA}: it imports a module under a package whose __init__.py, "
+ "which Python runs first, reaches the change"
+ ),
}
@@ -1400,6 +1411,7 @@ _VIAS = (
"changed-test",
"call-graph",
"import-graph",
+ PACKAGE_INIT_VIA,
"conftest-fixture",
"conftest-import-check",
BROADCAST_VIA,
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 7dfe083d7..bcc12300d 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
@@ -8,20 +8,23 @@ Every way a test or fixture asks for a fixture by name becomes a
``framework_binds`` edge stamped :data:`FIXTURE_HINT`: a test's parameters, a
fixture's own parameters, ``@pytest.mark.usefixtures(...)`` on a test or its
class, a module's ``pytestmark`` usefixtures, and ``request.getfixturevalue``
-with a literal name. Test selection reads these edges to find every test that
+with a literal name, or with a test parameter whose parametrize values are
+literal names (:func:`_parametrized_names`). Test selection reads these edges to find every test that
uses a fixture, and trusts them only when the stamp shows all of these forms
were recorded. A request no edge can record (a computed name, a test inherited
from a base class declared elsewhere, a class-level ``pytestmark``, marks inside
``pytest.param``, a parametrize call in a hook, any other code use of a
request name such as an aliased mark, ``add_marker`` or ``fixturenames``, a
config ``usefixtures`` or ``python_functions``, a helper asking for a fixture,
-a file whose text is not available) is stamped
+a request name in a string other than a docstring, code run from a string
+with ``exec``, ``eval`` or ``compile``, a file whose text is not available) is stamped
:data:`UNRECORDED_HINT` on the conftest edges it could reach, and selection
keeps every test under those conftests.
"""
from __future__ import annotations
+import ast
import io
import re
import tokenize
@@ -63,8 +66,11 @@ _USEFIXTURES_RE = re.compile(r"^@(?:[\w.]+\.)?usefixtures\b")
# marks): the statement up to the next top-level line.
_PYTESTMARK_RE = re.compile(r"^pytestmark\b[^\n]*(?:\n[ \t)\]][^\n]*)*", re.MULTILINE)
_USEFIXTURES_CALL_RE = re.compile(r"usefixtures\(([^)]*)\)")
-# The name is captured only when it is a literal alone in the call.
-_GETFIXTUREVALUE_RE = re.compile(r"""getfixturevalue\(\s*(?:["'](\w+)["']\s*(?=\)))?""")
+# The name is captured only when it is a literal (group 1) or a bare
+# identifier (group 2) alone in the call.
+_GETFIXTUREVALUE_RE = re.compile(
+ r"""getfixturevalue\(\s*(?:(?:["'](\w+)["']|([A-Za-z_]\w*))\s*(?=\)))?"""
+)
# Names through which code can ask for a fixture. A use of one that is not a
# form this module records (a literal decorator, a module ``pytestmark``, a
# literal ``getfixturevalue`` call in a test or fixture) hides a request.
@@ -450,12 +456,95 @@ def _module_usefixtures(text: str) -> tuple[list[str], bool]:
return names, unknown
-def _request_name_uses(text: str) -> Counter[str] | None:
- """How often code (not strings or comments) uses each of :data:`_REQUEST_NAMES`.
+@dataclass
+class _Lexed:
+ """What one tokenize pass over a file tells the request checks (:func:`_lex`)."""
+
+ # Offsets where ``getfixturevalue`` is code: a name, or text inside an f-string.
+ calls: set[int] = field(default_factory=set)
+ # Uses of each of :data:`_REQUEST_NAMES` outside comments and docstrings.
+ uses: Counter[str] = field(default_factory=Counter)
+ # Whether the file names ``exec``, ``eval`` or ``compile`` other than as an attribute.
+ runs_code: bool = False
+
+ def read_name(self, t: tokenize.TokenInfo, at: int, after_dot: bool) -> None:
+ if t.string in _REQUEST_NAMES:
+ self.uses[t.string] += 1
+ if t.string == "getfixturevalue":
+ self.calls.add(at)
+ self.runs_code = self.runs_code or (t.string in _RUNS_CODE and not after_dot)
+
+ def read_string(self, t: tokenize.TokenInfo, at: int) -> None:
+ words = list(_REQUEST_WORD_RE.finditer(t.string))
+ self.uses.update(m.group(1) for m in words)
+ if _FSTRING_PREFIX_RE.match(t.string):
+ self.calls.update(at + m.start() for m in words if m.group(1) == "getfixturevalue")
+
+
+_RUNS_CODE = frozenset({"exec", "eval", "compile"})
+_REQUEST_WORD_RE = re.compile(r"\b(" + "|".join(sorted(_REQUEST_NAMES)) + r")\b")
+# A string token's prefix holds ``f``: one STRING token per f-string before
+# Python 3.12, whose expressions are code.
+_FSTRING_PREFIX_RE = re.compile(r"[a-zA-Z]*[fF][a-zA-Z]*['\"]")
+_SKIPPED = frozenset({tokenize.COMMENT, tokenize.NL})
+_STATEMENT_START = frozenset({tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT})
+
+
+def _lex(text: str) -> _Lexed | None:
+ """:class:`_Lexed` for *text*; ``None`` when it does not tokenize, which hides everything.
+
+ A request name inside a string counts as a use (``getattr(pytest.mark,
+ "usefixtures")`` asks for a fixture) unless the string is a docstring;
+ comments never count. A substring check first, so the lexer runs only on
+ the few files that mention one of the names.
+ """
+ out = _Lexed()
+ if not any(n in text for n in (*_REQUEST_NAMES, *_RUNS_CODE)):
+ return out
+ starts = [0]
+ for line in io.StringIO(text): # the same lines the tokenizer reads
+ starts.append(starts[-1] + len(line))
+ try:
+ tokens = list(tokenize.generate_tokens(io.StringIO(text).readline))
+ except (tokenize.TokenError, SyntaxError):
+ return None
+ significant = [t for t in tokens if t.type not in _SKIPPED]
+ for i, t in enumerate(significant):
+ at = starts[t.start[0] - 1] + t.start[1]
+ if t.type == tokenize.NAME:
+ out.read_name(t, at, after_dot=i > 0 and significant[i - 1].string == ".")
+ elif _is_text_token(t) and not _is_docstring(significant, i):
+ out.read_string(t, at)
+ return out
+
+
+def _is_text_token(t: tokenize.TokenInfo) -> bool:
+ """A string literal, or the text part of an f-string (Python 3.12+)."""
+ return t.type == tokenize.STRING or tokenize.tok_name.get(t.type) == "FSTRING_MIDDLE"
+
+
+def _is_docstring(significant: list[tokenize.TokenInfo], i: int) -> bool:
+ """Whether the string token at *i* is a statement of its own: a docstring, prose."""
+ t = significant[i]
+ before = significant[i - 1].type if i else tokenize.NEWLINE
+ after = significant[i + 1].type if i + 1 < len(significant) else tokenize.ENDMARKER
+ return (
+ t.type == tokenize.STRING
+ and not _FSTRING_PREFIX_RE.match(t.string)
+ and before in _STATEMENT_START
+ and after in (tokenize.NEWLINE, tokenize.ENDMARKER)
+ )
+
- ``None`` when the text does not tokenize, which hides whatever it holds.
- A substring check first, so the lexer runs only on the few files that
- mention one.
+def _request_name_uses(text: str) -> Counter[str] | None:
+ """How often a helper module's code names each of :data:`_REQUEST_NAMES`.
+
+ Names only, strings left out, unlike a test or conftest (:func:`_lex`):
+ any module that reads a pytest option or matches these forms (a config
+ reader, this analyzer) would otherwise mark every conftest edge in the
+ repository. Ceiling: a helper asking by a string name
+ (``getattr(pytest.mark, "usefixtures")``) is not seen. ``None`` when the
+ text does not tokenize, which hides whatever it holds.
"""
if not any(name in text for name in _REQUEST_NAMES):
return Counter()
@@ -479,22 +568,192 @@ def _hidden_request(text: str) -> str | None:
return None
-def _runtime_requests(text: str, symbols: list[Any]) -> tuple[list[tuple[Any, str]], bool]:
- """``(enclosing function, name)`` per literal ``getfixturevalue``, and whether any is computed."""
- out: list[tuple[Any, str]] = []
+def _runtime_requests(text: str, parsed: Any) -> tuple[list[tuple[Any, list[str]]], bool]:
+ """``(enclosing function, names)`` per readable ``getfixturevalue``, and whether any is not."""
+ out: list[tuple[Any, list[str]]] = []
unknown = False
- functions = [s for s in symbols if s.kind in ("function", "method")]
+ lexed = _lex(text)
+ code = lexed.calls if lexed is not None else None
+ trees: dict[str, ast.Module | None] = {} # parsed once, on the first parameter request
for match in _GETFIXTUREVALUE_RE.finditer(text):
- name = match.group(1)
- line = text.count("\n", 0, match.start()) + 1
- enclosing = [s for s in functions if s.start_line <= line <= s.end_line]
- if name is None or not enclosing:
+ if code is not None and match.start() not in code:
+ continue # inside a comment or a plain string: no call
+ sym = _enclosing(parsed, text.count("\n", 0, match.start()) + 1)
+ names = _call_names(match, text, parsed, sym, trees)
+ if names is None:
unknown = True
- continue
- out.append((max(enclosing, key=lambda s: s.start_line), name))
+ else:
+ out.append((sym, names))
return out, unknown
+def _enclosing(parsed: Any, line: int) -> Any:
+ """The innermost function or method symbol holding *line*, ``None`` outside any."""
+ return max(
+ (
+ s for s in parsed.symbols
+ if s.kind in ("function", "method") and s.start_line <= line <= s.end_line
+ ),
+ key=lambda s: s.start_line,
+ default=None,
+ ) # fmt: skip
+
+
+def _call_names(
+ match: re.Match[str], text: str, parsed: Any, sym: Any, trees: dict[str, ast.Module | None]
+) -> list[str] | None:
+ """The fixtures one ``getfixturevalue`` call in *sym* asks for, ``None`` when unreadable."""
+ if sym is None:
+ return None
+ if match.group(1):
+ return [match.group(1)]
+ if not match.group(2):
+ return None
+ if "tree" not in trees:
+ trees["tree"] = _parse(text)
+ return _parametrized_names(parsed, trees["tree"], sym, match.group(2))
+
+
+def _parse(text: str) -> ast.Module | None:
+ try:
+ return ast.parse(text)
+ except (SyntaxError, ValueError):
+ return None
+
+
+def _parametrized_names(
+ parsed: Any, tree: ast.Module | None, sym: Any, param: str
+) -> list[str] | None:
+ """The fixtures ``request.getfixturevalue(param)`` in test *sym* asks for, else ``None``.
+
+ Read only when *param* is a parameter of *sym* that exactly one direct
+ ``parametrize`` (on the test or a class it runs in) supplies alone, its
+ values a literal list of names or a module constant bound once to one and
+ read nowhere but as a ``parametrize`` value, and nothing in the test
+ rebinds it. pytest then runs the test once per value, and each run asks
+ for that fixture. Any other shape stays unrecorded (``None``).
+ """
+ params = [raw.split(":")[0].strip() for raw in _call_arguments(sym.signature or "")]
+ if tree is None or param not in params or not _keeps_binding(tree, sym, param):
+ return None
+ values = _parametrize_values(parsed, sym, param)
+ if values is None:
+ return None
+ if _LITERAL_LIST_RE.fullmatch(values):
+ return _QUOTED_RE.findall(values)
+ return _constant_names(tree, values)
+
+
+def _parametrize_values(parsed: Any, sym: Any, param: str) -> str | None:
+ """The argvalues text of the one direct parametrize that supplies *param* alone."""
+ classes = {s.name: s for s in parsed.symbols if s.kind == "class"}
+ decorators = [*sym.decorators] + [
+ d for c in _fixture_scopes(parsed, sym.parent_name) if c in classes
+ for d in classes[c].decorators
+ ] # fmt: skip
+ specs = [
+ args
+ for dec in decorators
+ if _PARAMETRIZE_RE.match(dec.strip())
+ and (args := _call_arguments(dec.strip()))
+ and param in _argnames(args[0])
+ ]
+ if len(specs) != 1:
+ return None
+ args = specs[0]
+ if _argnames(args[0]) != [param] or len(args) < 2:
+ return None
+ return args[1] if _indirect_argument(args) in (None, "False") else None
+
+
+def _keeps_binding(tree: ast.Module, sym: Any, param: str) -> bool:
+ """Whether the body of test *sym* never rebinds or shadows its parameter *param*."""
+ node = next(
+ (
+ n for n in ast.walk(tree)
+ if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
+ and n.name == sym.name and sym.start_line <= n.lineno <= sym.end_line
+ ),
+ None,
+ ) # fmt: skip
+ if node is None:
+ return False
+ args = sum(isinstance(n, ast.arg) and n.arg == param for n in ast.walk(node))
+ stores = any(_rebinds(n, param) for n in ast.walk(node))
+ return args == 1 and not stores and f"nonlocal {param}" not in ast.unparse(node)
+
+
+def _rebinds(n: ast.AST, param: str) -> bool:
+ """Whether node *n* binds *param*: an assignment target, ``except ... as``,
+ an import alias, or a nested ``def`` / ``class`` of that name."""
+ if isinstance(n, ast.Name):
+ return n.id == param and not isinstance(n.ctx, ast.Load)
+ if isinstance(n, (ast.ExceptHandler, ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
+ return n.name == param
+ if isinstance(n, ast.alias):
+ return (n.asname or n.name.partition(".")[0]) == param # `import a.b` binds `a`
+ return False
+
+
+def _constant_names(tree: ast.Module, name: str) -> list[str] | None:
+ """The string values of module constant *name*, when its only read is a parametrize value."""
+ if not re.fullmatch(r"[A-Za-z_]\w*", name):
+ return None
+ parents = {id(c): p for p in ast.walk(tree) for c in ast.iter_child_nodes(p)}
+ values: list[str] | None = None
+ for n in ast.walk(tree):
+ if _binds_otherwise(n, name):
+ return None
+ if not (isinstance(n, ast.Name) and n.id == name):
+ continue
+ parent = parents.get(id(n))
+ if isinstance(n.ctx, ast.Load):
+ if not _is_parametrize_values(n, parent):
+ return None
+ continue
+ if values is not None:
+ return None
+ values = _assigned_strings(tree, parent, n)
+ if values is None:
+ return None
+ return values
+
+
+def _binds_otherwise(node: ast.AST, name: str) -> bool:
+ """Whether *node* binds *name* by a definition, an import or a ``global``."""
+ if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
+ return node.name == name
+ if isinstance(node, ast.alias):
+ # `import a.b` binds `a`: the head of a module path, not a type name.
+ return (node.asname or node.name.split(".")[0]) == name
+ return isinstance(node, (ast.Global, ast.Nonlocal)) and name in node.names
+
+
+def _is_parametrize_values(name: ast.Name, parent: ast.AST | None) -> bool:
+ """Whether *name* is the second positional argument of a ``parametrize`` call."""
+ func = getattr(parent, "func", None)
+ called = getattr(func, "attr", getattr(func, "id", None))
+ return called == "parametrize" and len(parent.args) > 1 and parent.args[1] is name
+
+
+def _assigned_strings(
+ tree: ast.Module, stmt: ast.AST | None, target: ast.Name
+) -> list[str] | None:
+ """The strings a module-level ``target = [...]`` binds, ``None`` for any other binding."""
+ value = getattr(stmt, "value", None)
+ top = (
+ isinstance(stmt, ast.Assign)
+ and any(stmt is s for s in tree.body)
+ and stmt.targets == [target]
+ )
+ if not top or not isinstance(value, (ast.List, ast.Tuple)):
+ return None
+ strings = [
+ e.value for e in value.elts if isinstance(e, ast.Constant) and isinstance(e.value, str)
+ ]
+ return strings if len(strings) == len(value.elts) else None
+
+
def _add_fixture_injection_edges(
graph: nx.DiGraph,
parsed_files: dict[str, Any],
@@ -723,13 +982,15 @@ def _link_text(
runtime_unknown, everywhere = _link_runtime_requests(linker, text, tests, fixture_ids, recorded)
# Any other use (an alias, a mark stored in a variable, `add_marker`,
# `request.fixturenames`, a lazy-fixture plugin) is a request no edge records.
- uses = _request_name_uses(text)
+ # Code run from a string (``exec``, ``eval``, ``compile``) can ask in any way.
+ lexed = _lex(text)
hidden = (
_hidden_request(text) is not None
or marks_unknown
or runtime_unknown
- or uses is None
- or any(n > recorded[name] for name, n in uses.items())
+ or lexed is None
+ or lexed.runs_code
+ or any(n > recorded[name] for name, n in lexed.uses.items())
)
return hidden, everywhere
@@ -756,14 +1017,14 @@ def _link_runtime_requests(
fixture_ids: set[str],
recorded: Counter[str],
) -> tuple[bool, bool]:
- """Link literal ``getfixturevalue`` calls; ``(any computed, a helper asks)``."""
+ """Link readable ``getfixturevalue`` calls; ``(any computed, a helper asks)``."""
if "getfixturevalue" not in text:
return False, False
- runtime, unknown = _runtime_requests(text, linker.parsed.symbols)
+ runtime, unknown = _runtime_requests(text, linker.parsed)
everywhere = False
- for sym, name in runtime:
+ for sym, names in runtime:
if sym in tests or sym.id in fixture_ids:
- linker.link(sym, [name])
+ linker.link(sym, names)
recorded["getfixturevalue"] += 1
else:
# A helper asking for a fixture serves whichever test calls it.
diff --git a/tests/unit/analysis/test_conftest_routes.py b/tests/unit/analysis/test_conftest_routes.py
index b26581a54..44d5d6200 100644
--- a/tests/unit/analysis/test_conftest_routes.py
+++ b/tests/unit/analysis/test_conftest_routes.py
@@ -95,6 +95,72 @@ def test_a_requested_fixture_that_only_patches_the_route_still_reaches_its_users
assert use.fixtures == {"quiet"}
+ "imports, line",
+ [
+ ("from app.store import clear_store_cache", "clear_store_cache()"),
+ ("from app import store", "store.reset_caches()"),
+ ("from app import store", "store.Lookup.invalidate_cache()"),
+ ],
+)
+def test_an_autouse_fixture_resetting_a_cache_is_import_time_only(imports: str, line: str) -> None:
+ """Emptying a cache changes no value; a test that sees it imports the owner itself."""
+ use = _use(
+ f"""
+ import pytest
+
+ @pytest.fixture(autouse=True)
+ def fresh():
+ {imports}
+ {line}
+ yield
+ {line}
+ """
+ )
+ assert use.run_all is None
+ assert use.fixtures == frozenset()
+
+
+ "line",
+ [
+ "store.reset()", # not named as a cache
+ "store.clear_cache(1)", # arguments
+ "x = store.clear_cache()", # result used
+ "store.clear_cache", # read, not called
+ "assert store.clear_cache() is None",
+ ],
+)
+def test_anything_but_a_bare_cache_reset_in_an_autouse_fixture_keeps_every_test(
+ line: str,
+) -> None:
+ use = _use(
+ f"""
+ import pytest
+ from app import store
+
+ @pytest.fixture(autouse=True)
+ def fresh():
+ {line}
+ """
+ )
+ assert use.run_all and "autouse fixture fresh uses store" in use.run_all
+
+
+def test_a_requested_fixture_resetting_a_cache_still_reaches_its_users() -> None:
+ use = _use(
+ """
+ import pytest
+ from app import store
+
+ @pytest.fixture
+ def fresh():
+ store.clear_cache()
+ """
+ )
+ assert use.fixtures == {"fresh"}
+
+
def test_a_class_body_or_a_fixture_in_a_class_using_the_route_keeps_every_test() -> None:
body = _use(
"""
diff --git a/tests/unit/analysis/test_test_selection.py b/tests/unit/analysis/test_test_selection.py
index 58c6fb034..a83e6946d 100644
--- a/tests/unit/analysis/test_test_selection.py
+++ b/tests/unit/analysis/test_test_selection.py
@@ -820,6 +820,18 @@ def test_a_test_importing_a_deleted_file_is_never_counted_apart() -> None:
assert "tests/test_x.py" in sel.test_files and not sel.import_closure
+def test_a_package_init_test_runs_and_names_why() -> None:
+ tiers = _tiers(inferred=[_inferred("pkg/core.py", "tests/test_leaf.py", "package-init")])
+ tiers["test_hops"] = {"tests/test_leaf.py": 3}
+ sel = _select(["pkg/core.py"], tiers)
+ assert not sel.run_all, sel.reasons
+ assert sel.test_files == ("tests/test_leaf.py",)
+ assert sel.basis == {"pkg/core.py": "package-init"}
+ assert "package-init: it imports a module under a package" in sel.why["tests/test_leaf.py"]
+ # Import-time reach only, so far away it is counted apart, never dropped.
+ assert sel.import_closure == {"tests/test_leaf.py": 3}
+
+
def test_a_covered_test_is_never_counted_apart() -> None:
covered = {"tests/test_x.py::t": {"test_file": "tests/test_x.py", "source_files": ["src/a.py"]}}
tiers = _tiers(covered=covered, inferred=[_inferred("src/a.py", "tests/test_x.py", "import-graph")])
diff --git a/tests/unit/ingestion/test_pytest_fixture_edges.py b/tests/unit/ingestion/test_pytest_fixture_edges.py
index fe7c13249..1909fed6b 100644
--- a/tests/unit/ingestion/test_pytest_fixture_edges.py
+++ b/tests/unit/ingestion/test_pytest_fixture_edges.py
@@ -625,6 +625,40 @@ class TestUnrecordedRequests:
"def test_x(request):\n gfv = request.getfixturevalue\n gfv('db')\n",
"import pytest\n\npytestmark = pytest.mark.usefixtures(*names())\n\n\n"
"def test_x():\n pass\n",
+ # A parametrized name the text cannot pin to its literal values.
+ "import pytest\n\n\[email protected]('f', ['db'])\n"
+ "def test_x(f, request):\n f = 'other'\n request.getfixturevalue(f)\n",
+ # Rebinding forms that are not a plain assignment.
+ *(
+ "import pytest\n\n\[email protected]('f', ['db'])\n"
+ f"def test_x(f, request):\n{rebind} request.getfixturevalue(f)\n"
+ for rebind in (
+ " try:\n pass\n except Exception as f:\n pass\n",
+ " import f\n",
+ " from os import path as f\n",
+ " def f():\n pass\n",
+ " class f:\n pass\n",
+ )
+ ),
+ "import pytest\n\nNAMES = ['db']\nNAMES.append('other')\n\n\n"
+ "@pytest.mark.parametrize('f', NAMES)\n"
+ "def test_x(f, request):\n request.getfixturevalue(f)\n",
+ "import pytest\n\n\[email protected]('f', names())\n"
+ "def test_x(f, request):\n request.getfixturevalue(f)\n",
+ "import pytest\n\n\[email protected]('f', ['db'])\n"
+ "@pytest.mark.parametrize('f', ['other'])\n"
+ "def test_x(f, request):\n request.getfixturevalue(f)\n",
+ "import pytest\n\n\[email protected]('f, g', [('db', 1)])\n"
+ "def test_x(f, g, request):\n request.getfixturevalue(f)\n",
+ # Forms that hide in a string: an indirect passed by **kwargs, a mark
+ # read by name, a call quoted in code, and code run from a string.
+ "def pytest_generate_tests(metafunc):\n kw = {'indirect': True}\n"
+ " metafunc.parametrize('db', [1], **kw)\n\n\ndef test_x(db):\n pass\n",
+ "import pytest\n\n\n@getattr(pytest.mark, 'usefixtures')('db')\n"
+ "def test_x():\n pass\n",
+ "def test_x(request):\n src = 'request.getfixturevalue(NAME)'\n assert src\n",
+ "def test_x(request):\n exec('x = 1')\n",
+ "def test_x(request):\n run = eval\n assert run\n",
],
)
def test_a_hidden_request_marks_its_own_edge(self, tmp_path: Path, body: str) -> None:
@@ -632,6 +666,72 @@ class TestUnrecordedRequests:
assert hints["tests/test_odd.py"] == "pytest_conftest_unrecorded"
assert hints["tests/test_plain.py"] == "pytest_conftest"
+ @pytest.mark.parametrize(
+ "values, prelude",
+ [("['db', 'cache']", ""), ("NAMES", "NAMES = ('db', 'cache')\n\n\n")],
+ )
+ def test_a_parametrized_fixture_name_is_recorded(
+ self, tmp_path: Path, values: str, prelude: str
+ ) -> None:
+ body = (
+ f"import pytest\n\n{prelude}"
+ f"@pytest.mark.parametrize('name', {values})\nclass TestEach:\n"
+ " def test_x(self, name, request):\n"
+ " assert request.getfixturevalue(name)\n"
+ )
+ tests = tmp_path / "tests"
+ tests.mkdir()
+ (tests / "conftest.py").write_text(
+ "import pytest\n\n\[email protected]\ndef db():\n return 1\n\n\n"
+ "@pytest.fixture\ndef cache():\n return 2\n"
+ )
+ (tests / "test_odd.py").write_text(body)
+ graph = _build(tmp_path)
+
+ assert self._hints(graph)["tests/test_odd.py"] == "pytest_conftest"
+ bound = _bound(graph)
+ assert ("tests/test_odd.py::TestEach::test_x", "tests/conftest.py::db") in bound
+ assert ("tests/test_odd.py::TestEach::test_x", "tests/conftest.py::cache") in bound
+
+ @pytest.mark.parametrize(
+ "line",
+ [
+ "assert db # request.getfixturevalue(other)",
+ '"""Reads request.getfixturevalue(NAME) names."""',
+ "pattern = re.compile('x')",
+ "model.eval()",
+ ],
+ )
+ def test_a_call_in_a_comment_docstring_or_attribute_is_no_request(
+ self, tmp_path: Path, line: str
+ ) -> None:
+ body = f"import re\n\n\ndef test_x(db, model=None):\n {line}\n"
+ assert self._hints(self._repo(tmp_path, body))["tests/test_odd.py"] == "pytest_conftest"
+
+ def test_an_f_string_is_code_when_it_is_one_token(self, monkeypatch) -> None:
+ """Before Python 3.12 an f-string is one STRING token; its call still counts."""
+ import io
+ import tokenize
+
+ from repowise.core.ingestion.framework_edges import pytest_edges
+
+ text = 'x = f"{request.getfixturevalue(n)}"\n'
+ whole = tokenize.TokenInfo(tokenize.STRING, text[4:-1], (1, 4), (1, len(text) - 1), text)
+ real = tokenize.generate_tokens
+
+ def as_311(readline):
+ for t in real(io.StringIO(text).readline):
+ if t.start[1] < 4 or t.type in (tokenize.NEWLINE, tokenize.ENDMARKER):
+ yield t
+ elif t.start == (1, 4):
+ yield whole
+
+ monkeypatch.setattr(tokenize, "generate_tokens", as_311)
+ lexed = pytest_edges._lex(text)
+ assert lexed is not None
+ assert text.index("getfixturevalue") in lexed.calls
+ assert lexed.uses["getfixturevalue"] == 1
+
def test_a_helper_asking_for_a_fixture_marks_every_edge(self, tmp_path: Path) -> None:
graph = self._repo(
tmp_path,
diff --git a/tests/unit/persistence/test_impacted_tests_resolution.py b/tests/unit/persistence/test_impacted_tests_resolution.py
index a852af208..f1d47ecb3 100644
--- a/tests/unit/persistence/test_impacted_tests_resolution.py
+++ b/tests/unit/persistence/test_impacted_tests_resolution.py
@@ -245,6 +245,33 @@ async def test_graph_candidates_are_not_capped(async_session) -> None:
assert set(_pairs(out, "src/hub.py")) == tests
+async def test_a_package_init_reaching_the_change_reaches_every_module_under_it(
+ async_session,
+) -> None:
+ """Importing ``pkg.a.leaf`` runs ``pkg/__init__.py`` and ``pkg/a/__init__.py`` first."""
+ repo = await insert_repo(async_session)
+ await _graph(
+ async_session,
+ repo.id,
+ tests={"tests/test_leaf.py", "tests/test_other.py", "tests/test_core.py"},
+ files={"pkg/__init__.py", "pkg/a/__init__.py", "pkg/a/leaf.py", "pkg/core.py", "other/x.py"},
+ imports=[
+ ("pkg/__init__.py", "pkg/core.py"),
+ ("tests/test_leaf.py", "pkg/a/leaf.py"),
+ ("tests/test_other.py", "other/x.py"),
+ ("tests/test_core.py", "pkg/core.py"),
+ ],
+ )
+
+ out = _empty_result(1)
+ await _resolve_impacted(async_session, repo.id, {"pkg/core.py": {1}}, set(), out)
+
+ assert _pairs(out, "pkg/core.py") == {
+ "tests/test_core.py": "import-graph",
+ "tests/test_leaf.py": "package-init",
+ }
+
+
async def test_a_file_level_lookup_also_asks_the_graph(async_session) -> None:
"""Matched by file (a deleted file, a stale map), coverage alone is not enough."""
repo = await _seed(async_session)
diff --git a/tests/unit/test_no_bare_type_name_copies.py b/tests/unit/test_no_bare_type_name_copies.py
index 0d15010ee..bd60735fe 100644
--- a/tests/unit/test_no_bare_type_name_copies.py
+++ b/tests/unit/test_no_bare_type_name_copies.py
@@ -120,6 +120,10 @@ _KNOWN: dict[str, int] = {
_PREFIX + "framework_edges/rust.py": 1,
_PREFIX + "framework_edges/go.py": 1,
_PREFIX + "framework_edges/flask.py": 1,
+ # Takes the head of an `import a.b` path (twice: the module's bound names and
+ # a test body rebinding a parameter): the name the import binds, a module
+ # name, not a type.
+ _PREFIX + "framework_edges/pytest_edges.py": 2,
# Takes the qualifier HEAD of `OrderHandlers.GetOrder` to reach the
# declaring type. It does want a type, but the shared helper returns the
# trailing segment, which is the member — the opposite end, the same reason