ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

type hinting rosmsgs in callbacks?

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

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.

python3.7.4

ROS1: noetic

edit retag flag offensive close merge delete

Comments

python3.7.4

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 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
2

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

gvdhoorn gravatar image

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

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

Comments

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 -0500 )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 -0500 )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 -0500 )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 -0500 )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 -0500 )edit
1

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 -0500 )edit

Question Tools

1 follower

Stats

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

Seen: 363 times

Last updated: Jan 16 '21