21
21
from typing import List
22
22
from typing import Optional
23
23
from typing import overload
24
- from typing import Sequence
25
24
from typing import Tuple
26
25
from typing import Type
27
26
from typing import TypeVar
@@ -164,7 +163,7 @@ def __init__(
164
163
"""
165
164
self .__handle = None
166
165
self ._context = get_default_context () if context is None else context
167
- self ._parameters : dict = {}
166
+ self ._parameters : Dict [ str , Parameter ] = {}
168
167
self ._publishers : List [Publisher ] = []
169
168
self ._subscriptions : List [Subscription ] = []
170
169
self ._clients : List [Client ] = []
@@ -179,8 +178,8 @@ def __init__(
179
178
self ._post_set_parameters_callbacks : List [Callable [[List [Parameter ]], None ]] = []
180
179
self ._rate_group = ReentrantCallbackGroup ()
181
180
self ._allow_undeclared_parameters = allow_undeclared_parameters
182
- self ._parameter_overrides : Dict [str , Parameter ] = {}
183
- self ._descriptors = {}
181
+ self ._parameter_overrides : Dict [str , Parameter [ AllowableParameterValue ] ] = {}
182
+ self ._descriptors : Dict [ str , ParameterDescriptor ] = {}
184
183
185
184
namespace = namespace or ''
186
185
if not self ._context .ok ():
@@ -357,20 +356,22 @@ def get_logger(self):
357
356
return self ._logger
358
357
359
358
@overload
360
- def declare_parameter (self , name : str , value : None = None ,
359
+ def declare_parameter (self , name : str ,
360
+ value : Union [None , Parameter .Type , ParameterValue ] = None ,
361
361
descriptor : Optional [ParameterDescriptor ] = None ,
362
362
ignore_override : bool = False ) -> Parameter [None ]: ...
363
363
364
364
@overload
365
- def declare_parameter (self , name : str , value : AllowableParameterValueT ,
365
+ def declare_parameter (self , name : str , value : Union [AllowableParameterValueT ,
366
+ Parameter .Type , ParameterValue ],
366
367
descriptor : Optional [ParameterDescriptor ] = None ,
367
368
ignore_override : bool = False
368
369
) -> Parameter [AllowableParameterValueT ]: ...
369
370
370
371
def declare_parameter (
371
372
self ,
372
373
name : str ,
373
- value : AllowableParameterValue = None ,
374
+ value : Union [ AllowableParameterValue , Parameter . Type , ParameterValue ] = None ,
374
375
descriptor : Optional [ParameterDescriptor ] = None ,
375
376
ignore_override : bool = False
376
377
) -> Parameter :
@@ -395,7 +396,8 @@ def declare_parameter(
395
396
"""
396
397
if value is None and descriptor is None :
397
398
# Temporal patch so we get deprecation warning if only a name is provided.
398
- args : Union [Tuple [str ], Tuple [str , AllowableParameterValue ,
399
+ args : Union [Tuple [str ], Tuple [str , Union [AllowableParameterValue ,
400
+ Parameter .Type , ParameterValue ],
399
401
ParameterDescriptor ]] = (name , )
400
402
else :
401
403
descriptor = ParameterDescriptor () if descriptor is None else descriptor
@@ -408,7 +410,8 @@ def declare_parameters(
408
410
parameters : List [Union [
409
411
Tuple [str ],
410
412
Tuple [str , Parameter .Type ],
411
- Tuple [str , AllowableParameterValue , ParameterDescriptor ],
413
+ Tuple [str , Union [AllowableParameterValue , Parameter .Type , ParameterValue ],
414
+ ParameterDescriptor ],
412
415
]],
413
416
ignore_override : bool = False
414
417
) -> List [Parameter ]:
@@ -483,9 +486,8 @@ def declare_parameters(
483
486
# Note(jubeira): declare_parameters verifies the name, but set_parameters doesn't.
484
487
validate_parameter_name (name )
485
488
486
- second_arg = parameter_tuple [1 ] if 1 < len (parameter_tuple ) else None
487
- descriptor = parameter_tuple [2 ] if 2 < len (parameter_tuple ) else ParameterDescriptor ()
488
-
489
+ second_arg = parameter_tuple [1 ] if len (parameter_tuple ) > 1 else None
490
+ descriptor = parameter_tuple [2 ] if len (parameter_tuple ) > 2 else ParameterDescriptor ()
489
491
if not isinstance (descriptor , ParameterDescriptor ):
490
492
raise TypeError (
491
493
f'Third element { descriptor } at index { index } in parameters list '
@@ -529,6 +531,10 @@ def declare_parameters(
529
531
if not ignore_override and name in self ._parameter_overrides :
530
532
value = self ._parameter_overrides [name ].value
531
533
534
+ if isinstance (value , ParameterValue ):
535
+ raise ValueError ('Cannot declare a Parameter from a ParameterValue without it'
536
+ 'being included _parameter_overrides, and ignore_override=False' )
537
+
532
538
parameter_list .append (Parameter (name , value = value ))
533
539
descriptors .update ({name : descriptor })
534
540
@@ -729,10 +735,7 @@ def get_parameter_or(
729
735
730
736
return self ._parameters [name ]
731
737
732
- def get_parameters_by_prefix (self , prefix : str ) -> Dict [str , Optional [Union [
733
- bool , int , float , str , bytes ,
734
- Sequence [bool ], Sequence [int ], Sequence [float ], Sequence [str ]
735
- ]]]:
738
+ def get_parameters_by_prefix (self , prefix : str ) -> Dict [str , Parameter ]:
736
739
"""
737
740
Get parameters that have a given prefix in their names as a dictionary.
738
741
@@ -1049,7 +1052,7 @@ def _check_undeclared_parameters(self, parameter_list: List[Parameter]):
1049
1052
if not self ._allow_undeclared_parameters and any (undeclared_parameters ):
1050
1053
raise ParameterNotDeclaredException (list (undeclared_parameters ))
1051
1054
1052
- def _call_pre_set_parameters_callback (self , parameter_list : [ List [Parameter ] ]):
1055
+ def _call_pre_set_parameters_callback (self , parameter_list : List [Parameter ]):
1053
1056
if self ._pre_set_parameters_callbacks :
1054
1057
modified_parameter_list = []
1055
1058
for callback in self ._pre_set_parameters_callbacks :
@@ -1059,7 +1062,7 @@ def _call_pre_set_parameters_callback(self, parameter_list: [List[Parameter]]):
1059
1062
else :
1060
1063
return None
1061
1064
1062
- def _call_post_set_parameters_callback (self , parameter_list : [ List [Parameter ] ]):
1065
+ def _call_post_set_parameters_callback (self , parameter_list : List [Parameter ]):
1063
1066
if self ._post_set_parameters_callbacks :
1064
1067
for callback in self ._post_set_parameters_callbacks :
1065
1068
callback (parameter_list )
0 commit comments