Skip to content

Added test_mypy.py and resolved all regressions in demo_nodes_py (#767)#780

Open
cronenberg64 wants to merge 5 commits intoros2:rollingfrom
cronenberg64:feature/add-mypy-demo-nodes-py
Open

Added test_mypy.py and resolved all regressions in demo_nodes_py (#767)#780
cronenberg64 wants to merge 5 commits intoros2:rollingfrom
cronenberg64:feature/add-mypy-demo-nodes-py

Conversation

@cronenberg64
Copy link
Copy Markdown

Added test_mypy.py file and resolved all regressions.

Fixes #767

Copilot AI review requested due to automatic review settings March 17, 2026 17:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds ament_mypy linting to demo_nodes_py and updates the Python demo nodes/tests with type annotations to satisfy strict mypy checks, addressing issue #767.

Changes:

  • Added a new test_mypy.py test and declared ament_mypy as a test dependency.
  • Added/expanded type annotations across multiple demo nodes (topics, services, parameters, logging, events) to resolve mypy regressions.
  • Minor formatting tweaks (e.g., line wrapping) and added shebangs to several Python entrypoint modules.

Reviewed changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
demo_nodes_py/test/test_pep257.py Adds return type annotation to satisfy stricter typing expectations in tests.
demo_nodes_py/test/test_flake8.py Adds return type annotation to satisfy stricter typing expectations in tests.
demo_nodes_py/test/test_copyright.py Adds return type annotation to satisfy stricter typing expectations in tests.
demo_nodes_py/test/test_mypy.py Introduces mypy linter test for the package.
demo_nodes_py/package.xml Adds ament_mypy as a test_depend.
demo_nodes_py/demo_nodes_py/topics/talker.py Adds shebang and type annotations (incl. timer type).
demo_nodes_py/demo_nodes_py/topics/talker_qos.py Adds shebang and type annotations for QoS usage and timer.
demo_nodes_py/demo_nodes_py/topics/listener.py Adds shebang, type annotations, and wraps subscription call for style.
demo_nodes_py/demo_nodes_py/topics/listener_qos.py Adds shebang and type annotations for QoS usage and callbacks.
demo_nodes_py/demo_nodes_py/topics/listener_serialized.py Adds shebang, callback typing for raw bytes subscription, and adjusts log formatting.
demo_nodes_py/demo_nodes_py/services/introspection.py Adds shebang and type annotations (Timer/Future, callbacks, service handler).
demo_nodes_py/demo_nodes_py/services/add_two_ints_server.py Adds shebang and type annotations for service handler.
demo_nodes_py/demo_nodes_py/services/add_two_ints_client.py Adds shebang and type annotations; refactors result logging for mypy.
demo_nodes_py/demo_nodes_py/services/add_two_ints_client_async.py Adds shebang and type annotations; refactors result logging for mypy.
demo_nodes_py/demo_nodes_py/parameters/set_parameters_callback.py Adds shebang and type annotations for parameter callback hooks.
demo_nodes_py/demo_nodes_py/parameters/async_param_client.py Adds shebang, type annotations, and renames futures/loop vars for clarity and typing.
demo_nodes_py/demo_nodes_py/logging/use_logger_service.py Adds shebang and type annotations; adds a couple targeted mypy ignores for generated message fields.
demo_nodes_py/demo_nodes_py/events/matched_event_detect.py Replaces ... lambdas with None returns and adds typing for callbacks/future helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.



def test_mypy() -> None:
rc = main()
…des_py (ros2#767)

Resolves ros2#767 by enabling strict type checking.

Key Changes:
- Added strict annotations and return types to 14+ modules to pass mypy --strict.
- Corrected a functional error in matched_event_detect.py where ROS message types were used as type annotations for topic names.
- Added test/test_mypy.py and updated package.xml for ament_mypy support.
- Ensured node scripts maintain correct #!/usr/bin/env python3 shebangs and LF line endings.

Signed-off-by: Jonathan Setiawan <jonathanrustam2@gmail.com>
@cronenberg64 cronenberg64 force-pushed the feature/add-mypy-demo-nodes-py branch from ebdb446 to b639a2d Compare March 17, 2026 19:01
Signed-off-by: Jonathan Setiawan <jonathanrustam2@gmail.com>
@cronenberg64
Copy link
Copy Markdown
Author

@fujitatomoya @InvincibleRMC
Hello,
I've just addressed all the suggestions you've given me. Thank you very much for the feedback, please check my latest commit if there are any further mistakes, I will address them right away! I look forward to hearing back from you soon.

@ahcorde
Copy link
Copy Markdown
Contributor

ahcorde commented Mar 18, 2026

Pulls: #780
Gist: https://gist.githubusercontent.com/ahcorde/461a365b20b2d6bd27d954d046a06c37/raw/df131c1c43d52ec2f46e16edc5727abe958fa11c/ros2.repos
BUILD args: --packages-up-to demo_nodes_py
TEST args: --packages-select demo_nodes_py
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/18542

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Signed-off-by: Jonathan Setiawan <jonathanrustam2@gmail.com>
Signed-off-by: Jonathan Setiawan <jonathanrustam2@gmail.com>
@ahcorde
Copy link
Copy Markdown
Contributor

ahcorde commented Mar 24, 2026

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@cronenberg64
Copy link
Copy Markdown
Author

@InvincibleRMC Just a quick ping on this, I believe I've addressed all the previous feedback regarding the python ver compatibility, typing changes, and linter formatting issues.

Let me know if there is anything else you need me to adjust, or if you are able to re-review and clear the requested changes. Thank you



def test_mypy() -> None:
rc = main()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rc = main()
rc = main('--ament-srict')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion @InvincibleRMC, I actually tried adding this flag locally earlier, but it caused colcon test to fail for a couple of reasons. First, ament_mypy.main() expects a list of strings (argv=['...']), so passing a raw string causes argparse to fail as it tries to parse each character individually. Even when passed correctly as argv=['--ament-strict'] (fixing the slight typo), ament_mypy throws a SystemExit: 2 crash because --ament-strict isn't a recognized flag in the ament parser. If we want to enforce stricter type checking, relying on the default ament_mypy configuration (which wraps strict mypy settings automatically) seems to be passing perfectly right now. Alternatively, we could pass a custom --config file. Please let me know what you think and how I should proceed. Thank you.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you update to the newest version on rolling it shouldn't crash.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please correct me if I'm wrong but I think we may need to provide argv=['--ament-strict']. If we just pass the raw string into main(), Python's argparse breaks the word apart and tries to read it letter-by-letter, which causes it to crash.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry yes that is what I meant.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed the update using argv=['--ament-strict'] and the letter-by-letter parsing crash seems to be gone but, the CI build is still failing with a new SystemExit: 2 from argparse because --ament-strict is throwing an 'unrecognized argument' error. Looking at the CI traceback, the server is executing ament_mypy from /opt/ros/rolling/..., meaning it's using the pre-built binaries. It looks like the ubuntu_noble CI containers haven't been synced with the latest version on rolling I think which is causing the build to fail.

How do you think we should proceed?

Signed-off-by: Jonathan Setiawan <jonathanrustam2@gmail.com>
@InvincibleRMC
Copy link
Copy Markdown
Contributor

Pulls: #780
Gist: https://gist.githubusercontent.com/InvincibleRMC/1a8249a1eed3c1163941baf78ee046ed/raw/2d65eb81b2d00baeccedc3d096fc9d55f8e6dab7/ros2.repos
BUILD args: --continue-on-error --packages-above-and-dependencies demo_nodes_py
TEST args: --packages-above demo_nodes_py
ROS Distro: rolling
Job: ci_launcher
ci_launcher ran: https://ci.ros2.org/job/ci_launcher/18883

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

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.

Add ament_mypy to demo_nodes_py

5 participants