Skip to content

Fix tools imports under a plain pytest invocation - #3484

Merged
deruyter92 merged 1 commit into
devfrom
jaap/fix-tool-imports-pytest
Sep 8, 2026
Merged

deruyter92 merged 1 commit into
devfrom
jaap/fix-tool-imports-pytest

Conversation

@deruyter92

Copy link
Copy Markdown
Collaborator

Problem
test_selector_decision.py imports tools.test_selector, but nothing, but nothing puts the repo root in sys.path at collection time. The import only succeeds currently, because CI invokes python -m pytest, which prepends the cwd. Running pytest directly fails collection with an ImportError.

The following code in tests/tools/conftest.py is actually dead code since it only runs after the test collection already happened.

def load_selector_module() -> ModuleType:
    root = _repo_root()
    ...
    # Ensure repo root is importable so `tools.test_selector` resolves as a package import.
    root_str = str(root)
    if root_str not in sys.path:
        sys.path.insert(0, root_str)

    return importlib.import_module("tools.test_selector")

Changes

  • add pythonpath = ["."] in pyproject.toml under tool.pytest.ini_options
  • remove the dead code in tests/tools/conftest.py

@deruyter92 deruyter92 self-assigned this Sep 7, 2026
@deruyter92
deruyter92 requested a review from C-Achard September 7, 2026 15:30
@deruyter92
deruyter92 marked this pull request as ready for review September 7, 2026 15:30

@C-Achard C-Achard left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to fix this!

- Set `pythonpath = ["."]`
- Remove dead run-time sys.path insertion in `test_selector_decision.py`

Importing from tools is required by e.g. `test_selector_decision.py`, but nothing currently puts the repo root on sys.path at collection time. (Importing `tools.test_selector` currently only worked because CI invokes `python -m pytest`, which prepends the cwd.)

This commit explicitly puts ["."] in the pyproject.toml to ensure importing from tools at test time works (even without inserting cwd).
@deruyter92
deruyter92 force-pushed the jaap/fix-tool-imports-pytest branch from 1c57813 to cc37cd0 Compare September 8, 2026 11:31
@deruyter92
deruyter92 merged commit 7e8dcd2 into dev Sep 8, 2026
46 of 58 checks passed
@deruyter92
deruyter92 deleted the jaap/fix-tool-imports-pytest branch September 8, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants