Repository navigation
fix: treat a float NaN as absent, the same as None (#367) - #381
Conversation
Two identical NaNs scored 0.0 and counted as a false discovery, because NaN equals nothing under IEEE 754. NaN is how pandas and numpy spell a missing number, so read it as absent everywhere absence is decided: NullHelper (and its _maybe_absent guard), FieldHelper.is_null_value, and BaseComparator.compare's shared None policy. NaN vs NaN or None is now TN/1.0, NaN vs a number FA or FN. A Decimal NaN is unchanged and still matches nothing.
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Top 2 HotspotsFiles with the highest number of security findings:
Detailed FindingsShow 4 actionable findingsFinding 1: SECRET-HEX-HIGH-ENTROPY-STRING
Description: Code Snippet: Finding 2: B108
Description: Code Snippet: Finding 3: B307
Description: Code Snippet: Finding 4: B307
Description: Code Snippet: Report generated by Automated Security Helper (ASH) at 2026-10-09T23:15:36+00:00 |
digalaviz
left a comment
There was a problem hiding this comment.
LGTM.
What I verified:
- Two isolated clones, baseline
dev@1479c3d4and this PR @6a259d80, each
installed withuv sync --frozen(the CI command). Both import their own tree —
worth stating because__version__reads1.0.0on both, so it cannot tell them
apart; I usedhasattr(NullHelper, 'is_nan')as the discriminator instead. - Full suite green on both: baseline
2769 passed, 12 skipped, 1 xfailed, candidate
2779 passed, 12 skipped, 1 xfailed. +10 passed, zero new failures, skip/xfail
counts unchanged. CI is green on6a259d80across all seven checks too, which
matches what I measured locally. - No dependency surface touched
pyproject.toml,uv.lockand the requirements
are untouched in the diff,uv lock --checkpasses, and both trees resolve the
same 169 packages. So the two environments are directly comparable. - No security findings. Nothing added uses eval/exec/pickle/subprocess, no new
imports beyond stdlibmath, andis_nanis safe on every input type I threw at
it (None, str, list, Decimal, numpy scalars, pydantic models) because the
isinstanceguard short-circuits.
One non-blocking note on null_helper.py, below. Nothing that should hold the merge.
| Returns: | ||
| True if the value is a float (including ``numpy.float64``) NaN | ||
| """ | ||
| return isinstance(val, float) and math.isnan(val) |
There was a problem hiding this comment.
Non-blocking, and you already documented this at L40 ("including numpy.float64"),
so this is a question about intent rather than a defect.
isinstance(val, float) catches a Python float and np.float64 (a float subclass),
but not np.float32, np.float16, or np.longdouble. Measured:
| value | baseline 1479c3d4 |
this PR 6a259d80 |
|---|---|---|
NumericComparator().compare(v, v), py-float / float64 NaN |
0.0 | 1.0 |
same, np.float32 / np.float16 NaN |
0.0 | 0.0 |
So the same missing value scores differently by dtype width, where the baseline was
uniformly 0.0.
Two things keep this low rather than blocking, and I want to be straight about both:
The normal path is fine. On a field annotated Optional[float], pydantic coerces
every numpy width to a Python float, so all four widths score 1.0 with a true
negative on your branch (all 0.0 with a false discovery on dev). A pandas float32
column bound to a typed float field is fully fixed. The split is only reachable via a
direct comparator call or a field annotated Any.
And the unfixed widths sit exactly at the old behaviour, so this is an incomplete fix
rather than a regression. Nothing got worse.
The part I think is worth acting on regardless: test_numpy_nan_is_absent exercises
np.float64 through a typed Optional[float] field, so pydantic coercion carries it
— the test would pass identically with no numpy handling at all. It doesn't currently
pin the numpy support it's named for. A case going through a direct comparator call or
an Any field would.
If you do want to widen it, concrete types read better here than an ABC:
return isinstance(val, (float, np.floating)) and math.isnan(val)That needs an import numpy as np at the top — this module currently imports only
math and typing, which is why I'm not leaving it as a one-click suggestion.
I tried numbers.Real first and talked myself out of it: it admits int, bool and
Fraction, which can never be NaN, so a reader of a function named is_nan has to
work out why integers are in scope — and it still excludes Decimal('NaN'), since
Decimal isn't registered as numbers.Real. It looks more general than it is, which
is worse to maintain than your explicit float64-only scope. (Cost is not the reason:
I measured ~184ns vs ~38ns per call, which is nothing at field-per-case volume.)
Equally fine to declare float64-only the intended scope and just tighten the test. The
test gap is the part I'd act on; the predicate is your call.
Measured on two isolated clones, baseline dev @ 1479c3d4 and this PR @ 6a259d80,
each importing its own tree — __version__ is 1.0.0 on both, so I keyed off
hasattr(NullHelper, 'is_nan') instead.
digalaviz
left a comment
There was a problem hiding this comment.
Approving. Raised eight candidate findings adversarially; seven turned out to be pre-existing behaviour on dev, one stands as the non-blocking note above
Resolve the conflicts with #381 (NaN as absent): both changes extend the same null rules, so NullHelper.is_effectively_null_for_primitives and the _maybe_absent guard now treat NaN and empty tuples/sets as absent, and the null rule in classification-logic.md and the CHANGELOG keep both entries.
Issue Reference
Fixes #367
Description of Changes
A float
NaNis now treated as absent, the same asNone. Two identicalNaNs used to score0.0and count as a false discovery.Changes Made
NullHelper.is_nan()added;is_effectively_null_for_primitives, its_maybe_absentguard, andFieldHelper.is_null_valueall treat a floatNaNas null.BaseComparator.compare()'s sharedNonepolicy also treats a floatNaNas missing, so list elements and direct comparator calls agree with the field path.classification-logic.md) and a CHANGELOGFixedentry.NaNvsNaNNaNvsNoneNaNvs5.05.0vsNaNWhy These Changes Are Needed
NaNis how pandas and numpy spell a missing number, and it isn't valid JSON, so it's not a value an extractor is asked to produce. #367 names "absent" and "not a value" as the two readings. This PR takes "absent", which matches how stickler already treats""and{}.Testing
test_nan_as_absent.py(9 of its 10 tests fail ondev; the 10th pins unchanged Decimal behaviour)test_nan_does_not_crashflipped totest_nan_reads_as_absent;NaNadded to the_maybe_absentsuperset testcompare_with,eval_for, direct comparator,List[StructuredModel]dev@1479c3d)Reviewers
@sromoam
Checklist
CHANGELOG.mdentry added under[Unreleased]Additional Notes
Num542f724. That guard still stops theInvalidOperationcrash; a floatNaN`just no longer reaches it.NaN(FD becomes TN/FA/FN),NaNis absent (numpy floats included). ADecimal('NaN')still matches nothing.base.pychange applies to every comparator, not just numeric.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.