ros / xacro

Xacro is an XML macro language. With xacro, you can construct shorter and more readable XML files by using macros that expand to larger XML expressions.

Home Page:http://www.ros.org/wiki/xacro

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot construct dicts with numerical leading names

Doomerdinger opened this issue · comments

Currently experiencing this issue with Xacro melodic 1.13.9

I expect to be able to construct a property dict that can (at least on a basic level) mimic a yaml file.

<xacro:property name="offset" value="${dict(1x=0)}"/>

results in the following error:

invalid syntax (<string>, line 1) 
when evaluating expression 'dict(1x=0)' 
when evaluating expression 'offset'

If you try to mark the name as a string like so

<xacro:property name="offset" value="${dict('1x'=0)}"/>

you get the following error

keyword can't be an expression (<string>, line 1) 
when evaluating expression 'dict('1x'=0)' 
when evaluating expression 'offset'

Also applies if ` is used.

This makes it impossible to replicate a yaml file using dicts if it has keys that start with numbers, like so

---
1x: 0

Leading numerical values in dict keys is perfectly valid in Python and in yaml, I expect this not working in xacro dict is a bug and not an intentional decision.

Also in Python, you cannot create such a dict using the constructor syntax:

dict('1x'=0)
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

or

dict(1x=0)
SyntaxError: invalid syntax

It only works via curly braces:

{'1x': 0}

However, unfortunately, this interferes with the simple parser of xacro, which cannot accept other curly braces in a ${...} expression. I'm afraid there is no solution to your problem.

Actually, there is another dict constructor using lists, which works:

dict([('1x',0)])

You're certain it is not possible? I don't quite see how the dict can be limited as such, but loading a yaml file will work just fine.

I'd be happy to look into it a bit myself though I am not currently familiar with the codebase. I won't bother if you are certain nothing can be done.

Actually, there is another dict constructor using lists, which works:

dict([('1x',0)])

Huh! Noted. I was not aware of that format, I'll go ahead and use that for now.
Not sure this bug is worth tackling (if possible) if there exists an alternative, considering this is probably a niche issue.

May be good to make note of these forms on the wiki?

You are welcome to augment the wiki.

I'll put it on my agenda.