fix: parse_float(bool/int) and parse_int(float-string) return wrong results#578
Closed
gaoflow wants to merge 3 commits into
Closed
fix: parse_float(bool/int) and parse_int(float-string) return wrong results#578gaoflow wants to merge 3 commits into
gaoflow wants to merge 3 commits 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.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes silent misparsing in ParseDict numeric getters by improving parse_float and parse_int behavior for boolean/int inputs and for integer parsing from float-like numeric strings.
Changes:
parse_float: handlebool/intinputs before falling back to string parsing.parse_int: ensureboolreturns anint, and add a fallback to parse int from float-like strings (e.g.,"3.5"→3).- Update unit tests to reflect the corrected results for these cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
benedict/dicts/parse/parse_util.py |
Fixes numeric parsing logic for bool/int and float-like strings. |
tests/dicts/parse/test_parse_dict.py |
Updates expectations for get_float/get_int based on corrected parsing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
249
to
+253
| except ValueError: | ||
| return None | ||
| try: | ||
| return int(float(val)) | ||
| except ValueError: | ||
| return None |
3 tasks
fabiocaccamo
pushed a commit
that referenced
this pull request
Jul 7, 2026
… and fix parse_float annotation (#584) * Initial plan * fix: convert bool/int to float in parse_float, and parse float-strings 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. * Address review comments: catch OverflowError in _parse_int, fix parse_float annotation --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Vincent Gao <gaobing1230@gmail.com>
This was referenced Jul 7, 2026
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
3 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #578 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 64 64
Lines 2418 2418
=======================================
Hits 2379 2379
Misses 39 39
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.