Assertion diff doesn't pretty-print the built-in frozen dict (Python 3.15)
维护者通常 1 天内回复
评估
调研方向
修复应当放在 src/_pytest/_io/pprint.py 中。阅读 PrettyPrinter._dispatch 和已有的 _pprint_ordered_dict 处理函数,然后添加一个 frozendict 处理函数,仅在 frozendict 类型存在时才注册,以确保旧版 Python 不受影响。使用 issue 中由两个 frozendict 组成的列表,在 pytest -vv 下复现问题。完成标准是:Full diff 中每个 frozendict 的键各占一行,被修改的 age 值被标记,并且覆盖新处理函数的测试通过。
由索引模型根据 Issue 内容生成。
描述
Python 3.15 added frozendict as a built-in type (PEP 814). When an assertion
comparing frozendicts fails with -vv, the "Full diff" prints each frozendict on
a single line. A dict with the same contents gets one key per line, which makes
the changed value easy to spot. With frozendict the change is buried mid-line.
def test_frozendict():
left = [frozendict(name='alice', age=30, city='Lagos'), frozendict(name='bob', age=25, city='Abuja')]
right = [frozendict(name='alice', age=30, city='Lagos'), frozendict(name='bob', age=26, city='Abuja')]
assert left == right
Output with pytest -vv:
Full diff:
[
frozendict({'name': 'alice', 'age': 30, 'city': 'Lagos'}),
- frozendict({'name': 'bob', 'age': 26, 'city': 'Abuja'}),
? ^
+ frozendict({'name': 'bob', 'age': 25, 'city': 'Abuja'}),
? ^
]
The same test using dict:
Full diff:
[
{
'name': 'alice',
'age': 30,
'city': 'Lagos',
},
{
'name': 'bob',
- 'age': 26,
? ^
+ 'age': 25,
? ^
'city': 'Abuja',
},
]
I think the cause is in src/_pytest/_io/pprint.py. PrettyPrinter._dispatch
has handlers for dict, OrderedDict, defaultdict, MappingProxyType and
others, but not frozendict, so it falls back to a one-line repr(). The
standard library's pprint in 3.15 already supports frozendict.
A possible fix is a _pprint_frozendict handler similar to _pprint_ordered_dict,
registered only when frozendict exists, so older Python versions aren't affected.
Versions
pytest 9.1.1, Python 3.15.0, Windows 11. Also seen on pytest main with Python 3.15 on Linux.
pip list
Package Version
--------- -------
colorama 0.4.6
iniconfig 2.3.1
packaging 26.3
pip 26.2.1
pluggy 1.6.0
Pygments 2.21.0
pytest 9.1.1
I'm happy to open a PR for this.
- 主要语言
- Python
- 星标
- 14.6k
- 派生
- 3.5k
- 平均合并
- 3 天 18 小时
- 30 天内合并 PR
- 27
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest 的其他 Issue
-
Docs build fails with Pluggy 1.7.0 on an unresolved private DistFacade reference可能已有人在做 @Lothnic 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
pytest-dev/pytest#15190 ·
维护者通常 1 天内回复
-
`faulthandler_timeout` stops covering a test once setup, call or a subtest fails (`pytest_exception_interact` cancels the timer)可能已有人在做 @XXXXXQ-0206 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15180 · 1 条评论 ·
维护者通常 1 天内回复
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)可能已有人在做 @hardikkaurani 于 21 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
pytest-dev/pytest#15099 · 6 条评论 ·
维护者通常 1 天内回复
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`可能重新可做 @siddy-stack 于 59 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 86/100
pytest-dev/pytest#14514 · 6 条评论 ·
维护者通常 1 天内回复
-
Expose CaptureManager as public API可能重新可做 @harsh21234i 于 197 天前认领,目前没有进行中的 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 小时 新手友好度 75/100
common-workflow-language/cwl-utils#477 ·
维护者通常 1 天内回复
-
Add dependabot未关闭feat
难度 2/5 1 小时以内 新手友好度 72/100
open-nudge/opentemplate#204 ·
维护者通常 1 天内回复
-
github-stale-ci-pr-closer: KV state is double-wrapped, never persists, and hits the depth-32 limit可能已有人在做 @VascoSch92 今天认领。 未关闭priority:medium ready-for-dev
难度 2/5 1-3 小时 新手友好度 72/100
OpenHands/extensions#781 ·
维护者通常 1 天内回复
-
bug good first issue
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
curriculum documentation quality
难度 2/5 1-3 小时 新手友好度 78/100
githubnext/gh-aw-workshop#4638 ·
维护者通常 1 天内回复