Repository navigation
fix(mqtt): serialise numpy scalars and arrays in published payloads - #41
rishabh-bansal wants to merge 1 commit into
Conversation
A command argument computed with numpy/OpenCV (e.g. a distance from detected marker corners) is a numpy.float32. json.dumps raised TypeError on it, publish() logged it and returned False, and the caller only saw "the MQTT client refused it". Add a json default hook that converts numpy values via .tolist(), and let _replace_non_finite handle numpy NaN/inf too. Detected by module, so mqtt still never imports numpy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Slack team notifications
|
|
Thanks for contributing to the Cyberwave Python SDK, @rishabh-bansal! Maintainer contacts: @KhushiSharma22. When this PR is ready, we request a maintainer review and run an automated advisory review. CI and human review remain the source of truth. Please describe what changed and how you tested it. See the contribution guide or ask for help on Discord. Drafts are welcome. Merged human contributions receive a Discord thank-you. To opt out, check the PR template's Discord opt-out box or add |
Automated advisory review · ClaudeCommit: The patch adds numpy-aware JSON serialisation to the MQTT publish path. Two concrete defects are present: (1) a non-finite numpy scalar (e.g. numpy.float32('nan')) is converted to a Python float by tolist() inside _replace_non_finite, but the subsequent isinstance(value, float) branch then returns None correctly only for plain Python floats — however the more critical ordering issue is in _json_default, where a non-finite numpy scalar is converted to a non-finite Python float by tolist() and then returned directly to json.dumps with allow_nan=False, which will raise ValueError again rather than mapping it to null; (2) the first json.dumps call with allow_nan=False can now raise either ValueError (non-finite float) or TypeError (numpy value that slipped through a nested structure via default=), but only ValueError is caught, so a TypeError escaping from _json_default in the fallback path would surface as an unhandled exception, breaking the previous guarantee that publish() returns Fals
Coverage: 2 of 2 changed files; patch excerpts only. Generated REST code, lockfiles, notebooks, SVGs, missing patches and files beyond the 40-file / 60,000-character budget are excluded. GitHub may truncate individual patches. |
What changed and why?
A command argument computed with numpy/OpenCV is naturally a numpy scalar. For example, a distance from detected ArUco corners comes out as
numpy.float32. Passing it totwin.move_forward(dist)makesjson.dumpsraiseTypeErrorinsideCyberwaveMQTTClient.publish(). That logsError publishing to …: Object of type float32 is not JSON serializableand returnsFalse, so the caller only seesCyberwaveMQTTError: … The MQTT client refused it; its log says why.and the twin stops mid-mission. I hit exactly this while building a DJI Tello marker-search demo in MuJoCo.This PR:
default=hook (_json_default) to the existing strictjson.dumpscalls inpublish(), converting numpy scalars and arrays with.tolist();_replace_non_finitehandle numpy values too, so numpyNaN/infstill becomenull;type(value).__module__, socyberwave.mqttstill never imports numpy;publish()returnsFalse).How did you test it?
tests/test_mqtt_numpy_serialization.py(4 tests): numpy scalars/arrays publish as plain JSON (ints stay ints), numpy NaN/inf →null,_replace_non_finitehandles numpy, unknown objects are still rejected.mainand pass with this change.pytest tests/test_mqtt_numpy_serialization.py tests/test_mqtt_non_finite_serialization.py tests/test_mqtt_payload_limit.py→ 68 passed.mypy cyberwave/mqtt/__init__.py: the same 4 pre-existing errors (in other files) before and after; none added.Contributor checklist
Recognition
🤖 Generated with Claude Code