python / python/cpython

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

Đang mở
#155,596 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

sprint stdlib type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu với pprint.pprint(..., expand=True) và tái hiện ví dụ của issue ở width=80 để kiểm tra cách các mapping lồng nhau được bố trí. Được xem là hoàn thành khi đầu ra đã mở rộng tuân theo bố cục lồng nhau được đề xuất, không có hiện tượng xuống dòng không mong muốn trong terminal và vẫn tuân thủ giới hạn độ rộng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.