ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

type hinting rosmsgs in callbacks?

asked 2021-01-15 15:04:32 -0600

rbed23 gravatar image

Trying to prepare proper documentation for a code review and I wanted to use type hinting, however Im getting errors (expectedly) when type hinting for rosmsgs during function callbacks...

Wanted to know if anyone else has experienced this and any workarounds?

def on_action_done(self, goal_status: actionlib_msgs/GoalStatus, result: rows/RowsResult) -> None:
def on_action_done(self, goal_status: GoalStatus, result: RowsResult) -> None:

Neither is working, both are throwing NameError exceptions.


ROS1: noetic

edit retag flag offensive close merge delete



have you built Noetic from source? The default Python 3 version supported on Ubuntu Focal by Noetic is 3.8 according to REP-3.

gvdhoorn gravatar image gvdhoorn  ( 2021-01-16 06:48:59 -0600 )edit

1 Answer

Sort by » oldest newest most voted

answered 2021-01-16 06:45:12 -0600

gvdhoorn gravatar image

updated 2021-01-16 06:50:13 -0600

however Im getting errors (expectedly) when type hinting for rosmsgs during function callbacks...

why expectedly?

ROS msgs in Python are Python modules containing (mainly) a class. So you should be able to treat them as such.

As long as the packages are on the PYTHONPATH, I'd expect things to work.

goal_status: actionlib_msgs/GoalStatus

this doesn't look like a proper Python type name to me.

shouldn't it at least be actionlib_msgs.msg.GoalStatus?

edit flag offensive delete link more


actionlib.GoalStatus works...

I just needed to use the msg imports i.e. result: RowsResult, target: TrackedTarget

rbed23 gravatar image rbed23  ( 2021-01-21 18:05:32 -0600 )edit

actionlib.GoalStatus works...

this confuses me a little.

Is that a verbatim copy-paste of the type identifier, or should it be actionlib_msgs.msg.GoalStatus?

The following fails for me: python -c 'from actionlib import GoalStatus'.

gvdhoorn gravatar image gvdhoorn  ( 2021-01-22 00:34:35 -0600 )edit

(robot) me@BenderInspiron:~/robot/ros_src$ python -c "from actionlib import GoalStatus; print(GoalStatus)" <class 'actionlib_msgs.msg._goalstatus.goalstatus'="">

Im working in a venv and I have the setup.bash file sourced...

rbed23 gravatar image rbed23  ( 2021-01-23 15:26:23 -0600 )edit

Isn't that because almost all top-level modules in actionlib themselves import GoalStatus from actionlib_msgs?

The type is really declared and defined in actionlib_msgs.

if you're doing this to be clear about which types get used in your code, it would make more sense to me to use the appropriate (ie: correct) type definition, instead of one which happens to be exposed via a transitive dependency.

gvdhoorn gravatar image gvdhoorn  ( 2021-01-24 04:13:44 -0600 )edit

sure, if you want/need to separately import actionlib_msgs explicitly for that, in addition to import actionlib then go ahead :thumbsup:

rbed23 gravatar image rbed23  ( 2021-01-24 13:04:31 -0600 )edit

So you're going to go through the effort of providing Python with typing hints, but then are OK with passing it the wrong type?

GoalStatus is part of the action definition. It's a message. It comes from the actionlib_msgs package.

Relying on another package to bring in your dependency is actually an anti-pattern, and should be avoided.

gvdhoorn gravatar image gvdhoorn  ( 2021-01-24 13:59:24 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-01-15 15:04:32 -0600

Seen: 199 times

Last updated: Jan 16 '21