fix(python): make _float_value total on malformed numeric input - #2113
Open
VaggelisGian wants to merge 1 commit into
Open
fix(python): make _float_value total on malformed numeric input#2113VaggelisGian wants to merge 1 commit into
VaggelisGian wants to merge 1 commit into
Conversation
_float_value raised an uncaught ValueError for non-numeric strings and an uncaught TypeError for any other non-numeric type. A single malformed numeric field in a hosted v0 response crashed parsing in order_from_v0, position_from_v0, balance_from_v0 and user_trade_from_v0, while the mirrored TypeScript helpers floatOrUndefined and floatOrZero never throw: they degrade to undefined and 0. Return None instead of raising: catch ValueError from float(str) for non-numeric strings and fall through to None for unsupported types. Numeric behavior is unchanged (int, float, Decimal, numeric strings, empty string, None), and _float_or_zero keeps degrading to 0.0 through the now-total helper. Fixes pmxt-dev#2045
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.
Fixes #2045
What was broken
_float_value in sdks/python/pmxt/_hosted_mappers.py raised an uncaught ValueError for non-numeric strings (float('N/A')) and an uncaught TypeError for any other non-numeric type. A single malformed numeric field in a hosted v0 response crashed parsing in order_from_v0, position_from_v0, balance_from_v0 and user_trade_from_v0, so one bad field took down the whole response. The mirrored TypeScript helpers floatOrUndefined and floatOrZero never throw: they degrade to undefined and 0.
What changed
_float_value now returns None instead of raising: it catches ValueError from float(str) for non-numeric strings and falls through to None for unsupported types, matching floatOrUndefined in sdks/typescript/pmxt/hosted-mappers.ts. _float_or_zero already delegates to it, so zero-fallback fields keep degrading to 0.0 exactly like the TypeScript side. Numeric behavior is unchanged for int, float, Decimal, numeric strings, empty string and None. The change does not touch to_6dec (#1551) or _timestamp_to_ms (#1560).
Tests
Extended sdks/python/tests/test_hosted_mappers.py with a TestHostedMalformedNumerics class covering non-numeric strings, unsupported JSON types (dict, list, object), a parametrized guard over the existing numeric behaviors, and mapper-level assertions across all four v0 response mappers. Without the patch exactly these 6 tests fail with ValueError/TypeError; with it all 26 tests in the file pass.
Verification