diff --git a/fedbiomed/common/training_args.py b/fedbiomed/common/training_args.py index ee9171ab0..e9a8451f3 100644 --- a/fedbiomed/common/training_args.py +++ b/fedbiomed/common/training_args.py @@ -79,7 +79,7 @@ def __init__(self, ta: Dict = None, extra_scheme: Dict = None, only_required: bo # validators @staticmethod @validator_decorator - def _metric_validation_hook( metric: Union[MetricTypes, str, None] ): + def _metric_validation_hook( metric: Union[MetricTypes, str, None] ) -> Union[bool, str]: """ Validate the metric argument of test_metric. """ @@ -99,7 +99,7 @@ def _metric_validation_hook( metric: Union[MetricTypes, str, None] ): @staticmethod @validator_decorator - def _test_ratio_hook( v: Any): + def _test_ratio_hook( v: Any) -> bool: """ Test if in [ 0.0 , 1.0] interval. """ @@ -110,7 +110,7 @@ def _test_ratio_hook( v: Any): @staticmethod @validator_decorator - def _loss_rate_hook( v: Any): + def _loss_rate_hook( v: Any) -> bool: """ Test if lr is greater than 0. """ @@ -264,7 +264,7 @@ def __getitem__(self, key: str) -> Any: def update(self, values: Dict) -> TypeVar("TrainingArgs"): """ - Update multiple keys of the trainig arguments. + Update multiple keys of the training arguments. Args: values: a dictionnary of keys to validate/update @@ -290,9 +290,9 @@ def __ixor__(self, other: Dict) -> TypeVar("TrainingArgs"): t ^= { 'epochs': 2 , 'lr': 0.01 } ``` Args: - other: a dictionnary of keys to validate/update + other (Dict): a dictionnary of keys to validate/update - Return: + Returns: the object itself after modification Raises: @@ -318,7 +318,7 @@ def default_value(self, key: str) -> Any: Returns the default value for the key. Args: - key: key + key (str): key Returns: value: the default value associated to the key @@ -341,7 +341,7 @@ def default_value(self, key: str) -> Any: raise FedbiomedUserInputError(msg) - def dict(self): + def dict(self) -> Dict: """Returns the training_args as a dictionnary.""" if 'test_metric' in self._ta and \ diff --git a/fedbiomed/common/validator.py b/fedbiomed/common/validator.py index c6101440b..63c0cb2fa 100755 --- a/fedbiomed/common/validator.py +++ b/fedbiomed/common/validator.py @@ -37,13 +37,13 @@ def my_validation_funct( value ): This class provides json validation against a scheme describing the expected json content. -The scheme need to follow a specific format, which describe each +The scheme needs to follow a specific format, which describes each allowed fields and their characteristics: - a list of associated validators to check against (aka Validator instances) - the field requirement (required on not) - a default value (which will be used if the field is required but not provided) -A SchemeValidator is accepted byt the Validator class. +A SchemeValidator is accepted by the Validator class. **Typical example:** @@ -243,7 +243,7 @@ def validate(self, value: Dict) -> bool: Validate a value against the scheme passed at creation time. Args: - value: value (json) to validate against the scheme passed + value (dict): value (json) to validate against the scheme passed at __init__ Returns: True if value is valid @@ -279,20 +279,20 @@ def populate_with_defaults(self, value: Dict, only_required: bool = True) -> Dic """ Inject default values defined in the rule to a given dictionary. - Parse the given json value and add default value is key was required + Parse the given json value and add default value if key was required but not provided. - Of course, the default value must be provided in the scheme. + Of course, the default values must be provided in the scheme. Warning: this does not parse the result against the scheme. It has to be done by the user. Args: - value: a json data to verify/populate - only_required: if True, only force required key. If False, update all - keys with default values in the scheme + value (dict): a json data to verify/populate + only_required (bool): if True, only force required key. If False, update all + keys with default values in the scheme. Defaults to True. Return: - a json populated with default values, returns an empty dict if something is wrong + (dict) a json populated with default values, returns an empty dict if something is wrong Raises: RuleError: if scheme provided at init contains a required rules without default value @@ -449,7 +449,7 @@ def validate(self, value: Any, rule: Any, strict: bool = True) -> bool: True if rule exists and value is compliant. Raises: - ValidateError: if provided value does not com[ly to the rule + ValidateError: if provided value does not comply to the rule """ # rule is in the rulebook -> execute the rule associated function if isinstance(rule, str) and rule in self._validation_rulebook: @@ -605,13 +605,13 @@ def register(self, rule: str, hook: Any, override: bool = False) -> bool: Add a rule/validation_function to the rulebook. if the rule (entry of the catalog) was already registered, - it will be rejected, except if ovverride is True + it will be rejected, except if override is True Args: rule: registration name (string) hook: validation hook to register (the hook is checked against the accepted hook types) - override: if True, still register the rule even if it existed + override: if True, still register the rule even if it existed. Defaults to False. Returns: True if rule is accepted, False instead if rule exists and overrride is False diff --git a/tests/test_validator.py b/tests/test_validator.py index 01921c929..5edd8c40a 100644 --- a/tests/test_validator.py +++ b/tests/test_validator.py @@ -1,3 +1,4 @@ +from typing import Tuple import unittest from fedbiomed.common.validator import Validator, SchemeValidator, \ validator_decorator, _ValidatorHookType @@ -20,7 +21,7 @@ def tearDown(self): # define some validation hooks to add to the validator @staticmethod @validator_decorator - def hook_01_positive_integer_check(value): + def hook_01_positive_integer_check(value) -> bool: """ value must be a positive integer """ @@ -29,7 +30,7 @@ def hook_01_positive_integer_check(value): return True @staticmethod - def hook_02_positive_integer_check(value): + def hook_02_positive_integer_check(value) -> Tuple[bool, str]: """ value must be a positive integer