Skip to content

Conversation

bjsowa
Copy link
Member

@bjsowa bjsowa commented Aug 31, 2025

Public API Changes
Aside from specifying type for public API arguments, the names of any arguments are still thead same.

Description
This was exhausting to do but IMO worth it. It always bothered me that I had to go back and forth between files to figure out what types does rosbridge implementation expect.

Changes

  • Added from __future__ import annotations to every source file - for better compatibility and flexibility in type hinting.
  • Set required-python version to 3.10 - so ruff checks don't try to keep compatibility with earlier versions
  • Enabled flake8-annotations check rules which enforce type hints for every argument and return types in all functions and methods.
  • Fixed ruff check errors and new mypy errors

Additional changes
During the process of adding annotations and fixing type errors, I've made a few implementation changes that might affect the behavior of the library. Most notable changes are:

  • In rosbridge_library.capabilities.advertise.Registration class, added dummy advertise ID to use in place of None when the client does not provide id of the advertisement
  • In rosbridge_library.capabilities.defragmentation module, got rid of ReceivedFragments singleton class and instead, added lists class variable to Defragment capability
  • Simplified logging in rosbridge_library.capabilities.defragmentation.Defragment class, making use of f-strings where applicable
  • Multiple changes to rosbridge_library.internal.message_conversion module:
    • The extract_values function now calls _from_object_inst instead of _from_inst and populate_instance calls _to_object_inst instead of _to_inst. We can do this small optimization, assuming the inst passed to these function is a ROS message type.
    • I added _from_primitive_inst to split the logic and make it more readable.
    • Improved _to_time_inst a little, raising errors when incorrect type name or message, creating Time and Duration message instances directly, instead of calling rclpy.time.Time().to_msg()
    • Added isinstance asserts to narrow down union types for static type checking. This can have a small but IMO negligible impact on performance, and can cause some false positives if I didn't predict that some types can be allowed at certain places.
    • Added helper _remove_list_indicators function

bjsowa added 30 commits August 14, 2025 02:47
@bjsowa bjsowa changed the title Add type annotations to all functions and methods feat: Add type annotations to all functions and methods Sep 2, 2025
@bjsowa bjsowa changed the title feat: Add type annotations to all functions and methods refactor: Add type annotations to all functions and methods Sep 2, 2025
@bjsowa
Copy link
Member Author

bjsowa commented Sep 4, 2025

@ros-pull-request-builder retest this please

@bjsowa
Copy link
Member Author

bjsowa commented Sep 5, 2025

This fails in Jazzy, due to lack of support for type arguments in Future, Client, Subscription and other classes. @sea-bass Do you think it would be a good idea to branch out for jazzy now?

@sea-bass
Copy link
Contributor

sea-bass commented Sep 8, 2025

This fails in Jazzy, due to lack of support for type arguments in Future, Client, Subscription and other classes. @sea-bass Do you think it would be a good idea to branch out for jazzy now?

I'm OK with branching out!

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.

2 participants