Skip to content

feat(hiroz-py): rclpy alignment — P1–P8 API improvements#192

Open
YuanYuYuan wants to merge 1 commit into
mainfrom
dev/review-python-api
Open

feat(hiroz-py): rclpy alignment — P1–P8 API improvements#192
YuanYuYuan wants to merge 1 commit into
mainfrom
dev/review-python-api

Conversation

@YuanYuYuan
Copy link
Copy Markdown
Collaborator

Summary

Implements 8 concrete API improvements to hiroz-py that reduce migration friction for users coming from rclpy, without touching hiroz's reactive no-spin core. All changes are additive and non-breaking.

  • P1 — Add wait_for_service / wait_for_server / wait_for_subscription: graph-polling wait primitives that replace the time.sleep(1.0) anti-pattern present in every example
  • P2 — Swapped-arg detection in create_publisher/create_subscriber: rclpy orders (msg_type, topic), hiroz orders (topic, msg_type) — silent positional failures now raise a clear TypeError with a migration hint
  • P3create_subscription and create_service aliases on ZNode
  • P4 — Codegen emits service grouping classes (AddTwoInts.Request / .Response with __srvtype__); create_client/create_server accept the grouping class or the bare Request class (back-compat preserved)
  • P5 — Wire the already-defined TimeoutError / SerializationError / TypeMismatchError: timeouts now raise hiroz_py.TimeoutError instead of bare RuntimeError
  • P6 — Optional callback-style create_server(callback=fn): spawns a background handler thread; pull mode unchanged when omitted. Adds create_service alias.
  • P7 — Codegen emits action grouping classes (Fibonacci.Goal / .Result / .Feedback with __actiontype__); create_action_client/create_action_server accept either form
  • P8 — QoS policy enums (ReliabilityPolicy.RELIABLE, DurabilityPolicy.VOLATILE, etc.) and qos=10 int-depth shorthand (rclpy-style)

Key Changes

  • crates/hiroz-py/src/: node.rs, service.rs, action.rs, pubsub.rs, graph.rs, error.rs, qos.rs, traits.rs
  • crates/hiroz-codegen/src/python_msgspec_generator.rs: P4 + P7 grouping class emission
  • crates/hiroz-msgs/python/hiroz_msgs_py/types/*.py: regenerated with grouping classes
  • crates/hiroz-py/python/hiroz_py/__init__.py + __init__.pyi: aliases, enums, updated stubs
  • crates/hiroz-py/examples/: all three examples updated to use the new API
  • crates/hiroz-py/tests/test_rclpy_alignment.py: 17 new end-to-end tests

Breaking Changes

None. All changes are additive. Existing code using bare Request classes, create_subscriber, pull-mode servers, and string QoS values continues to work.

Open items (not in this PR)

  • P6 callback thread swallows exceptions silently — should surface via threading.excepthook or a .last_error property
  • wait_for_service uses a 50ms polling loop; could be replaced with a liveliness-based notify once that API stabilises in hiroz core
  • Migration guide (_tmp/MIGRATION-GUIDE.md) not yet added to the book

Implements all 8 proposals from the rclpy API alignment review:

P1 — wait_for_service / wait_for_server / wait_for_subscription
  Adds graph-polling wait primitives to ZClient, ZActionClient, and ZPublisher.
  Replaces time.sleep(1.0) anti-pattern in all examples.

P2 — Smart error for swapped arguments
  create_publisher/create_subscriber detect when msg_type and topic args are
  positionally swapped (rclpy order vs hiroz order) and raise a clear TypeError.

P3 — Method aliases
  ZNode.create_subscription aliased to create_subscriber.

P4 — Service grouping type in codegen
  python_msgspec_generator now emits AddTwoInts.Request / .Response grouping
  classes with __srvtype__. create_client/create_server accept the grouping
  class or the bare Request class (back-compat).

P5 — Wire custom exception types
  Timeouts now raise hiroz_py.TimeoutError instead of bare RuntimeError.
  Tests and examples updated accordingly.

P6 — Optional callback-style create_server
  create_server accepts optional callback= kwarg; spawns a background thread
  when provided. Pull mode (take_request/send_response) remains the default.
  ZNode.create_service added as alias.

P7 — Action grouping type in codegen
  Actions emit Fibonacci.Goal / .Result / .Feedback grouping classes with
  __actiontype__. create_action_client/server accept either form.

P8 — QoS enum constants + int-depth shorthand
  ReliabilityPolicy, DurabilityPolicy, HistoryPolicy, LivelinessPolicy enums
  added to __init__.py. QoS params accept int (depth shorthand, rclpy-style).

Also adds test_rclpy_alignment.py (17 tests, all passing).
65 total tests pass, clippy clean, ruff clean.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant