Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`
I maintainer di solito rispondono entro 1 giorno
@siddy-stack ci sta già lavorando.
Dal 18/8/2026.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 86/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- python
- Ambito
- testing-qa
Direzione di ricerca
Inizia in _pytest/pathlib.py, in compute_module_name, e confronta la gestione delle parti del percorso con module_name_from_path intorno alla riga 796. Verifica il caso del nome file con punti nelle modalità di importazione pertinenti, quindi aggiungi il comportamento di escaping in modo che pkg/foo.test.py venga risolto come pkg.foo_test senza modificare i nomi file senza punti.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.tsconvention 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_namealready produces a valid module name - Maps dotted filenames to a deterministic name (
foo.test.py→foo_test)
Related
- #1426 (closed, "warn about invalid test module names")
- #6422 (closed as invalid, "filename contains more than one dot")
- Lingua principale
- Python
- Stelle
- 14.6k
- Fork
- 3.5k
- Merge medio
- 1g 21h
- PR unite (30g)
- 29
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pytest-dev/pytest
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)Forse già presa @Maurenz2530 l’ha presa 13 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pytest-dev/pytest#15099 · 6 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Expose CaptureManager as public APIForse di nuovo libera @harsh21234i l’ha presa 190 giorni fa e non c’è nessuna pull request aperta. Apertatype: enhancement type: feature-branch
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pytest-dev/pytest#14186 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Docs on warnings filters should note that `ResourceWarning` is often delayedForse già presa @tanishmisra9 l’ha presa 108 giorni fa. Apertatype: docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pytest-dev/pytest#9825 · 7 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
conftest.py: support importorskip at import, make every other outcome exception a usage errorForse già presa @RonnyPfannschmidt l’ha presa 3 giorni fa. Apertatopic: collection
pytest-dev/pytest#15142 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Native TOML numeric faulthandler_timeout is rejectedForse già presa @hfycium l’ha presa 4 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 25/100
pytest-dev/pytest#15132 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pytest-dev/pytest
Issue simili
-
Broken link in index.rstApertadocumentation
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 65/100
ansys/pydpf-core#3547 ·
I maintainer di solito rispondono entro 1 giorno
-
core
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
vectorize-io/hindsight#5457 ·
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: LangChain drops OpenAI Responses text blocks from session recordingForse già presa @ktz03 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
volcengine/OpenViking#5806 ·
I maintainer di solito rispondono entro 1 giorno
-
HTML: <template> content is extracted as document textForse già presa @ryanmeowy l’ha presa oggi. Apertabug html
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
docling-project/docling#4714 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
APIv2 event data accepts a non-string reply and a NaN upper_boundForse già presa @awss1i l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
freedomofpress/securedrop#7946 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni