fix: parse_float(bool/int) and parse_int(float-string) return wrong results#578
Open
gaoflow wants to merge 1 commit into
Open
fix: parse_float(bool/int) and parse_int(float-string) return wrong results#578gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
…s to int
- `get_float(True)` wrongly returned `0.0` (the default) because `bool`
failed the `is_float` type-check, `str(True)` produced `"True"`, and
`float("True")` raised `ValueError`. Now `parse_float` short-circuits
on any `bool`/`int` value and returns `float(val)` directly, so
`get_float(True)` → `1.0` and `get_float(False)` → `0.0`.
- `get_int("3.5")` wrongly returned the caller-supplied default instead
of `3` because `int("3.5")` raises `ValueError`. `_parse_int` now
falls back to `int(float(val))` so numeric strings with a fractional
part are truncated correctly.
- `get_int(True)` returned `True` (a `bool`) rather than the `int` `1`
because `isinstance(True, int)` is `True` and `_parse_with` returned
the value unchanged. `parse_int` now explicitly casts `bool` inputs
with `int(val)` before any further processing.
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
Three related bugs in
parse_float/parse_intthat cause silent wrong results:get_float(True)0.01.0get_float(False)0.00.0get_int(True)True(bool)1(int)get_int(False)False(bool)0(int)get_int("3.5")0(default)3Root causes
parse_floatwithbool/intinput:is_float(True)isFalse(bool is not a float subclass), so_parse_withfalls through tostr(True)→"True"→float("True")raisesValueError→None→ caller's default (0.0).parse_intwithboolinput:is_integer(True)isTrue(bool is an int subclass), so_parse_withshort-circuits and returnsTrueunchanged — abool, not anint.parse_intwith a float-string:int("3.5")raisesValueErrorwith no fallback, so numeric strings with a fractional part always fall through to the caller's default.Fix
All 756 existing tests pass with the corrected expectations.