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
分支
36k
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 摘要。