Skip to content

fix(python): make _float_value total on malformed numeric input - #2113

Open
VaggelisGian wants to merge 1 commit into
pmxt-dev:mainfrom
VaggelisGian:fix/python-float-value-total
Open

fix(python): make _float_value total on malformed numeric input#2113
VaggelisGian wants to merge 1 commit into
pmxt-dev:mainfrom
VaggelisGian:fix/python-float-value-total

Conversation

@VaggelisGian

Copy link
Copy Markdown

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

  • python -m pytest tests/ from sdks/python: 277 passed; the only failures are the two pre-existing HOME-vs-USERPROFILE sandbox cases that fail identically on clean main.
  • bash scripts/verify-all.sh on this branch matches a clean checkout of main exactly: only the three known Windows-only sandbox defects fail (core pmxt-ensure-server plus the two Python server_manager HOME cases), which pass on ubuntu-latest CI.

_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant