You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PEP 813: Implement !p pretty-print conversion for f-strings
Add the !p conversion flag for f-strings that calls sys.__prettyhook__
(defaulting to pprint.pformat) on the value. When a callable is provided
as a format spec (e.g. f"{obj!p:my_formatter}"), the format spec is
evaluated as a Python expression and called on the value instead.
Key changes:
- Tokenizer: When !p: is detected inside an f-string expression, keep
expression tokenization mode instead of switching to format-spec mode,
allowing the format spec to be parsed as a Python expression.
- Grammar: Add fstring_pretty_conversion rule and two new alternatives
in fstring_replacement_field for !p with and without callable.
- Parser: Add _PyPegen_check_pretty_conversion() to validate the !p
conversion, and _PyPegen_pretty_formatted_value() to build the AST.
- Codegen: Handle 'p' conversion - without callable uses CONVERT_VALUE
with new FVC_PRETTY opcode arg; with callable emits CALL bytecode.
- Runtime: Add _PyObject_Pretty() conversion function that looks up
sys.__prettyhook__ and calls it on the object.
- sys module: Add sys.prettyhook (default: pprint.pformat) and
sys.__prettyhook__ backup copy following the existing hook pattern.
https://claude.ai/code/session_01M64NyGDjnkVvSmDRmJFF64
0 commit comments