Skip to content

Fully annotate the example code and gate it with pyright - #57

Merged
mmmarinho merged 3 commits into
mainfrom
workspace-full-annotations
Oct 28, 2026
Merged

mmmarinho merged 3 commits into
mainfrom
workspace-full-annotations

Conversation

@mmmarinho

Copy link
Copy Markdown
Owner

What

The preamble teaches "Take the (type) hint: Always use type hints" and promises the two simplest forms (argument and return types), but the ROS2 examples had drifted: 62 functions across 26 files were missing annotations (audited with mypy --disallow-untyped-defs, now 0).

Workspace (2840e10)

All annotated in the tutorial's own style:

  • -> None everywhere nothing is returned; main() becomes def main(args: list[str] | None = None) -> None: (the canonical ROS2 entry-point signature, jazzy ships Python 3.12 so | None needs no import)
  • subscription/service/action callbacks keep their message parameters and gain return types; compute_control_action() gains float parameters (also fixing :float spacing)
  • the two action clients declare self.send_goal_future: Future / self.get_result_future: Future instead of = None, which also removes the type conflict mypy flagged when the futures are assigned
  • send_pose_to_gazebo() returns Future[SetEntityPose.Response]; launch files' generate_launch_description() returns LaunchDescription

flake8 vs the base commit shows no new violations (net -4).

Docs (68b51b7)

Two gazebo examples grew one line each (rclpy.task import, wrapped controller signature); the four literalinclude :lines: excerpt ranges in the custom-nodes page are re-anchored accordingly.

CI (34719eb)

New typecheck job (no ROS container needed):

- run: npx --yes pyright -p ros2_tutorial_workspace

with a ros2_tutorial_workspace/pyrightconfig.json (reportMissingParameterType: error, ROS/colcon imports silenced) plus tiny stubs for the classes colcon only generates at build time (package_with_interfaces msg/srv/action, launch.LaunchDescription, launch_ros.actions.Node). pyright currently reports 0 errors, 0 warnings over the whole workspace.

Note: pyright has no "missing return type" check (reportMissingReturnType does not exist), so return annotations are guarded by convention + the mypy audit, and pyright guards parameter types and call structure.

The preamble teaches 'Take the (type) hint: Always use type hints' and
promises the two simplest forms (argument and return types), but the
ROS2 examples drifted away from it: 62 functions across 26 files were
missing annotations. All now annotated in the tutorial's own style:

- '-> None' everywhere nothing is returned; main() takes
  'args: list[str] | None = None';
- subscription/service/action callbacks keep their (already annotated)
  message parameters and gain return types; compute_control_action()
  gains float parameters (also fixing the ':float' spacing);
- the two action clients declare 'self.send_goal_future: Future' /
  'self.get_result_future: Future' instead of '= None', which also
  removes the type conflict mypy flagged when the futures are assigned;
- send_pose_to_gazebo() returns Future[SetEntityPose.Response];
  launch files' generate_launch_description() returns LaunchDescription.

mypy --disallow-untyped-defs is now clean over the whole workspace;
flake8 shows no new violations (net -4).
The re-annotated send_poses_to_gazebo_node.py and
control_shape_thrust_node.py grew one line each (rclpy.task import and
the wrapped compute_control_action signature); the four excerpt ranges
are shifted accordingly.
New typecheck job runs pyright over ros2_tutorial_workspace
(reportMissingParameterType error; ROS imports resolved via targeted
stubs for the colcon-generated package_with_interfaces message/service
/ action classes and the launch/launch_ros classes, since those Python
bindings are generated at build time). Together with the annotation
pass this keeps the examples 'fully annotated' per the preamble rule:
pyright guards parameter types and structure, and the mypy
--disallow-untyped-defs audit (run in review) guards return types.
@mmmarinho
mmmarinho merged commit 9cf4855 into main Oct 28, 2026
6 checks passed
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