Skip to content

Comments

gh-145117: Fix pprint.isreadable() returning True for inf/nan#145116

Closed
zetzschest wants to merge 2 commits intopython:mainfrom
zetzschest:fix/pprint_isreadable_float_specials
Closed

gh-145117: Fix pprint.isreadable() returning True for inf/nan#145116
zetzschest wants to merge 2 commits intopython:mainfrom
zetzschest:fix/pprint_isreadable_float_specials

Conversation

@zetzschest
Copy link

@zetzschest zetzschest commented Feb 22, 2026

Problem

pprint.isreadable() returns True for float('inf'), float('-inf'), and float('nan'), but their repr() values 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

Fix

In Lib/pprint.py, in _safe_repr, add a check for float specials and return readable=False for them.

@bedevere-app
Copy link

bedevere-app bot commented Feb 22, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@python-cla-bot
Copy link

python-cla-bot bot commented Feb 22, 2026

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@zetzschest zetzschest changed the title gh-XXXXX: Fix pprint.isreadable() returning True for inf/nan gh-145117: Fix pprint.isreadable() returning True for inf/nan Feb 22, 2026
Lib/pprint.py Outdated
typ = type(object)
if typ is float:
import math
if math.isinf(object) or math.isnan(object):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not math.isfinite() would be equivalent

@zetzschest zetzschest force-pushed the fix/pprint_isreadable_float_specials branch from 6fabca6 to 8d2e450 Compare February 22, 2026 15:44
@bedevere-app
Copy link

bedevere-app bot commented Feb 22, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible alternative though not necessarily better I think:

  • Move the typ is float check in the _builtin_scalars branch.
  • Compute the repr(object)
  • Compare with -inf/inf/nan as strings. That could save an import here though it's not that costly.

However, please add tests.

inf, -inf, nan are not valid Python literals so eval() fails on them.
isreadable() should return False for these values.
@zetzschest zetzschest force-pushed the fix/pprint_isreadable_float_specials branch from 227a709 to 672b0f2 Compare February 22, 2026 15:50
@bedevere-app
Copy link

bedevere-app bot commented Feb 22, 2026

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@picnixz
Copy link
Member

picnixz commented Feb 22, 2026

Btw, please stop force-pushing (see our devguide: https://devguide.python.org/getting-started/pull-request-lifecycle/#don-t-force-push)

@zetzschest zetzschest closed this Feb 22, 2026
@zetzschest
Copy link
Author

Created a fresh PR (#145120) to avoid force-pushing. Apologies for the noise.

@zetzschest zetzschest deleted the fix/pprint_isreadable_float_specials branch February 22, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants