~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Static structural resolvers for the migration evidence guardians.
Nothing here imports or executes a target module. The pytest resolver models
the collection shapes used by the guarded files: top-level ``test_*``
functions and direct ``test_*`` methods on top-level ``Test*`` classes. It is
deliberately conservative when collection status cannot be resolved. It does
not evaluate control-flow conditions or iterables, so ambiguous branches may
omit a node or over-report a blocker rather than credit unproven evidence.
"""
from __future__ import annotations
import ast
from collections import Counter
from dataclasses import dataclass
_BLOCKING_MARKS = frozenset(
{"skip", "skipif", "skipunless", "xfail", "expectedfailure"}
)
_BLOCKING_CALLS = frozenset(
{"pytest.skip", "pytest.importorskip", "unittest.skiptest"}
)
_TEST_FUNCTION_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)
_KNOWN_ROOTS = {
"functools": "functools",
"pytest": "pytest",
"unittest": "unittest",
}
def _qualified_name(node: ast.AST, aliases: dict[str, str]) -> str:
if isinstance(node, ast.Call):
return _qualified_name(node.func, aliases)
if isinstance(node, ast.Name):
return aliases.get(node.id, node.id)
if isinstance(node, ast.Attribute):
parent = _qualified_name(node.value, aliases)
return f"{parent}.{node.attr}" if parent else node.attr
return ""
def _target_path(node: ast.AST) -> str:
if isinstance(node, ast.Name):
return node.id
if isinstance(node, ast.Attribute):
parent = _target_path(node.value)
return f"{parent}.{node.attr}" if parent else node.attr
if isinstance(node, ast.Subscript):
return _target_path(node.value)
return ""
@dataclass
class _Binding:
path: str
node: ast.AST
@dataclass
class _ScopeNodes:
assignments: list[ast.Assign | ast.AnnAssign]
augmented_assignments: list[ast.AugAssign]
calls: list[ast.Call]
bindings: list[_Binding]
raises: list[ast.Raise]
class _ScopeNodeVisitor(ast.NodeVisitor):
"""Inspect module/class execution, including if/try/with branches."""
def __init__(self) -> None:
self.assignments: list[ast.Assign | ast.AnnAssign] = []
self.augmented_assignments: list[ast.AugAssign] = []
self.calls: list[ast.Call] = []
self.bindings: list[_Binding] = []
self.raises: list[ast.Raise] = []
def _record(self, target: ast.AST, node: ast.AST) -> None:
if isinstance(target, ast.Starred):
self._record(target.value, node)
return
if isinstance(target, (ast.List, ast.Tuple)):
for item in target.elts:
self._record(item, node)
return
path = _target_path(target)
if path:
self.bindings.append(_Binding(path, node))
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
self.bindings.append(_Binding(node.name, node))
return
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
self.bindings.append(_Binding(node.name, node))
return
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.bindings.append(_Binding(node.name, node))
return
def visit_Lambda(self, node: ast.Lambda) -> None:
return None
def visit_Assign(self, node: ast.Assign) -> None:
self.assignments.append(node)
for target in node.targets:
self._record(target, node)
self.visit(node.value)
def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
self.assignments.append(node)
self._record(node.target, node)
if node.value is not None:
self.visit(node.value)
def visit_AugAssign(self, node: ast.AugAssign) -> None:
self.augmented_assignments.append(node)
self._record(node.target, node)
self.visit(node.value)
def visit_Delete(self, node: ast.Delete) -> None:
for target in node.targets:
self._record(target, node)
def visit_For(self, node: ast.For) -> None:
self._record(node.target, node)
self.visit(node.iter)
for statement in (*node.body, *node.orelse):
self.visit(statement)
def visit_AsyncFor(self, node: ast.AsyncFor) -> None:
self.visit_For(node)
def visit_With(self, node: ast.With) -> None:
for item in node.items:
self.visit(item.context_expr)
if item.optional_vars is not None:
self._record(item.optional_vars, node)
for statement in node.body:
self.visit(statement)
def visit_AsyncWith(self, node: ast.AsyncWith) -> None:
self.visit_With(node)
def visit_ExceptHandler(self, node: ast.ExceptHandler) -> None:
if node.name is not None:
self.bindings.append(_Binding(node.name, node))
if node.type is not None:
self.visit(node.type)
for statement in node.body:
self.visit(statement)
def visit_NamedExpr(self, node: ast.NamedExpr) -> None:
self._record(node.target, node)
self.visit(node.value)
def visit_MatchAs(self, node: ast.MatchAs) -> None:
if node.name is not None:
self.bindings.append(_Binding(node.name, node))
if node.pattern is not None:
self.visit(node.pattern)
def visit_MatchStar(self, node: ast.MatchStar) -> None:
if node.name is not None:
self.bindings.append(_Binding(node.name, node))
def visit_MatchMapping(self, node: ast.MatchMapping) -> None:
if node.rest is not None:
self.bindings.append(_Binding(node.rest, node))
self.generic_visit(node)
def visit_Import(self, node: ast.Import) -> None:
for imported in node.names:
bound = imported.asname or imported.name.split(".", 1)[0]
self.bindings.append(_Binding(bound, node))
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
for imported in node.names:
bound = (
"*"
if imported.name == "*"
else imported.asname or imported.name
)
self.bindings.append(_Binding(bound, node))
def visit_Call(self, node: ast.Call) -> None:
self.calls.append(node)
self.generic_visit(node)
def visit_Raise(self, node: ast.Raise) -> None:
self.raises.append(node)
self.generic_visit(node)
def _scope_nodes(body: list[ast.stmt]) -> _ScopeNodes:
visitor = _ScopeNodeVisitor()
visitor.visit(ast.Module(body=body, type_ignores=[]))
return _ScopeNodes(
visitor.assignments,
visitor.augmented_assignments,
visitor.calls,
visitor.bindings,
visitor.raises,
)
def _merged_scope(*scopes: _ScopeNodes) -> _ScopeNodes:
return _ScopeNodes(
[item for scope in scopes for item in scope.assignments],
[item for scope in scopes for item in scope.augmented_assignments],
[item for scope in scopes for item in scope.calls],
[item for scope in scopes for item in scope.bindings],
[item for scope in scopes for item in scope.raises],
)
def _import_alias(binding: _Binding) -> str:
node = binding.node
if isinstance(node, ast.Import):
for imported in node.names:
bound = imported.asname or imported.name.split(".", 1)[0]
root = imported.name.split(".", 1)[0]
if bound == binding.path and root in _KNOWN_ROOTS:
return root
elif isinstance(node, ast.ImportFrom) and node.module in _KNOWN_ROOTS:
for imported in node.names:
bound = imported.asname or imported.name
if bound == binding.path:
return f"{node.module}.{imported.name}"
return ""
def _assigned_blocker_alias(binding: _Binding, aliases: dict[str, str]) -> str:
node = binding.node
value: ast.expr | None = None
if isinstance(node, ast.Assign) and any(
_target_path(target) == binding.path for target in node.targets
):
value = node.value
elif (
isinstance(node, ast.AnnAssign)
and _target_path(node.target) == binding.path
):
value = node.value
if value is None:
return ""
qualified = _qualified_name(value, aliases).lower()
return qualified if qualified in _BLOCKING_CALLS else ""
def _aliases_before(
scope: _ScopeNodes,
node: ast.AST,
initial: dict[str, str] | None = None,
) -> dict[str, str]:
"""Resolve direct imports and stopper aliases before one syntax node.
Simple statements use their completion position, which preserves exact
ordering even across semicolons. Compound targets deliberately take effect
after the whole statement: whether a loop/handler executes is unresolved,
so retaining a prior stopper within its body is the fail-closed result.
"""
aliases = dict(_KNOWN_ROOTS if initial is None else initial)
position = (getattr(node, "lineno", 0), getattr(node, "col_offset", 0))
def completion(binding: _Binding) -> tuple[int, int]:
return (
getattr(binding.node, "end_lineno", binding.node.lineno),
getattr(binding.node, "end_col_offset", 0),
)
for binding in sorted(scope.bindings, key=completion):
if completion(binding) >= position:
break
if binding.path == "*":
aliases.update((name, "") for name in aliases)
continue
if "." in binding.path:
continue
aliases[binding.path] = _import_alias(
binding
) or _assigned_blocker_alias(binding, aliases)
return aliases
def _assigned_values(scope: _ScopeNodes, name: str) -> list[ast.expr]:
values: list[ast.expr] = []
for statement in scope.assignments:
if isinstance(statement, ast.Assign):
if any(
_target_path(target) == name for target in statement.targets
):
values.append(statement.value)
elif (
_target_path(statement.target) == name
and statement.value is not None
):
values.append(statement.value)
for statement in scope.augmented_assignments:
if _target_path(statement.target) == name:
values.append(statement.value)
for call in scope.calls:
target = _target_path(call.func)
if target == f"{name}.append" and len(call.args) == 1:
values.append(call.args[0])
elif target == f"{name}.extend" and len(call.args) == 1:
values.append(call.args[0])
elif target == f"{name}.insert" and len(call.args) == 2:
values.append(call.args[1])
elif target.startswith(f"{name}."):
# A direct but unmodelled mutation of pytestmark cannot establish
# that the scope is active. Feed an unresolved expression into the
# marker classifier so collection fails closed.
values.append(ast.Name(id="__unresolved_pytestmark_write"))
return values
def _disabled_or_unresolved_test_targets(scope: _ScopeNodes) -> set[str]:
"""Return ``__test__`` targets not statically proven to be enabled."""
disabled: set[str] = set()
for statement in scope.assignments:
if isinstance(statement, ast.Assign):
targets = statement.targets
value = statement.value
else:
targets = [statement.target]
value = statement.value
if value is None:
continue
statically_enabled = (
isinstance(value, ast.Constant) and value.value is True
)
for target in targets:
path = _target_path(target)
if path == "__test__" and not statically_enabled:
disabled.add("")
elif path.endswith(".__test__") and not statically_enabled:
disabled.add(path[: -len(".__test__")])
for statement in scope.augmented_assignments:
path = _target_path(statement.target)
if path == "__test__":
disabled.add("")
elif path.endswith(".__test__"):
disabled.add(path[: -len(".__test__")])
assigned_nodes = {id(statement) for statement in scope.assignments}
for binding in scope.bindings:
if id(binding.node) in assigned_nodes:
continue
path = binding.path
if path == "__test__":
disabled.add("")
elif path.endswith(".__test__"):
disabled.add(path[: -len(".__test__")])
return disabled
def _name_is_rebound_after(
scope: _ScopeNodes, name: str, definition_line: int
) -> bool:
"""Return whether module/class execution later overwrites ``name``."""
return any(
binding.path in {name, "*"} and binding.node.lineno > definition_line
for binding in scope.bindings
)
def _name_is_aliased_after(
scope: _ScopeNodes, path: str, definition_line: int
) -> bool:
"""Catch direct aliases without treating ordinary call sites as escapes."""
return any(
statement.lineno > definition_line
and statement.value is not None
and _target_path(statement.value) == path
for statement in scope.assignments
)
def _single_stable_value(
scope: _ScopeNodes, name: str, before_line: int
) -> tuple[ast.expr, int] | None:
values: list[tuple[ast.expr, int]] = []
for statement in scope.assignments:
if statement.lineno >= before_line:
continue
if isinstance(statement, ast.Assign):
if any(
_target_path(target) == name for target in statement.targets
):
values.append((statement.value, statement.lineno))
elif (
_target_path(statement.target) == name
and statement.value is not None
):
values.append((statement.value, statement.lineno))
if len(values) != 1:
return None
if (
sum(
binding.path == name and binding.node.lineno < before_line
for binding in scope.bindings
)
!= 1
):
return None
if _name_is_aliased_after(scope, name, 0):
return None
if any(
_target_path(call.func).startswith(f"{name}.") for call in scope.calls
):
return None
return values[0]
def _literal_container_is_empty(
node: ast.AST,
scope: _ScopeNodes,
seen: frozenset[str] = frozenset(),
before_line: int | None = None,
) -> bool | None:
before_line = node.lineno if before_line is None else before_line
if isinstance(node, (ast.List, ast.Tuple, ast.Set)):
return not node.elts
if isinstance(node, ast.Dict):
return not node.keys
if isinstance(node, ast.Name) and node.id not in seen:
stable = _single_stable_value(scope, node.id, before_line)
if stable is not None:
value, assigned_line = stable
return _literal_container_is_empty(
value, scope, seen | {node.id}, assigned_line
)
return None
if isinstance(node, ast.Call):
target = _target_path(node.func)
if (
target in {"list", "set", "sorted", "tuple"}
and len(node.args) == 1
and not node.keywords
):
return _literal_container_is_empty(
node.args[0], scope, seen, before_line
)
if (
isinstance(node.func, ast.Attribute)
and node.func.attr in {"items", "keys", "values"}
and not node.args
and not node.keywords
):
return _literal_container_is_empty(
node.func.value, scope, seen, before_line
)
return None
def _parametrize_argvalues(call: ast.Call) -> ast.expr | None:
if len(call.args) > 1:
return call.args[1]
return next(
(
keyword.value
for keyword in call.keywords
if keyword.arg == "argvalues"
),
None,
)
def _marker_status(
node: ast.AST, aliases: dict[str, str], scope: _ScopeNodes
) -> bool | None:
"""Return True for blocking, False for known-safe, None for unresolved."""
if isinstance(node, (ast.List, ast.Tuple, ast.Set)):
statuses = [_marker_status(item, aliases, scope) for item in node.elts]
if any(status is True for status in statuses):
return True
if any(status is None for status in statuses):
return None
return False
if isinstance(node, ast.Dict):
return None
qualified = _qualified_name(node, aliases).lower()
terminal = qualified.rsplit(".", 1)[-1]
if qualified.startswith("unittest.") and terminal in _BLOCKING_MARKS:
return True
if qualified.startswith("pytest.mark."):
if len(qualified.split(".")) != 3:
return None
if terminal in _BLOCKING_MARKS:
return True
if terminal == "parametrize":
if not isinstance(node, ast.Call):
return None
argvalues = _parametrize_argvalues(node)
if argvalues is None:
return None
empty = _literal_container_is_empty(argvalues, scope)
if empty is None:
return None
return empty
return False
return None
def _decorators_are_safe(
node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef,
aliases: dict[str, str],
scope: _ScopeNodes,
) -> bool:
return not any(
_marker_status(decorator, aliases, scope) is not False
for decorator in node.decorator_list
)
def _has_collection_stopping_call(
scope: _ScopeNodes, aliases: dict[str, str]
) -> bool:
for call in scope.calls:
qualified = _qualified_name(
call.func, _aliases_before(scope, call, aliases)
).lower()
if qualified in {"pytest.skip", "pytest.importorskip"}:
return True
return any(
statement.exc is not None
and _qualified_name(
statement.exc, _aliases_before(scope, statement, aliases)
).lower()
== "unittest.skiptest"
for statement in scope.raises
)
def _has_unresolved_pytestmark_binding(scope: _ScopeNodes) -> bool:
direct_assignments: set[int] = set()
for statement in scope.assignments:
if isinstance(statement, ast.Assign):
if any(
_target_path(target) == "pytestmark"
for target in statement.targets
):
direct_assignments.add(id(statement))
elif (
_target_path(statement.target) == "pytestmark"
and statement.value is not None
):
direct_assignments.add(id(statement))
return any(
binding.path in {"pytestmark", "*"}
and id(binding.node) not in direct_assignments
for binding in scope.bindings
)
def _scope_is_blocked(
scope: _ScopeNodes, aliases: dict[str, str], disabled: set[str]
) -> bool:
if (
"" in disabled
or _has_unresolved_pytestmark_binding(scope)
or _name_is_aliased_after(scope, "pytestmark", 0)
):
return True
for value in _assigned_values(scope, "pytestmark"):
# Dynamic pytestmark expressions fail closed: a guardian cannot prove
# that an unresolved module marker is active.
value_aliases = _aliases_before(scope, value, aliases)
if _marker_status(value, value_aliases, scope) is not False:
return True
return _has_collection_stopping_call(scope, aliases)
class _ImportTimeClassBodyVisitor(ast.NodeVisitor):
"""Collect class bodies and definition headers executed on import."""
def __init__(self) -> None:
self.classes: list[ast.ClassDef] = []
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
return
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
return
def visit_Lambda(self, node: ast.Lambda) -> None:
return None
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.classes.append(node)
for statement in node.body:
self.visit(statement)
def _import_time_classes(tree: ast.Module) -> list[ast.ClassDef]:
visitor = _ImportTimeClassBodyVisitor()
visitor.visit(tree)
return visitor.classes
class _ImmediateDefinitionVisitor(ast.NodeVisitor):
"""Collect definitions executed in one module or class namespace."""
def __init__(self) -> None:
self.definitions: list[
ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef
] = []
def visit_FunctionDef(self, node: ast.FunctionDef) -> None:
self.definitions.append(node)
def visit_AsyncFunctionDef(self, node: ast.AsyncFunctionDef) -> None:
self.definitions.append(node)
def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.definitions.append(node)
def visit_Lambda(self, node: ast.Lambda) -> None:
return None
def _definitions_in_body(
body: list[ast.stmt],
) -> list[ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef]:
visitor = _ImmediateDefinitionVisitor()
visitor.visit(ast.Module(body=body, type_ignores=[]))
return visitor.definitions
def _import_time_definition_contexts(
tree: ast.Module,
module_scope: _ScopeNodes,
module_aliases: dict[str, str],
) -> list[
tuple[ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef, dict[str, str]]
]:
contexts = [
(
definition,
_aliases_before(module_scope, definition, module_aliases),
)
for definition in _definitions_in_body(tree.body)
]
for class_node in _import_time_classes(tree):
class_scope = _scope_nodes(class_node.body)
class_aliases = _aliases_before(
module_scope, class_node, module_aliases
)
contexts.extend(
(
definition,
_aliases_before(class_scope, definition, class_aliases),
)
for definition in _definitions_in_body(class_node.body)
)
return contexts
def _definition_expressions(
node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef,
*,
include_decorators: bool = True,
) -> list[ast.expr]:
expressions = list(node.decorator_list) if include_decorators else []
if isinstance(node, ast.ClassDef):
expressions.extend(node.bases)
expressions.extend(keyword.value for keyword in node.keywords)
return expressions
expressions.extend(node.args.defaults)
expressions.extend(
default for default in node.args.kw_defaults if default is not None
)
arguments = (
*node.args.posonlyargs,
*node.args.args,
*node.args.kwonlyargs,
)
expressions.extend(
argument.annotation
for argument in arguments
if argument.annotation is not None
)
if node.args.vararg is not None and node.args.vararg.annotation is not None:
expressions.append(node.args.vararg.annotation)
if node.args.kwarg is not None and node.args.kwarg.annotation is not None:
expressions.append(node.args.kwarg.annotation)
if node.returns is not None:
expressions.append(node.returns)
return expressions
def _definition_scope(
node: ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef,
*,
include_decorators: bool = True,
) -> _ScopeNodes:
body = [
ast.Expr(value=expression)
for expression in _definition_expressions(
node, include_decorators=include_decorators
)
]
return _scope_nodes(body)
def _definition_values_are_safe(
node: ast.FunctionDef | ast.AsyncFunctionDef,
) -> bool:
"""Defaults/annotations with calls are outside the supported grammar."""
return not _definition_scope(node, include_decorators=False).calls
def _literal_reason(call: ast.Call, qualified: str = "") -> str:
for keyword in call.keywords:
if keyword.arg == "reason" and isinstance(keyword.value, ast.Constant):
if isinstance(keyword.value.value, str):
return keyword.value.value
normalized = qualified.lower()
terminal = normalized.rsplit(".", 1)[-1]
if normalized == "pytest.importorskip":
reason_index = 2
elif terminal in {"skipif", "skipunless", "xfail"}:
reason_index = 1
else:
reason_index = 0
if len(call.args) > reason_index:
argument = call.args[reason_index]
if isinstance(argument, ast.Constant) and isinstance(
argument.value, str
):
return argument.value
return ""
def _blocking_marker_calls(
node: ast.AST, aliases: dict[str, str], scope: _ScopeNodes
) -> list[ast.Call]:
if isinstance(node, (ast.List, ast.Tuple, ast.Set)):
return [
call
for item in node.elts
for call in _blocking_marker_calls(item, aliases, scope)
]
if (
isinstance(node, ast.Call)
and _marker_status(node, aliases, scope) is True
):
return [node]
return []
def _scope_blocking_reasons(
scope: _ScopeNodes, aliases: dict[str, str]
) -> list[str]:
reasons: list[str] = []
for call in scope.calls:
call_aliases = _aliases_before(scope, call, aliases)
qualified = _qualified_name(call.func, call_aliases).lower()
if qualified in {"pytest.skip", "pytest.importorskip"}:
reasons.append(_literal_reason(call, qualified))
for statement in scope.raises:
exception = statement.exc
statement_aliases = _aliases_before(scope, statement, aliases)
if (
exception is not None
and _qualified_name(exception, statement_aliases).lower()
== "unittest.skiptest"
):
reasons.append(
_literal_reason(exception)
if isinstance(exception, ast.Call)
else ""
)
return reasons
def _pytestmark_blocking_reasons(
scope: _ScopeNodes, aliases: dict[str, str]
) -> list[str]:
reasons: list[str] = []
for value in _assigned_values(scope, "pytestmark"):
value_aliases = _aliases_before(scope, value, aliases)
for call in _blocking_marker_calls(value, value_aliases, scope):
qualified = _qualified_name(call.func, value_aliases)
reasons.append(_literal_reason(call, qualified))
return reasons
def module_level_blocking_reasons(source: str) -> tuple[str, ...]:
"""Return literal reasons for supported import-time collection blockers.
Supported forms are blocking ``pytestmark`` assignments plus direct
``pytest.skip``/``pytest.importorskip`` calls and
``raise unittest.SkipTest(...)`` in module/class execution or definition
headers. Direct imports and direct stopper assignments may provide aliases;
reasons must be literal positional or ``reason=`` strings. Unresolved
control flow may conservatively over-report a blocker.
"""
tree = ast.parse(source)
aliases = dict(_KNOWN_ROOTS)
scope = _scope_nodes(tree.body)
reasons = _pytestmark_blocking_reasons(scope, aliases)
reasons.extend(_scope_blocking_reasons(scope, aliases))
for definition, definition_aliases in _import_time_definition_contexts(
tree, scope, aliases
):
reasons.extend(
_scope_blocking_reasons(
_definition_scope(definition), definition_aliases
)
)
for class_node in _import_time_classes(tree):
class_scope = _scope_nodes(class_node.body)
class_aliases = _aliases_before(scope, class_node, aliases)
reasons.extend(_pytestmark_blocking_reasons(class_scope, class_aliases))
reasons.extend(_scope_blocking_reasons(class_scope, class_aliases))
return tuple(reasons)
def active_pytest_nodes(source: str) -> set[str]:
"""Return statically collectible, active test node IDs from ``source``.
Class nodes use ``TestClass::test_method``. Class-only names, nested
classes, duplicate or subsequently rebound definitions, custom-constructed
test classes, fixture/property-decorated functions, blocking collection
marks, ``__test__`` flags not statically proven true, and empty literal
parametrizations do not qualify. Per-parameter/runtime marks, malformed
parametrization signatures or row cardinality, conftest hooks, custom
decorators, and runtime fixture behavior remain normal pytest CI's
responsibility.
"""
tree = ast.parse(source)
aliases = dict(_KNOWN_ROOTS)
module_scope = _scope_nodes(tree.body)
module_disabled = _disabled_or_unresolved_test_targets(module_scope)
if _scope_is_blocked(module_scope, aliases, module_disabled):
return set()
if any(
_has_collection_stopping_call(
_definition_scope(definition), definition_aliases
)
for definition, definition_aliases in _import_time_definition_contexts(
tree, module_scope, aliases
)
):
return set()
# A direct pytest.skip/importorskip executed in any class body aborts
# collection while the module is imported, so it blocks top-level tests as
# well as methods on that class.
if any(
_has_collection_stopping_call(
_scope_nodes(class_node.body),
_aliases_before(module_scope, class_node, aliases),
)
for class_node in _import_time_classes(tree)
):
return set()
top_functions = [
node
for node in tree.body
if isinstance(node, _TEST_FUNCTION_TYPES)
and node.name.startswith("test_")
]
top_classes = [
node
for node in tree.body
if isinstance(node, ast.ClassDef) and node.name.startswith("Test")
]
function_counts = Counter(node.name for node in top_functions)
class_counts = Counter(node.name for node in top_classes)
active: set[str] = set()
for function in top_functions:
if function_counts[function.name] != 1:
continue
if (
function.name in module_disabled
or _name_is_rebound_after(
module_scope, function.name, function.lineno
)
or _name_is_aliased_after(
module_scope, function.name, function.lineno
)
or _name_is_rebound_after(
module_scope,
f"{function.name}.pytestmark",
function.lineno,
)
):
continue
function_aliases = _aliases_before(module_scope, function, aliases)
if not _decorators_are_safe(
function, function_aliases, module_scope
) or not _definition_values_are_safe(function):
continue
active.add(function.name)
for test_class in top_classes:
if (
class_counts[test_class.name] != 1
or test_class.bases
or test_class.keywords
or _name_is_rebound_after(
module_scope, test_class.name, test_class.lineno
)
or _name_is_aliased_after(
module_scope, test_class.name, test_class.lineno
)
or _name_is_rebound_after(
module_scope,
f"{test_class.name}.pytestmark",
test_class.lineno,
)
or any(
_name_is_rebound_after(
module_scope,
f"{test_class.name}.{special}",
test_class.lineno,
)
for special in ("__init__", "__new__")
)
):
continue
class_scope = _scope_nodes(test_class.body)
combined_scope = _merged_scope(module_scope, class_scope)
class_aliases = _aliases_before(module_scope, test_class, aliases)
class_disabled = _disabled_or_unresolved_test_targets(class_scope)
class_markers = _assigned_values(class_scope, "pytestmark")
if (
test_class.name in module_disabled
or "" in class_disabled
or _has_unresolved_pytestmark_binding(class_scope)
or _name_is_aliased_after(class_scope, "pytestmark", 0)
or not _decorators_are_safe(test_class, class_aliases, module_scope)
or _has_collection_stopping_call(class_scope, class_aliases)
or any(
_marker_status(
value,
_aliases_before(class_scope, value, class_aliases),
combined_scope,
)
is not False
for value in class_markers
)
or any(
binding.path in {"__init__", "__new__"}
for binding in class_scope.bindings
)
):
continue
methods = [
member
for member in test_class.body
if isinstance(member, _TEST_FUNCTION_TYPES)
and member.name.startswith("test_")
]
method_counts = Counter(method.name for method in methods)
for method in methods:
node_id = f"{test_class.name}::{method.name}"
if method_counts[method.name] != 1:
continue
if (
method.name in class_disabled
or f"{test_class.name}.{method.name}" in module_disabled
or _name_is_rebound_after(
class_scope, method.name, method.lineno
)
or _name_is_rebound_after(
module_scope,
f"{test_class.name}.{method.name}",
method.lineno,
)
or _name_is_aliased_after(
class_scope, method.name, method.lineno
)
or _name_is_aliased_after(
module_scope,
f"{test_class.name}.{method.name}",
method.lineno,
)
or _name_is_rebound_after(
class_scope,
f"{method.name}.pytestmark",
method.lineno,
)
or _name_is_rebound_after(
module_scope,
f"{test_class.name}.{method.name}.pytestmark",
method.lineno,
)
or not _decorators_are_safe(
method,
_aliases_before(class_scope, method, class_aliases),
combined_scope,
)
or not _definition_values_are_safe(method)
):
continue
active.add(node_id)
return active
def source_definition_kinds(source: str) -> dict[str, str]:
"""Return unique, unrebound direct definitions and methods by kind."""
tree = ast.parse(source)
module_scope = _scope_nodes(tree.body)
candidates: list[
tuple[
str,
str,
ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef,
_ScopeNodes,
ast.ClassDef | None,
]
] = []
for node in tree.body:
if isinstance(node, ast.FunctionDef):
candidates.append((node.name, "function", node, module_scope, None))
elif isinstance(node, ast.AsyncFunctionDef):
candidates.append(
(node.name, "async-function", node, module_scope, None)
)
elif isinstance(node, ast.ClassDef):
class_scope = _scope_nodes(node.body)
candidates.append((node.name, "class", node, module_scope, None))
for member in node.body:
if isinstance(member, ast.FunctionDef):
candidates.append(
(
f"{node.name}::{member.name}",
"method",
member,
class_scope,
node,
)
)
elif isinstance(member, ast.AsyncFunctionDef):
candidates.append(
(
f"{node.name}::{member.name}",
"async-method",
member,
class_scope,
node,
)
)
counts = Counter(name for name, *_rest in candidates)
definitions: dict[str, str] = {}
for name, kind, node, scope, owner in candidates:
local_name = name.rsplit("::", 1)[-1]
if (
counts[name] != 1
or _name_is_rebound_after(scope, local_name, node.lineno)
or _name_is_aliased_after(scope, local_name, node.lineno)
):
continue
if owner is not None:
qualified = f"{owner.name}.{local_name}"
if (
counts[owner.name] != 1
or _name_is_rebound_after(
module_scope, owner.name, owner.lineno
)
or _name_is_aliased_after(
module_scope, owner.name, owner.lineno
)
or _name_is_rebound_after(module_scope, qualified, owner.lineno)
or _name_is_aliased_after(module_scope, qualified, owner.lineno)
):
continue
definitions[name] = kind
return definitions