gh-145117: fix pprint.isreadable() for non-finite float and complex values#145120
Open
zetzschest wants to merge 7 commits intopython:mainfrom
Open
gh-145117: fix pprint.isreadable() for non-finite float and complex values#145120zetzschest wants to merge 7 commits intopython:mainfrom
zetzschest wants to merge 7 commits intopython:mainfrom
Conversation
inf, -inf, nan repr strings are not valid Python literals so eval() fails on them. Check repr string inside _builtin_scalars branch instead of using math.isinf/isnan.
skirpichev
reviewed
Feb 23, 2026
…hub.com/zetzschest/cpython into fix/pprint_isreadable_float_specials_v2
…ex non-finite values
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.
Issue
isreadableis documented as returningTrueif the object's repr is readable by the interpreter — implyingeval(pformat(obj)) == obj. For non-finite float and complex values,isreadablereturnsTrue, but the roundtrip fails —evalraisesNameErrorsinceinfandnanaren't valid Python expressions.Reproducer
Fix
Two options: update the documentation to weaken the guarantee so
isreadableno longer implies the eval roundtrip, or fix the implementation so non-finite float and complex values are correctly marked as not readable. We think the latter is the right call — the documented contract is reasonable and worth preserving. In_safe_repr, usecmath.isfinite()to catch both float and complex non-finite values: