From fe29332abe03451cd9f26d21eb226c041b445d74 Mon Sep 17 00:00:00 2001 From: Michael Carlstrom Date: Wed, 27 Mar 2024 14:23:55 -0400 Subject: [PATCH] fix small bug Signed-off-by: Michael Carlstrom --- rclpy/rclpy/parameter.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/rclpy/rclpy/parameter.py b/rclpy/rclpy/parameter.py index 7ffdef046..b276e14a3 100644 --- a/rclpy/rclpy/parameter.py +++ b/rclpy/rclpy/parameter.py @@ -36,7 +36,12 @@ List[float], Tuple[float, ...], 'array.array[float]', List[str], Tuple[str, ...], 'array.array[str]'] -AllowableParameterValueT = TypeVar('AllowableParameterValueT', AllowableParameterValue, None) + +AllowableParameterValueT = TypeVar('AllowableParameterValueT', None, bool, int, float, str, + List[bytes], Tuple[bytes, ...], List[bool], Tuple[bool, ...], + List[int], Tuple[int, ...], 'array.array[int]', + List[float], Tuple[float, ...], 'array.array[float]', + List[str], Tuple[str, ...], 'array.array[str]') class Parameter(Generic[AllowableParameterValueT]): @@ -55,7 +60,8 @@ class Type(IntEnum): @classmethod def from_parameter_value(cls, - parameter_value: AllowableParameterValue) -> 'Parameter.Type': + parameter_value: AllowableParameterValueT + ) -> 'Parameter.Type': """ Get a Parameter.Type from a given variable. @@ -91,7 +97,7 @@ def from_parameter_value(cls, raise TypeError( f"The given value is not one of the allowed types '{parameter_value}'.") - def check(self, parameter_value: AllowableParameterValue) -> bool: + def check(self, parameter_value: AllowableParameterValueT) -> bool: if Parameter.Type.NOT_SET == self: return parameter_value is None if Parameter.Type.BOOL == self: @@ -143,9 +149,11 @@ def from_parameter_msg(cls, param_msg: ParameterMsg) -> 'Parameter[AllowablePara value = param_msg.value.string_array_value return cls(param_msg.name, type_, value) + # value: AllowableParameterValueT = None Needs a type: ignore due to TypeVars not inferring + # types of defaults yet https://github.com/python/mypy/issues/3737# def __init__(self, name: str, type_: Optional['Parameter.Type'] = None, - value: AllowableParameterValueT = None) -> None: + value: AllowableParameterValueT = None) -> None: # type: ignore[assignment] if type_ is None: # This will raise a TypeError if it is not possible to get a type from the value. type_ = Parameter.Type.from_parameter_value(value) @@ -158,7 +166,7 @@ def __init__(self, name: str, self._type_ = type_ self._name = name - self._value = value + self._value: AllowableParameterValueT = value @property def name(self) -> str: