Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
numericise()(used byWorksheet.get_all_records()andCell.numeric_value) no longer turns strings such as"nan","NaN","inf"or"Infinity"intofloat("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'sfloat(), 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 defaultnumericise_ignore, a sheet like this:gives, on master:
Problems this causes:
nan != nan, so equality checks, deduplication and lookups on these records break.update()/append_row()fails before any request is made, withrequests.exceptions.InvalidJSONError: Out of range float values are not JSON compliant.Cell("Nan").numeric_valuereturnsnaninstead ofNone, even though the docstring saysNoneis returned when the value can't be converted to a number.The same applies to overflowing literals such as
"1e999", whichfloat()turns intoinf.How
A small helper,
_finite_float(), callsfloat()and raisesValueErrorwhen the result is not finite (math.isfinite).numericise()uses it in place offloat(), 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,empty2zeroanddefault_blankall behave as before.How tested
UtilsTest.test_numericise_keeps_non_finite_values_as_textcoversnan/inf/infinitywith mixed case and signs,1e999,numericise_all, and checks that finite floats are still converted.CellNumericValueTest.test_numeric_value_of_non_finite_text_is_nonecoversCell.numeric_value.tox -e lint,py,build,doc: all pass (168 tests). The full suite also passes on Python 3.8.20. The CIbanditcommand 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.