Repository navigation
Return NaN for malformed infinity strings - #123
Conversation
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): | |||
There was a problem hiding this comment.
Is there a reason to use exception handling instead of checking the end of the string in the regex?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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.
Match the whole infinity string before passing it to
Decimal. Invalid values such asinformation,inf_, andinf_initynow follow the usual NaN path, andto_intreports its normal conversion error.The existing
infandinfinityspellings still work, including negative values, surrounding whitespace, and mixed-case suffixes such asinfiNITY. The optional suffix uses ASCII case-insensitive matching so Unicode variants oficannot reachDecimal.The tests cover constant strings and values resolved during evaluation.
Validation:
python -m rule_engine.parser --checkon all three Python versions.