ITADN

python: _get_property returns dict method when field name matches a dict builtin

#1613Openremete618 创建于 2026-03-28
R
remete618commented
When a message field is named after a dict method (e.g. "values", "keys", "items"), `_get_property` in `_dynamic.py` returns the dict method instead of the field value. `_get_property` checks `hasattr(obj, name)` before trying dict key access. For a dict like `{"values": [1.0, 2.0]}`, `hasattr(dict, "values")` is True because `dict.values` is a built-in method, so `getattr` returns the method rather than falling through to `obj["values"]`. ```python def _get_property(obj, name): if hasattr(obj, name): # True for "values" on any dict return getattr(obj, name) # returns dict.values method, not the field try: return obj[name] except (KeyError, TypeError): return None ``` Repro: ```python from mcap_ros2.writer import Writer from io import BytesIO output = BytesIO() w = Writer(output=output) schema = w.register_msgdef("test/Msg", "float64[] values") w.write_message( topic="/test", schema=schema, message={"values": [1.0, 2.0, 3.0]}, log_time=0, publish_time=0, sequence=0, ) # raises: ValueError: Field "values" is not an array (<class 'builtin_function_or_method'>) ``` A fix would be to check dict key access first when `obj` is a dict, or to reverse the lookup order.
1 条评论