Skip to content

Commit

Permalink
fix small bug
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Carlstrom <rmc@carlstrom.com>
  • Loading branch information
InvincibleRMC committed Mar 27, 2024
1 parent 6b5b793 commit fe29332
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions rclpy/rclpy/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit fe29332

Please sign in to comment.