Skip to content

fix: use rai without rai_interfaces #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 29, 2025
Merged

Conversation

maciejmajek
Copy link
Member

@maciejmajek maciejmajek commented May 29, 2025

Purpose

While testing RAI installed with pip, I've come across a problem when running a very simple example:

from rai.communication.ros2 import ROS2Connector
  File "/home/mmajek/projects/rai_pip/.venv/lib/python3.12/site-packages/rai/communication/ros2/messages.py", line 31, in <module>
    if importlib.util.find_spec("rai_interfaces.msg") is None:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib.util>", line 92, in find_spec
ModuleNotFoundError: No module named 'rai_interfaces'

Proposed Changes

Implements safe_find_spec to catch an error if package cannot be imported.

Issues

#607

Testing

  • How was it tested, what were the results?

@maciejmajek maciejmajek requested a review from boczekbartek May 29, 2025 15:37
@maciejmajek maciejmajek changed the title feat: safe_find_spec fix: use rai without rai_interfaces May 29, 2025
# limitations under the License.

import importlib.util

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
from importlib.machinery import ModuleSpec

import importlib.util


def safe_find_spec(name):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def safe_find_spec(name):
def safe_find_spec(name: str) -> ModuleSpec | None:


def safe_find_spec(name):
try:
return importlib.util.find_spec(name)
Copy link
Member

Choose a reason for hiding this comment

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

I tried with python3.10 and python 3.13 and errors are not raised... When did you encounter this error?

>>> importlib.util.find_spec('aaa')
>>> importlib.util.find_spec('numpy')
ModuleSpec(name='numpy', loader=<_frozen_importlib_external.SourceFileLoader object at 0x10d9b69e0>, origin='/usr/local/lib/python3.10/site-packages/numpy/__init__.py', submodule_search_locations=['/usr/local/lib/python3.10/site-packages/numpy'])

Copy link
Member Author

Choose a reason for hiding this comment

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

try aaa.aaa

Copy link
Member Author

@maciejmajek maciejmajek May 29, 2025

Choose a reason for hiding this comment

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

Hmm. I think we should rewrite all find_spec('rai_interfaces.*') to find_spec('rai_interfaces'). This way we will mitigate the problem without introducing custom safe_find_spec

@maciejmajek
Copy link
Member Author

@boczekbartek I've refactored the solution to this problem to a mere two line changes (removing the submodule part of the rai_interfaces)

Copy link
Member

@boczekbartek boczekbartek left a comment

Choose a reason for hiding this comment

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

Great! LGTM

@maciejmajek maciejmajek merged commit 245efa9 into development May 29, 2025
7 of 8 checks passed
@maciejmajek maciejmajek deleted the feat/safe-spec branch May 29, 2025 17:28
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