Subclassing PrettyPrinter.format doesn't work in all cases
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 36k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
See also the earlier fixed https://github.com/python/cpython/issues/73036. This appears to have only been a partial fix, or it has regressed.
import collections
import pprint
import types
SENTINAL = object()
class _hashable:
def __hash__(self):
return 1
HASHABLE_SENTINAL = _hashable()
class CustomPrettyPrinter(pprint.PrettyPrinter):
def format(self, obj, context, maxlevels, level):
if obj is SENTINAL:
return "SENTINAL", True, False
elif obj is HASHABLE_SENTINAL:
return "HASHABLE_SENTINAL", True, False
else:
return super().format(obj, context, maxlevels, level)
if __name__ == "__main__":
printer = CustomPrettyPrinter(sort_dicts=False)
test_data = {
"item": SENTINAL,
"dict": {"a": SENTINAL},
"dict_key": {HASHABLE_SENTINAL: 1},
"OrderedDict": collections.OrderedDict({"a": SENTINAL}),
"OrderedDict_key": collections.OrderedDict({HASHABLE_SENTINAL: 1}),
"list": [SENTINAL],
"tuple": (SENTINAL,),
"set": {SENTINAL},
"frozenset": frozenset({SENTINAL}),
"mappingproxy": types.MappingProxyType({"a": SENTINAL}),
"mappingproxy_key": types.MappingProxyType({HASHABLE_SENTINAL: 1}),
"SimpleNamespace": types.SimpleNamespace(a=SENTINAL),
"defaultdict": collections.defaultdict(list, {"a": SENTINAL}),
"defaultdict_key": collections.defaultdict(list, {HASHABLE_SENTINAL: 1}),
"Counter": collections.Counter({"a": SENTINAL}),
"Counter_key": collections.Counter({HASHABLE_SENTINAL: 1}),
"deque": collections.deque([SENTINAL]),
}
printer.pprint(test_data)
This outputs:
{'item': SENTINAL,
'dict': {'a': SENTINAL},
'dict_key': {HASHABLE_SENTINAL: 1},
'OrderedDict': OrderedDict({'a': <object object at 0x1041f4660>}),
'OrderedDict_key': OrderedDict([(HASHABLE_SENTINAL, 1)]),
'list': [SENTINAL],
'tuple': (SENTINAL,),
'set': {<object object at 0x1041f4660>},
'frozenset': frozenset({<object object at 0x1041f4660>}),
'mappingproxy': mappingproxy({'a': <object object at 0x1041f4660>}),
'mappingproxy_key': mappingproxy({HASHABLE_SENTINAL: 1}),
'SimpleNamespace': namespace(a=<object object at 0x1041f4660>),
'defaultdict': defaultdict(<class 'list'>,
{'a': SENTINAL}),
'defaultdict_key': defaultdict(<class 'list'>,
{HASHABLE_SENTINAL: 1}),
'Counter': Counter({'a': <object object at 0x1041f4660>}),
'Counter_key': Counter({<__main__._hashable object at 0x1045e4d70>: 1}),
'deque': deque([<object object at 0x1041f4660>])}
Every instance of < in that output is a bug.
CPython versions tested on:
3.13
Operating systems tested on:
macOS
Linked PRs
- gh-133238
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo il reproducer e ispezionando pprint.PrettyPrinter.format, in particolare il modo in cui gestisce i tipi di contenitore elencati e il loro contenuto. Confronta ogni output con la formattazione personalizzata prevista, quindi aggiungi la copertura di regressione per i casi in cui l’override viene aggirato. Il lavoro è completo quando ogni o repr mostrato dal reproducer rispetta il metodo format della sottoclasse.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 25/100