-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
Open
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
Description
pprint.isreadable() returns True for float('inf'), float('-inf'), and float('nan'), but their repr() values (inf, -inf, nan) are not valid Python literals — eval() raises NameError. The documented contract of isreadable is that the representation can be used to recreate the value via eval().
Reproducer
import pprint
print(pprint.isreadable(float('inf'))) # True — but eval('inf') raises NameError
print(pprint.isreadable(float('-inf'))) # True
print(pprint.isreadable(float('nan'))) # TrueExpected behavior
isreadable() should return False for inf, -inf, and nan. The docstring states it determines if saferepr(object) is readable by eval(). Since isreadable and saferepr/pformat use the same internal repr, isreadable(x) being True implies eval(pformat(x)) == x should hold — but it doesn't:
import pprint
eval(pprint.pformat(float('inf'))) # NameError: name 'inf' is not definedActual behavior
Returns True for all three, violating the documented contract.
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error