3232
3333from __future__ import annotations
3434
35- from typing import Any , Protocol , TypeVar , runtime_checkable
35+ from typing import Any , TypeAlias , TypeVar
3636
3737from rclpy .action .client import ActionClient as _ActionClient
3838from rclpy .action .client import ClientGoalHandle as _ClientGoalHandle
3939from rclpy .action .server import ActionServer as _ActionServer
4040from rclpy .action .server import ServerGoalHandle as _ServerGoalHandle
41+ from rosidl_pycommon .interface_base_classes import BaseAction , BaseImpl , BaseMessage , BaseService
4142
42- try :
43- from rosidl_pycommon .interface_base_classes import (
44- BaseAction ,
45- BaseMessage ,
46- BaseService ,
47- )
48-
49- ROSMessage = BaseMessage
50- ROSService = BaseService
51- ROSAction = BaseAction
52-
53- except ImportError :
54- # Fallback to Protocols if interface base classes are not available
55- # TODO: Remove this fallback once we drop support for Kilted
56-
57- @runtime_checkable
58- class ROSMessage (Protocol ): # type: ignore[no-redef]
59- """Protocol for ROS message types."""
60-
61- __slots__ : list [str ]
62- _fields_and_field_types : dict [str , str ]
63-
64- def get_fields_and_field_types (self ) -> dict [str , str ]:
65- """Return a dictionary of field names to field types."""
66-
67- @runtime_checkable
68- class ROSService (Protocol ): # type: ignore[no-redef]
69- """Protocol for ROS service types."""
70-
71- Request : type [ROSMessage ]
72- Response : type [ROSMessage ]
73- Event : type [ROSMessage ]
74-
75- @runtime_checkable
76- class ROSAction (Protocol ): # type: ignore[no-redef]
77- """Protocol for ROS action types."""
78-
79- Goal : type [ROSMessage ]
80- Result : type [ROSMessage ]
81- Feedback : type [ROSMessage ]
82- Impl : type [Any ]
83-
43+ ROSMessage : TypeAlias = BaseMessage
44+ ROSService : TypeAlias = BaseService
45+ ROSAction : TypeAlias = BaseAction
8446
8547# Type variables for ROS types
8648ROSMessageT = TypeVar ("ROSMessageT" , bound = ROSMessage )
@@ -91,40 +53,17 @@ class ROSAction(Protocol): # type: ignore[no-redef]
9153ROSActionGoalT = TypeVar ("ROSActionGoalT" , bound = ROSMessage )
9254ROSActionResultT = TypeVar ("ROSActionResultT" , bound = ROSMessage )
9355ROSActionFeedbackT = TypeVar ("ROSActionFeedbackT" , bound = ROSMessage )
94-
95- try :
96- from rosidl_pycommon .interface_base_classes import BaseImpl
97-
98- ROSActionImplT = TypeVar ("ROSActionImplT" , bound = BaseImpl [Any , Any , Any ])
99-
100- ActionClientType = _ActionClient [
101- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
102- ]
103- ClientGoalHandleType = _ClientGoalHandle [
104- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
105- ]
106- ActionServerType = _ActionServer [
107- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
108- ]
109- ServerGoalHandleType = _ServerGoalHandle [
110- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
111- ]
112-
113- except ImportError :
114- # Fallback to old type variables if BaseImpl is not available
115- # TODO: Remove this fallback once we drop support for Kilted
116-
117- ROSActionImplT = TypeVar ("ROSActionImplT" ) # type: ignore[misc]
118-
119- ActionClientType = _ActionClient [ # type: ignore[misc]
120- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT
121- ]
122- ClientGoalHandleType = _ClientGoalHandle [ # type: ignore[misc]
123- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT
124- ]
125- ActionServerType = _ActionServer [ # type: ignore[misc]
126- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT
127- ]
128- ServerGoalHandleType = _ServerGoalHandle [ # type: ignore[misc]
129- ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT
130- ]
56+ ROSActionImplT = TypeVar ("ROSActionImplT" , bound = BaseImpl [Any , Any , Any ])
57+
58+ ActionClientType : TypeAlias = _ActionClient [
59+ ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
60+ ]
61+ ClientGoalHandleType : TypeAlias = _ClientGoalHandle [
62+ ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
63+ ]
64+ ActionServerType : TypeAlias = _ActionServer [
65+ ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
66+ ]
67+ ServerGoalHandleType : TypeAlias = _ServerGoalHandle [
68+ ROSActionGoalT , ROSActionResultT , ROSActionFeedbackT , ROSActionImplT
69+ ]
0 commit comments