python / python/cpython

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

Aperta
#155,596 1 commento 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

sprint stdlib type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con pprint.pprint(..., expand=True) e riproduci l'esempio dell'issue con width=80 per esaminare come sono disposte le mapping annidate. Il lavoro è completato quando l'output espanso segue il layout annidato proposto, senza interruzioni di riga indesiderate nel terminale e rispettando il vincolo di larghezza.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.