Skip to content

Keep "nan" and "inf" strings as text in numericise - #1610

Open
vidigoat wants to merge 1 commit into
burnash:masterfrom
vidigoat:fix/numericise-nan-infinity
Open

vidigoat wants to merge 1 commit into
burnash:masterfrom
vidigoat:fix/numericise-nan-infinity

Conversation

@vidigoat

Copy link
Copy Markdown

What

numericise() (used by Worksheet.get_all_records() and Cell.numeric_value) no longer turns strings such as "nan", "NaN", "inf" or "Infinity" into float("nan") / float("inf"). They are returned unchanged, like any other non-numeric text. Finite numbers are converted exactly as before.

Why

There is no existing issue for this, so here is the bug in detail.

numericise() falls back to Python's float(), which also accepts "nan", "inf" and "infinity" in any case and with an optional sign. Google Sheets has no NaN or infinite numbers. A cell showing one of those words holds text. With the default numericise_ignore, a sheet like this:

name score
Nan Infinity
Ada 3.5

gives, on master:

>>> ws.get_all_records()
[{'name': nan, 'score': inf}, {'name': 'Ada', 'score': 3.5}]

Problems this causes:

  • Text gets changed silently. "Nan" is a real first name, and "Infinity" or "inf" can show up as a label or a product name.
  • nan != nan, so equality checks, deduplication and lookups on these records break.
  • The values can't be written back. Sending a record to update() / append_row() fails before any request is made, with requests.exceptions.InvalidJSONError: Out of range float values are not JSON compliant.
  • Cell("Nan").numeric_value returns nan instead of None, even though the docstring says None is returned when the value can't be converted to a number.

The same applies to overflowing literals such as "1e999", which float() turns into inf.

How

A small helper, _finite_float(), calls float() and raises ValueError when the result is not finite (math.isfinite). numericise() uses it in place of float(), so a non-finite result goes through the existing "not a number" path. The function's complexity stays within the flake8 limit, and nothing else about numericising changes: ints, comma-grouped numbers, underscores, empty2zero and default_blank all behave as before.

How tested

  • New UtilsTest.test_numericise_keeps_non_finite_values_as_text covers nan/inf/infinity with mixed case and signs, 1e999, numericise_all, and checks that finite floats are still converted.
  • New offline CellNumericValueTest.test_numeric_value_of_non_finite_text_is_none covers Cell.numeric_value.
  • Both tests fail on master (15 failing checks, counting subtests) and pass with this change. No new HTTP calls, so no cassettes are needed.
  • tox -e lint,py,build,doc: all pass (168 tests). The full suite also passes on Python 3.8.20. The CI bandit command reports no issues.

Developed with Claude Code (AI) under my direction; the new test fails on master and passes with this change, and tox lint/py pass locally.

numericise() falls back to float(), which also parses "nan", "inf" and
"infinity" in any case and with an optional sign. Google Sheets has no
NaN or infinite numbers and stores such cell contents as text, so
get_all_records() turned a name like "Nan" into float("nan") and
"Infinity" into float("inf"). Cell.numeric_value had the same problem.
NaN compares unequal to itself, and neither value can be sent back to
the API because they are not valid JSON.

Only convert to float when the result is finite; otherwise the value is
returned unchanged, like any other non-numeric string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant