pytest-dev / pytest-dev/pytest

Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`

Open Beginner friendly
#14,514 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Proposal

Escape dots inside path parts in _pytest.pathlib.compute_module_name, the same way module_name_from_path already does:

  def compute_module_name(root: Path, module_path: Path) -> str | None:
      ...
      names = list(relative.parts)
      if not names:
          return None
      if names[-1] == "__init__":
          names.pop()
+     names = [p.replace(".", "_") for p in names]
      return ".".join(names)

This makes pkg/foo.test.py collect as module pkg.foo_test instead of the invalid pkg.foo.test.

Motivation

The *.test.py filename convention has a property that test_*.py and *_test.py do not: a test file colocated next to its target module cannot be imported by production code. A dot in the filename makes it an invalid Python module name, so the import system cannot reach it through any import statement. With this change, pytest becomes the only thing that can load it.

In practice this:

  • Allows colocating tests next to source (pkg/foo.py + pkg/foo.test.py) without any risk of production code importing test helpers, fixtures, or mock data
  • Eliminates a class of "we accidentally shipped/imported test code" bugs that naming conventions alone cannot prevent
  • Mirrors the widely-used foo.test.ts convention from the JS/TS ecosystem
Current behavior

compute_module_name joins path parts with . without escaping, so pkg/foo.test.py produces the module name pkg.foo.test. Under --import-mode=importlib, _import_module_using_spec then walks up looking for the parent package, concludes it needs pkg.foo, finds pkg/__init__.py adjacent, and loads pkg/__init__.py under the wrong name pkg.foo — silently aliasing the package onto a name that should belong to the sibling source module pkg/foo.py. Subsequent import pkg.foo from inside the test gets the package's __init__.py, not foo.py.

Under --import-mode=prepend|append, the failure is loud: ModuleNotFoundError: No module named 'pkg.foo.test'; 'pkg.foo' is not a package.

The fallback module_name_from_path already applies this exact dot-escape (line 796 of _pytest/pathlib.py):

path_parts = tuple(x.replace(".", "_") for x in path_parts)

But that fallback only runs when resolve_pkg_root_and_module_name fails (no __init__.py chain) — exactly the case where colocation is least useful.

Why this is safe

The proposed line is verbatim what pytest already does in its sibling code path. Applying it consistently in compute_module_name:

  • Has no effect on any filename without a dot (the vast majority of test files)
  • Doesn't change behavior for files where compute_module_name already produces a valid module name
  • Maps dotted filenames to a deterministic name (foo.test.pyfoo_test)
Related
  • #1426 (closed, "warn about invalid test module names")
  • #6422 (closed as invalid, "filename contains more than one dot")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in _pytest/pathlib.py at compute_module_name and compare its path-part handling with module_name_from_path around line 796. Verify the dotted filename case under the relevant import modes, then add the escaping behavior so pkg/foo.test.py resolves as pkg.foo_test without changing undotted filenames.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.