Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ on:
schedule:
- cron: "0 6 * * 1"
jobs:
lint:
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: dead code and pytest contract
run: |
python3 -m pip install "pytest==7.4.4"
python3 tools/check_dead_code.py
python3 tools/check_pytest_contract.py
python3 -m pytest tests/test_simplicity_checks.py
transport:
runs-on: ubuntu-24.04
env:
Expand Down
15 changes: 15 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Agent rules

Each rule is paired with the check that enforces it. Run the command locally; CI runs the same command.

| Rule | Check | Command | Review finding |
| --- | --- | --- | --- |
| Do not keep code with no caller. An attribute that is only assigned, appended, or length-capped is dead. Do not duck-type `LogXML` or wrap `get_plugin` / `stash.get` in `try/except Exception: pass`, and do not call `get_plugin("html")`. | `tools/check_dead_code.py` | `python3 tools/check_dead_code.py` | Simplicity pass on selectiveci/selective-python-pytest#1 at `6fa455b`, finding #4 (`transport.py` stderr ring buffer) and finding #1 (dead plugin lookups) |
| Pin `pytest>=7.4`. Access attributes that version guarantees (`path`, `location`, `name`, `nodeid`, `duration`, `failed`, `skipped`, `passed`, `when`, `outcome`, `longrepr`, `keywords`) directly. Call `SetupState.teardown_exact` with one argument; do not catch `TypeError` to retry a pytest 6 arity. | `tools/check_pytest_contract.py` | `python3 tools/check_pytest_contract.py` | Same pass, finding #7 (`getattr` hedges on item and report) and finding #5 (pytest 6 arity shims) |

An exception belongs on the offending line and must include a reason. `# noqa: dead-code <reason>` or `# noqa: pytest-contract <reason>`. A bare marker does not suppress the finding.

These hedges are judgment calls. Not enforced:

- `getattr(item, "originalname", item.name)` — `originalname` is not on every item.
- `getattr(config.option, "numprocesses" / "stepwise" / "stepwise_skip" / "setuponly" / "cov_context", ...)` — those options exist only when the plugin is loaded.
9 changes: 4 additions & 5 deletions src/selective_pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,16 @@ def _line_number(item):


def _description(item):
name = getattr(item, "name", "") or item.nodeid
return clean_text(name)
return clean_text(item.name or item.nodeid)


def _fold(reports):
phases = [reports.get("setup"), reports.get("call"), reports.get("teardown")]
phases = [report for report in phases if report is not None]
failed = [report for report in phases if getattr(report, "failed", False)]
failed = [report for report in phases if report.failed]
if failed:
return "failed", failed[0]
skipped = [report for report in phases if getattr(report, "skipped", False)]
skipped = [report for report in phases if report.skipped]
if skipped:
return "pending", skipped[0]
call = reports.get("call")
Expand Down Expand Up @@ -185,7 +184,7 @@ def _silence_terminal(self):
writer.write = lambda *args, **kwargs: None

def pytest_collectreport(self, report):
if getattr(report, "failed", False):
if report.failed:
self.collect_failures.append(report)

@pytest.hookimpl(trylast=True)
Expand Down
2 changes: 1 addition & 1 deletion src/selective_pytest/reporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def forget_terminal(config, nodeid):
return
for key, reports in list(stats.items()):
stats[key] = [
report for report in reports if getattr(report, "nodeid", None) != nodeid
report for report in reports if report.nodeid != nodeid
]


Expand Down
207 changes: 207 additions & 0 deletions tests/test_simplicity_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
import ast
import shutil
from pathlib import Path

from tools import check_dead_code
from tools import check_pytest_contract
from tools.check_dead_code import dead_lookups
from tools.check_dead_code import findings_for
from tools.check_dead_code import write_only
from tools.check_pytest_contract import high_enough
from tools.check_pytest_contract import source_findings

ROOT = Path(__file__).resolve().parents[1]

JUNIT_BEFORE = """
def junit_plugin(config):
try:
from _pytest.junitxml import xml_key
found = config.stash.get(xml_key, None)
if found is not None:
return found
except Exception:
pass
for plugin in config.pluginmanager.get_plugins():
if hasattr(plugin, "node_reporters") and hasattr(plugin, "stats"):
return plugin
return None

def html_plugin(config):
plugin = config.pluginmanager.get_plugin("html")
reports = getattr(plugin, "_reports", None)
if reports is not None:
return plugin
return None

def use(config):
return junit_plugin(config), html_plugin(config)

def main():
return use(None)
"""

JUNIT_AFTER = """
from _pytest.junitxml import xml_key

def junit_plugin(config):
return config.stash.get(xml_key, None)

def html_plugin(config):
for plugin in config.pluginmanager.get_plugins():
if hasattr(plugin, "_reports") and hasattr(plugin, "_report"):
return plugin
return None

def use(config):
return junit_plugin(config), html_plugin(config)

def main():
return use(None)
"""

