Skip to content

Message named pkg/Duration with field builtin_interfaces/Duration is invalid #257

@peci1

Description

@peci1

Generated by Generative AI

No response

Operating System:

Linux

ROS version or commit hash:

rolling

RMW implementation (if applicable):

No response

RMW Configuration (if applicable):

No response

Client library (if applicable):

No response

'ros2 doctor --report' output

No response

Steps to reproduce issue

  1. Create a message package pkg containing a message type named Duration with a field of type builtin_interfaces/Duration.
  2. Compile the workspace and inspect the generated Python code.
  3. Run mypy tests and flake8

Expected behavior

Message type pkg/Duration should be valid.

Actual behavior

The generated message Python code is invalid:

 27: /root/target_ws/build/stamped_msgs/rosidl_generator_py/stamped_msgs/msg/_duration.py:87:1: F811 redefinition of unused 'Duration' from line 20
  27: class Duration(rosidl_pycommon.interface_base_classes.BaseMessage, metaclass=Metaclass_Duration):
  27: ^
  27: 
  27: 1     F811 redefinition of unused 'Duration' from line 20
  30: /root/target_ws/build/stamped_msgs/rosidl_generator_py/stamped_msgs/msg/_duration.py:87:1: error: Name "Duration" already defined (possibly by an import)  [no-redef]
  30: /root/target_ws/build/stamped_msgs/rosidl_generator_py/stamped_msgs/msg/_duration.py: note: In member "__eq__" of class "Duration":
  30: /root/target_ws/build/stamped_msgs/rosidl_generator_py/stamped_msgs/msg/_duration.py:151:27: error: "Duration" has no attribute "header"  [attr-defined]
  30: /root/target_ws/build/stamped_msgs/rosidl_generator_py/stamped_msgs/msg/_duration.py:153:25: error: "Duration" has no attribute "data"  [attr-defined]
  30: Found 3 errors in 1 file (checked 46 source files)

Additional information

Indeed, there are multiple places in the generated code that do:

from a.msg import b

But if the message is named pkg.Duration and the type of one of its fields is builtin_interfaces.Duration, then this import shadows the actual pkg.Duration and the code gets either confused, or even invalid.

I think all usages of from a.msg import b should be replaced by import a.msg; .... a.msg.b in the generated code. This way, the name collisions would not happen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions