Skip to content

pprint.isreadable() returns True for float('inf'), float('-inf'), float('nan') #145117

@zetzschest

Description

@zetzschest

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')))   # True

Expected 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 defined

Actual 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions