python / python/cpython

`pprint(..., expand=True)`'s "best effort" for adhering to `width=...` seems not very "best"

未关闭
#155,596 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

sprint stdlib type-feature
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Proposal:

was playing around with this both for a pytest improvement and for a video -- and noticed that the width setting doesn't seem to play all that well with expand=True -- perhaps the width calculation is actually based on the old algorithm? (haven't looked closely yet)

import pprint

obj = {'a' * 12: 1, 'b' * 20: 2, 'c' * 30: {'d' * 5: 3, 'e' * 40: 3}}

pprint.pprint(obj)
print('===')
pprint.pprint(obj, expand=True)

this is the current output on an 80 character terminal (I've added newlines where natural wrapping occurs in the terminal)

$ python3.15 t.py 
{'aaaaaaaaaaaa': 1,
 'bbbbbbbbbbbbbbbbbbbb': 2,
 'cccccccccccccccccccccccccccccc': {'ddddd': 3,
                                    'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee': 
3}}
===
{
 'aaaaaaaaaaaa': 1,
 'bbbbbbbbbbbbbbbbbbbb': 2,
 'cccccccccccccccccccccccccccccc': {'ddddd': 3, 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
eeeeeeee': 3},
}

it makes sense to me that the old format doesn't line break properly (it wouldn't fit with the 36-space (!) indent -- but I believe the new format should be able to do better -- something similar to:

{
 'aaaaaaaaaaaa': 1,
 'bbbbbbbbbbbbbbbbbbbb': 2,
 'cccccccccccccccccccccccccccccc': {
  'ddddd': 3,
  'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee': 3,
 },
}

(which is well within the width=80 constraint)

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

I believe this is also related to #149189 -- and perhaps would strengthen the ideas in that issue if resolved

Linked PRs
  • gh-155926
  • gh-157684

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 pprint.pprint(..., expand=True) 开始,并在 width=80 时重现 issue 中的示例,以检查嵌套 mapping 的布局方式。当展开后的输出遵循提议的嵌套布局、没有不必要的终端换行,并且遵守宽度限制时,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
tooling
Issue 类型
功能
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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