ros2 / ros2/launch

Type checking with mypy

Open
#676 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

help wanted
Dominant language
Python
Stars
155
Forks
182
Avg merge
2d 14h
Merged PRs (30d)
6

Description

Bug report

I am sorry to open an issue in such short order, but as part of my work at Woven Planet, I'm trying to use the ROS2 launch system. Unfortunately, I have found that it type information is currently unusable from an external package, thus I'd like to improve it.

Required Info:

  • Operating System:
    • Ubuntu 18.04
  • Installation type:
    • Source
  • Version or commit hash:
    • 6783ca8
  • DDS implementation:
    • CycloneDDS
  • Client library (if applicable):
    • N/A
Steps to reproduce issue
  1. Create a snippet with this content:
import launch

context = launch.LaunchContext()
reveal_type(context)
  1. Install mypy
  2. Run the following:
MYPYPATH=$PATH_TO_WS/install/launch/lib/python3.8/site-packages/ mypy -m launch

(mypy does not use PYTHONPATH by default)

Expected behavior
test.py:4: note: Revealed type is 'launch.launch_context.LaunchContext'
Actual behavior
test.py:1: error: Cannot find implementation or library stub for module named 'launch'
test.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
test.py:4: note: Revealed type is 'Any'

Feature request

Feature description

I'd like to make the type checking in launch (and potentially other ROS2 core libraries) a little bit stricter. This would allow to enforce a quality standard, and root out bugs at the base. It would also make the code easier to use from the outside, since type checking would prevent some common bugs in launch files, such as using a substitution where a condition is expected. Since type-checking is strictly opt-in, there would be no impact on the broader community.

Implementation considerations
  • I have already implemented adding the py.typed file to mark the launch package as typed to the broader system, and can submit a PR promptly
  • However, there are many type-checking issues in the current codebase, including bugs. For example, mypy reported that this function does not have a return statement, contradicting its signature. Similarly, this statement has invalid type syntax. Probably someone meant Tuple? In total mypy reports 224 errors in 47 files.
  • Thus, I think we should also add a CI test to check the type information using mypy. This is I think very straightforward using ament_mypy in the same way that you are currently using ament_pep257 and others.

I can take care of the above points, but I'd like to make sure that the maintainers agree with this approach before making a PR full of tiny changes.

Thank you very much!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the proposed py.typed marker, the mypy findings in launch/launch/event_handler.py and launch/frontend/parser.py, and the existing CI checks using ament_pep257. Start by checking how ament_mypy would fit the current test setup. Done means the agreed type-checking scope is implemented and CI verifies it without the reported errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ci-cd, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.