Skip to content

pprint.isreadable returns True for non-eval-able float and complex specials #145117

@zetzschest

Description

@zetzschest

Issue

isreadable is documented as returning True if the object's repr is readable by the interpreter — implying eval(pformat(obj)) == obj. For float and complex non-finite values, isreadable returns True, but the roundtrip fails — eval raises NameError since inf and nan aren't valid Python expressions.

Reproducer

import pprint

print(pprint.isreadable(float('inf')))        # True — but eval('inf') raises NameError
print(pprint.isreadable(float('nan')))        # True
print(pprint.isreadable(complex(float('inf'), 0)))  # True

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