Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Assertion diff doesn't pretty-print the built-in frozen dict (Python 3.15)

未关闭 适合新手
#15,172 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@HakeemTheEmperor 已经在做这个了。

开始于 2026年10月13日。

  • #15173 来自 @HakeemTheEmperor —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
testing

调研方向

修复应当放在 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

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

pytest-dev/pytest 的其他 Issue

查看 pytest-dev/pytest 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。