Skip to content

Generate non-finite float defaults as valid Python source - #212

Merged
horejsek merged 1 commit into
horejsek:masterfrom
Sanjays2402:fix/non-finite-float-defaults
Aug 9, 2026
Merged

Generate non-finite float defaults as valid Python source#212
horejsek merged 1 commit into
horejsek:masterfrom
Sanjays2402:fix/non-finite-float-defaults

Conversation

@Sanjays2402

Copy link
Copy Markdown
Contributor

Fixes #101

A schema default of NaN or infinity was written into the generated validation function with repr(), which emits the bare names nan / inf / -inf. Those names don't exist in the generated module's globals, so compiling such a schema produced a function that raised NameError: name 'nan' is not defined the moment the default was applied.

Added repr_default(), which renders non-finite floats as float('nan') and recurses into lists/tuples/dicts for nested defaults; everything else still goes through plain repr(). It's also used for the definition= argument of the generated exceptions, which had the same leak.

Regression tests in tests/test_default.py fail with NameError on master and pass with the fix; full suite is green (503 passed, 4 skipped).

A schema default of NaN or infinity was written into the generated
validation function with repr(), which produces the bare names `nan`,
`inf` and `-inf`. Those names do not exist in the generated module's
global state, so compiling such a schema produced a function that raised
`NameError: name 'nan' is not defined` as soon as the default was applied.

Added `repr_default()`, which renders non-finite floats as `float('nan')`
and recurses into lists, tuples and dicts so nested defaults are handled
too; everything else still goes through plain repr(). It is used for both
the default-assignment code and the `definition=` argument of the
generated exceptions, which had the same leak.

Fixes horejsek#101
@horejsek
horejsek merged commit c13e524 into horejsek:master Aug 9, 2026
17 checks passed
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.

Nameerror when I use numpy.nan as default

2 participants