Skip to content
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

Field name clash in some MAVLink messages #243

Open
pietrodn opened this issue Nov 17, 2018 · 10 comments
Open

Field name clash in some MAVLink messages #243

pietrodn opened this issue Nov 17, 2018 · 10 comments

Comments

@pietrodn
Copy link
Contributor

In mavgen_python.py, all generated MAVLink message types have two class attributes, id and name, expressing the MAVLink message type.

However, these class attributes clash with the MAVLink messages with fields called id and name!
Indeed:

  1. LOG_ENTRY, LOG_REQUEST_DATA, LOG_DATA, DISTANCE_SENSOR, BATTERY_STATUS, COLLISION have an id field;
  2. DEBUG_VECT, NAMED_VALUE_FLOAT, NAMED_VALUE_INT, UAVCAN_NODE_INFO, DEBUG_FLOAT_ARRAY have a name field.

This name clash can cause problems in code depending on the semantics of the name and id attribute. Example:

>>> from pymavlink.dialects.v20.common import MAVLink_log_entry_message
>>> MAVLink_log_entry_message.name
'LOG_ENTRY'
>>> MAVLink_log_entry_message.id
118
>>> MAVLink_log_entry_message(id=70, num_logs=2, last_log_num=2, time_utc=1000, size=1024).id
70
@hamishwillee
Copy link
Contributor

This also appears to affect Java as well - see https://groups.google.com/g/mavlink/c/9cRgGSRzCLw/m/x9TDaLAiCAAJ

@peterbarker For the java case (at least) they solved the problem by modifying the generated code for the id case. Is it worth thinking about doing this automatically for a couple of key field names - like id and name?

@peterbarker
Copy link
Contributor

@hamishwillee I think ideally we remove .id and .name as attributes - unless they're actual attributes of hte message.

Sadly, that's a significant change and likely to break a lot of stuff. I believe we upped and broke the API for Java - that's less an option for the python bindings given their widespread use.

We could probably start towards moving people off using .id and .name and whatever message metadata we have - I think Python probably gives us the flexibility to do so.

The message ID should always we available with get_msgId - so its really a matter of weaning people off the use of .id. That might be one by specifying @attr on the default attribute values and warning anybody using them we're going to break them in a few years. There is no equivalent for name, however.

BTW, the problem is getting worse over time:
https://github.com/ArduPilot/pymavlink/blob/master/generator/mavgen_python.py#L359

where we move the data to I don't know. Fundamentally you can't use class attributes to store information about the message and also use the fieldnames as attributes on instances of the class. I expect we'll choose to just prepend underscores where we aren't storing the data elsewhere), so e.g. msg._lengths. Alternatively we could provide a function to retrieve an object which has all of the metadata about the message....

While this is all rather messy - but its been this way for a very long time :-)

@hamishwillee
Copy link
Contributor

hamishwillee commented Jan 14, 2021

@peterbarker Thanks very much. The only thing I know for sure is that we should either fix this or explicitly state that we're not going to; not just leave it as an open issue.

Prepending underscores for the general attributes of the message like id makes sense to me. But as there doesn't appear to be any non-breaking solution I'm more concerned about agreeing a deprecation plan. Will be lead by you.

Can you talk to @tridge about what his preferred solution might be?

@peterbarker
Copy link
Contributor

peterbarker commented Jan 14, 2021 via email

@tridge
Copy link
Contributor

tridge commented Jan 19, 2021

I think we should rename the fields to have an _ prefix

@hamishwillee
Copy link
Contributor

OK. So @peterbarker I'll keep an eye on this to see if you decide to fix this and what the roll out/mitigation plan is.

@peterbarker
Copy link
Contributor

peterbarker commented Jan 20, 2021 via email

@hamishwillee
Copy link
Contributor

Great. @amilcarlucas Any movement on this?

@amilcarlucas
Copy link
Contributor

amilcarlucas commented Feb 25, 2021

No, not yet. Basically I need to do:

s/\.id/\._id/g
s/\.name/\._name/g
s/\.instance/\._instance/g

but I have not done it yet :(

@amilcarlucas
Copy link
Contributor

amilcarlucas commented Feb 25, 2021

Nope :( not that simple.
\me lowers his hand now.

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

No branches or pull requests

6 participants