Skip to content

Return NaN for malformed infinity strings - #123

Merged
zeroSteiner merged 2 commits into
zeroSteiner:masterfrom
fbirds5230:fix/invalid-infinity-strings
Nov 26, 2026
Merged

zeroSteiner merged 2 commits into
zeroSteiner:masterfrom
fbirds5230:fix/invalid-infinity-strings

Conversation

@fbirds5230

@fbirds5230 fbirds5230 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Match the whole infinity string before passing it to Decimal. Invalid values such as information, inf_, and inf_inity now follow the usual NaN path, and to_int reports its normal conversion error.

The existing inf and infinity spellings still work, including negative values, surrounding whitespace, and mixed-case suffixes such as infiNITY. The optional suffix uses ASCII case-insensitive matching so Unicode variants of i cannot reach Decimal.

The tests cover constant strings and values resolved during evaluation.

Validation:

  • All 713 tests pass on Python 3.12.14, 3.13.5, and 3.14.10 with the locked python-dateutil 2.9.0.post0.
  • All 713 tests also pass on Python 3.14.10 with python-dateutil 3.0.0 and 4.0.0.
  • On Python 3.14.10: mypy, the Sphinx HTML build, coverage (98%), and the sdist/wheel build pass.
  • The built wheel installs and passes python -m rule_engine.parser --check on all three Python versions.

Handle invalid Decimal conversions in the infinity branch of string to_flt,
matching the existing result for other nonnumeric strings. Keep valid
infinity spellings and the ordinary number parser unchanged.

Cover literal and resolved values, including the to_int error path.
@@ -301,7 +301,10 @@ def _string_encode(cls, value: str, encoding: str) -> bytes:
def string_to_flt(self, value: str) -> decimal.Decimal:
value = value.strip()
if re.match(r'-?inf', value):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Is there a reason to use exception handling instead of checking the end of the string in the regex?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I used it to preserve Decimal's existing input handling. For example, inf_inity and inf_ already return Infinity; Decimal documents that underscores are removed before parsing. A simple end-anchored inf/infinity pattern would reject those. An anchored regex could preserve them too, but the catch keeps Decimal as the validator and changes only the failed conversions to NaN.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Well the parser doesn't seem to allow inf_inity right now anyways and if it did, that would seem to me like a bug because inf_inity is not documented as a reserved keyword so it would conflict with what should be a valid variable name.

python -m rule_engine.debug_repl                    
rule > inf_inity
SymbolResolutionError: unknown symbol: 'inf_inity'
rule > -inf_inity
SymbolResolutionError: unknown symbol: 'inf_inity'
rule > -inf
result: 
Decimal('-Infinity')
rule > -inf_inity
SymbolResolutionError: unknown symbol: 'inf_inity'
rule > -inf_inity+1
SymbolResolutionError: unknown symbol: 'inf_inity'
rule > -inf_
SymbolResolutionError: unknown symbol: 'inf_'
rule > inf_
SymbolResolutionError: unknown symbol: 'inf_'
rule > inf+1
result: 
Decimal('Infinity')
rule >

The behavior of to_flt should also be consistent. Let's drop the exception handling and match on both the start and end of the string then if the tests pass, I'll get this landed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 3aac90f. to_flt now uses a full match and the exception handler is gone. inf_ and inf_inity return NaN, while the existing infinity spellings, including infiNITY, still work.

I made the optional suffix ASCII case-insensitive: otherwise Python's case folding also accepts Unicode variants of i that Decimal rejects. Those cases are covered along with the underscore variants, for both constant and resolved strings.

All 713 tests pass on Python 3.12, 3.13, and 3.14, including the additional dateutil 3.0.0 and 4.0.0 runs on 3.14.

return decimal.Decimal(value)
except decimal.InvalidOperation:
return decimal.Decimal('nan')
if re.fullmatch(r'-?inf(?ai:inity)?', value):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm going to drop the i flag on inity so infInItY doesn't match any more but I'll do it post merge to avoid more back and forth.

@zeroSteiner
zeroSteiner merged commit a4584a3 into zeroSteiner:master Nov 26, 2026
10 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.

2 participants