Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`
维护者通常 1 天内回复
@siddy-stack 已经在做这个了。
开始于 2026年8月18日。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 86/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- python
- 领域
- testing-qa
调研方向
从 _pytest/pathlib.py 中的 compute_module_name 开始,将其对路径部分的处理与第 796 行附近的 module_name_from_path 进行比较。在相关的导入模式下验证带点文件名的情况,然后添加转义行为,使 pkg/foo.test.py 解析为 pkg.foo_test,同时不改变不带点的文件名。
由索引模型根据 Issue 内容生成。
描述
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")
- 主要语言
- Python
- 星标
- 14.6k
- 派生
- 3.5k
- 平均合并
- 1 天 17 小时
- 30 天内合并 PR
- 25
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest 的其他 Issue
-
Subtest durations become negative when `time` is mocked (subtests.py bypasses `_pytest.timing`)可能已有人在做 @RonnyPfannschmidt 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
pytest-dev/pytest#15201 ·
维护者通常 1 天内回复
-
`faulthandler_timeout` stops covering a test once setup, call or a subtest fails (`pytest_exception_interact` cancels the timer)可能已有人在做 @XXXXXQ-0206 于 3 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15180 · 1 条评论 ·
维护者通常 1 天内回复
-
Assertion diff doesn't pretty-print the built-in frozen dict (Python 3.15)可能已有人在做 @HakeemTheEmperor 于 6 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15172 ·
维护者通常 1 天内回复
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)可能已有人在做 @hardikkaurani 于 24 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15099 · 6 条评论 ·
维护者通常 1 天内回复
-
Expose CaptureManager as public API可能重新可做 @harsh21234i 于 200 天前认领,目前没有进行中的 PR。 未关闭type: enhancement type: feature-branch
难度 2/5 1-3 小时 新手友好度 68/100
pytest-dev/pytest#14186 · 2 条评论 ·
维护者通常 1 天内回复
查看 pytest-dev/pytest 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
google-ai-edge/litert-torch#1346 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
geneontology/gocam-py#272 ·
-
pushed_authorization_request sends list authorization_details as a Python repr可能已有人在做 @RaphaelFakhri 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
auth0/auth0-python#903 ·
维护者通常 1 天内回复