refactor: simplify _extract_namespaced_type_fields, remove redundant try-except and post-processing

Made-with: Cursor
This commit is contained in:
Xuwznln
2026-03-22 01:33:01 +08:00
parent 9e6007eb60
commit 182c5187a0

View File

@@ -1,11 +1,8 @@
import array import array
import importlib import importlib
import logging
from collections import OrderedDict from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Optional, Type from typing import TYPE_CHECKING, Any, Dict, Optional, Type
logger = logging.getLogger(__name__)
from rosidl_parser.definition import NamespacedType # type: ignore from rosidl_parser.definition import NamespacedType # type: ignore
from rosidl_runtime_py import ( # type: ignore from rosidl_runtime_py import ( # type: ignore
import_message_from_namespaced_type, import_message_from_namespaced_type,
@@ -248,9 +245,6 @@ class ROS2MessageInstance(MessageInstance[Any]):
type_info: 要填充 object_fields 的 TypeInfo 对象 type_info: 要填充 object_fields 的 TypeInfo 对象
namespaced_type: rosidl NamespacedType 定义 namespaced_type: rosidl NamespacedType 定义
""" """
from msgcenterpy.core.type_info import TypeInfoPostProcessor
try:
msg_cls = import_message_from_namespaced_type(namespaced_type) msg_cls = import_message_from_namespaced_type(namespaced_type)
msg_instance = msg_cls() msg_instance = msg_cls()
@@ -269,8 +263,5 @@ class ROS2MessageInstance(MessageInstance[Any]):
original_type=slot_type, original_type=slot_type,
) )
self._extract_from_rosidl_definition(field_type_info) self._extract_from_rosidl_definition(field_type_info)
TypeInfoPostProcessor.post_process_type_info(field_type_info)
field_type_info.add_constraint(ConstraintType.REQUIRED, True) field_type_info.add_constraint(ConstraintType.REQUIRED, True)
type_info.object_fields[field_name] = field_type_info type_info.object_fields[field_name] = field_type_info
except Exception as e:
logger.warning("Failed to extract fields from NamespacedType %s: %s", namespaced_type, e)