Repository navigation
PEP 813: sorting and None printer fixes, and a lazily loaded protocol module - #5
Merged
ericvsmith merged 2 commits intoOct 23, 2026
Conversation
…ault
Sorting a sortable layout that contains keyword items failed with
TypeError. Keyword items are now sorted by name after the other items,
so the output stays in call order, as in Example('y', 'z', a=10, b=0).
PyObject_Pretty() now treats a printer of None like NULL and uses the
default printer. So f'{obj!p:{None}}' matches f'{obj!p}' and
string.templatelib.convert(obj, 'p', None), where None already meant
that no printer was given.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cycles The protocol code lived inside _load_protocol(), which a module __getattr__ ran on first use and then copied into the module globals. It now lives at module level in a new private module, _pprint_protocol, and pprint re-exports its names with lazy imports. Importing pprint alone still does not import dataclasses or the protocol module. The __pprint__() methods in collections, _collections_abc and dataclasses imported helpers from pprint inside the function to avoid import cycles. The helpers are now inlined into those methods, and the names they need come from the C _pprint module with lazy imports, so there is no cycle even when lazy imports are forced eager. pprint's own function-local imports of dataclasses and re are now lazy imports at the top of the module. string.templatelib.convert() no longer special-cases a printer of None, since PyObject_Pretty() handles it. Regenerate stdlib_module_names.h, which also adds the missing _pprint entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes found while comparing the implementation with the PEP, plus a restructuring of how the protocol code is loaded.
Keyword sorting. A sortable layout with keyword items failed with
TypeError: unexpected document node. Keyword items are now sorted by name, after the positional and key/value items, so the output stays valid call syntax:Example('y', 'z', a=10, b=0), neverExample(a=10, 'z').None printer.
convert(1, 'p', None)used the default printer, butf'{1!p:{None}}'raisedTypeError: 'NoneType' object is not callable.PyObject_Pretty()now treatsNonelikeNULL, so all three spellings agree.Interpolation.printeralready usesNoneto mean that no printer was given. The C API docs are updated, andconvert()no longer special-casesNone._pprint_protocol._load_protocol()and the module__getattr__/__dir__are gone. The Python half of the protocol (Printer,decompose,Keyword,Entry,Expand,LEAF,DescriptionTypeand the renderer) now lives at module level in a new private module,Lib/_pprint_protocol.py.pprintre-exports it withlazy from _pprint_protocol import ..., soimport pprintalone still imports neither it nordataclasses, and upstream'stest_pprint.test_lazy_importpasses unchanged. The C names still come from_pprint.pprint's own function-local imports ofdataclassesandreare now lazy imports at the top.Import cycles. The
__pprint__()methods incollections,_collections_abcanddataclassesimported helpers frompprintinside the function. Those helpers are now inlined into the methods, which takeLayout,KWARGS,ITEMSandDELEGATEfrom the C_pprintmodule with lazy imports. Nothing importspprintto describe a built-in type any more, sotest_lazy_import's forced-eager mode works.Python/stdlib_module_names.his regenerated, which also adds the missing_pprintentry.Tests: test__pprint, test_pprint, test_fstring, test_string, test_tstring, test_dataclasses, test_capi, test_collections, test___all__, test_lazy_import, test_embed, test_site, test_typing, test_enum, test_inspect, test_pydoc and test_sys pass.
🤖 Generated with Claude Code