HEDGE_BEFORE = """
def _file_path(item, cwd):
path = getattr(item, "path", None)
if path is None:
path = item.location[0]
return path

def _line_number(item):
location = getattr(item, "location", (None, None))
lineno = location[1]
return 0 if lineno is None else lineno + 1

def _duration(reports):
return sum(getattr(report, "duration", 0) for report in reports.values())

def finish_hooks(self, item, nextitem):
try:
self.session._setupstate.teardown_exact(item, nextitem)
except TypeError:
self.session._setupstate.teardown_exact(nextitem)
"""

HEDGE_AFTER = """
def _file_path(item, cwd):
return item.path

def _line_number(item):
lineno = item.location[1]
return 0 if lineno is None else lineno + 1

def _duration(reports):
return sum(report.duration for report in reports.values())

def finish_hooks(self):
try:
self.session._setupstate.teardown_exact(None)
except Exception:
self.hook_failed = True
"""


def parsed(text):
return ast.parse(text), text.splitlines()


def ring_buffer(original):
injected = original.replace(
" self.closed = False\n",
" self.closed = False\n"
" self.stderr_lines = []\n"
" self._stderr = threading.Thread(target=self._drain_stderr, daemon=True)\n"
" self._stderr.start()\n",
1,
)
drain = (
" def _drain_stderr(self):\n"
" try:\n"
" for line in self.proc.stderr:\n"
' self.stderr_lines.append(line.rstrip("\\n"))\n'
" if len(self.stderr_lines) > 50:\n"
" del self.stderr_lines[:-50]\n"
" except Exception:\n"
" pass\n"
"\n"
)
injected = injected.replace(" def _read_stdout(self):\n", drain + " def _read_stdout(self):\n", 1)
return injected.replace(
" stdout=subprocess.PIPE,\n",
" stdout=subprocess.PIPE,\n stderr=subprocess.PIPE,\n",
1,
)


def test_unread_stderr_ring_buffer_fails_until_deleted(tmp_path):
original = (ROOT / "src" / "selective_pytest" / "transport.py").read_text()
tree, lines = parsed(ring_buffer(original))
red = write_only(tree, lines, "transport.py")
assert any("stderr_lines" in item and "never read" in item for item in red), red
tree, lines = parsed(original)
assert write_only(tree, lines, "transport.py") == []
src = tmp_path / "src"
shutil.copytree(ROOT / "src", src)
transport = src / "selective_pytest" / "transport.py"
transport.write_text(ring_buffer(original))
cli_red = findings_for([str(src)], [str(src), str(ROOT / "tests")])
assert len(cli_red) == 1 and "stderr_lines" in cli_red[0] and "never read" in cli_red[0]
transport.write_text(original)
assert findings_for([str(src)], [str(src), str(ROOT / "tests")]) == []


def test_dead_plugin_lookups_fail_until_removed():
tree, lines = parsed(JUNIT_BEFORE)
red = dead_lookups(tree, lines, "reporters.py")
assert any("node_reporters" in item for item in red), red
assert any("do not raise" in item for item in red), red
assert any("get_plugin('html')" in item for item in red), red
tree, lines = parsed(JUNIT_AFTER)
assert dead_lookups(tree, lines, "reporters.py") == []


def test_framework_hedges_fail_until_direct_access():
tree, lines = parsed(HEDGE_BEFORE)
red = source_findings(tree, lines, "plugin.py")
text = "\n".join(red)
assert "to access the attribute directly because pytest>=7.4 guarantees it (path)" in text
assert "to access the attribute directly because pytest>=7.4 guarantees it (location)" in text
assert "to access the attribute directly because pytest>=7.4 guarantees it (duration)" in text
assert "pytest-6 arity" in text
tree, lines = parsed(HEDGE_AFTER)
assert source_findings(tree, lines, "plugin.py") == []


def test_pytest_floor_is_7_4():
assert high_enough('dependencies = [\n "pytest>=7.4",\n]\n')
assert high_enough('dependencies = [\n "pytest>=8",\n]\n')
assert not high_enough('dependencies = [\n "pytest>=7",\n]\n')
assert not high_enough('dependencies = [\n "pytest>=6.2",\n]\n')
assert not high_enough("dependencies = []\n")
assert high_enough((ROOT / "pyproject.toml").read_text())


def test_exception_on_the_line_needs_a_reason():
bare = "class Box:\n def __init__(self):\n self.stderr_lines = [] # noqa: dead-code\n"
tree, lines = parsed(bare)
assert write_only(tree, lines, "box.py")
reasoned = (
"class Box:\n"
" def __init__(self):\n"
" self.stderr_lines = [] # noqa: dead-code harness Popen, not the transport buffer\n"
)
tree, lines = parsed(reasoned)
assert write_only(tree, lines, "box.py") == []
hedge = 'def f(item):\n return getattr(item, "path", None) # noqa: pytest-contract\n'
tree, lines = parsed(hedge)
assert source_findings(tree, lines, "plugin.py")
hedge = (
'def f(item):\n'
' return getattr(item, "path", None) # noqa: pytest-contract not a pytest Item\n'
)
tree, lines = parsed(hedge)
assert source_findings(tree, lines, "plugin.py") == []


def test_repository_sources_pass():
assert check_dead_code.main([]) == 0
assert check_pytest_contract.main([]) == 0
Empty file added tools/__init__.py
Empty file.
Loading
Loading