-
Notifications
You must be signed in to change notification settings - Fork 257
Closed
Description
Feature request
Feature description
I use mypy, one of the most famous static type checkers in Python, to check my Python code depending on rclpy.
However, mypy won't use the type annotation of rclpy, since mypy only uses the type annotation of packages that are marked as PEP561-compatible (i.e. Packages that have py.typed inside).
Therefore, I can't check the code like:
$ cat foo.py
import rclpy
reveal_type(rclpy)
reveal_type(rclpy.init)
rclpy.foo # should be error
rclpy.init(foo="bar") # should be error
$ mypy foo.py
foo.py:1: error: Cannot find implementation or library stub for module named 'rclpy'
foo.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
foo.py:3: note: Revealed type is 'Any'
foo.py:4: note: Revealed type is 'Any'
Found 1 error in 1 file (checked 1 source file)Would it be possible to include py.typed in rclpy package? (I am happy to work on this task!)
Implementation considerations
- The simplest solution is to create an empty file
py.typedunderrclpydirectoryrclpyusesament_python_install_packagemacro, and the macro just copies the directory tree into the install directory:
https://github.com/ament/ament_cmake/blob/b84cf9e6f2a61d8f9fc5a90c02dc2b5cb63e7f76/ament_cmake_python/cmake/ament_python_install_package.cmake#L181-L186- Adding this empty
py.typedfile is common in Python (e.g. numpy)
- Another approach is to add the following line in
CMakeLists.txt:file(WRITE "${PYTHON_INSTALL_DIR}/${PROJECT_NAME}/py.typed" "")
- But I don't think it's a good idea since it's a bit harder to know whether the package is PEP561-compatible or not.
Metadata
Metadata
Assignees
Labels
No labels