validation ¤
This module contains logic used to validate parameters passed to duties.
We validate the parameters before running the duties, effectively checking all CLI arguments and failing early if they are incorrect.
Classes:
-
ParamsCaster
–A helper class to cast parameters based on a function's signature annotations.
Functions:
-
cast_arg
–Cast an argument using a type annotation.
-
to_bool
–Convert a string to a boolean.
-
validate
–Validate positional and keyword arguments against a function.
ParamsCaster ¤
ParamsCaster(signature: Signature)
A helper class to cast parameters based on a function's signature annotations.
Parameters:
-
signature
(Signature
) –The signature to use to cast arguments.
Methods:
-
annotation_at_pos
–Give the annotation for the parameter at the given position.
-
cast
–Cast all positional and keyword arguments.
-
cast_kwarg
–Cast a keyword argument.
-
cast_posarg
–Cast a positional argument.
-
eaten_by_var_positional
–Tell if the parameter at this position is eaten by a variable positional parameter.
Attributes:
-
has_var_positional
(bool
) –Tell if there is a variable positional parameter.
-
var_keyword_annotation
(Any
) –Give the variable keyword parameter (
**kwargs
) annotation if any. -
var_positional_annotation
(Any
) –Give the variable positional parameter (
*args
) annotation if any. -
var_positional_position
(int
) –Give the position of the variable positional parameter in the signature.
Source code in src/duty/validation.py
70 71 72 73 74 75 76 77 |
|
var_keyword_annotation cached
property
¤
var_keyword_annotation: Any
Give the variable keyword parameter (**kwargs
) annotation if any.
Returns:
-
Any
–The variable keyword parameter annotation.
var_positional_annotation cached
property
¤
var_positional_annotation: Any
Give the variable positional parameter (*args
) annotation if any.
Returns:
-
Any
–The variable positional parameter annotation.
var_positional_position cached
property
¤
var_positional_position: int
Give the position of the variable positional parameter in the signature.
Returns:
-
int
–The position of the variable positional parameter.
annotation_at_pos ¤
Give the annotation for the parameter at the given position.
Parameters:
-
pos
(int
) –The position of the parameter.
Returns:
-
Any
–The positional parameter annotation.
Source code in src/duty/validation.py
121 122 123 124 125 126 127 128 129 130 |
|
cast ¤
Cast all positional and keyword arguments.
Parameters:
-
*args
(Any
, default:()
) –The positional arguments.
-
**kwargs
(Any
, default:{}
) –The keyword arguments.
Returns:
Source code in src/duty/validation.py
171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
cast_kwarg ¤
Cast a keyword argument.
Parameters:
Returns:
-
Any
–The cast value.
Source code in src/duty/validation.py
157 158 159 160 161 162 163 164 165 166 167 168 169 |
|
cast_posarg ¤
Cast a positional argument.
Parameters:
Returns:
-
Any
–The cast value.
Source code in src/duty/validation.py
143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
eaten_by_var_positional ¤
Tell if the parameter at this position is eaten by a variable positional parameter.
Parameters:
-
pos
(int
) –The position of the parameter.
Returns:
-
bool
–Whether the parameter is eaten.
Source code in src/duty/validation.py
132 133 134 135 136 137 138 139 140 141 |
|
cast_arg ¤
Cast an argument using a type annotation.
Parameters:
Returns:
-
Any
–The cast value.
Source code in src/duty/validation.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
to_bool ¤
Convert a string to a boolean.
Parameters:
-
value
(str
) –The string to convert.
Returns:
-
bool
–True or False.
Source code in src/duty/validation.py
29 30 31 32 33 34 35 36 37 38 |
|
validate ¤
Validate positional and keyword arguments against a function.
First we clone the function, removing the first parameter (the context) and the body, to fail early with a TypeError
if the arguments are incorrect: not enough, too much, in the wrong order, etc.
Then we cast all the arguments using the function's signature and we return them.
Parameters:
-
func
(Callable
) –The function to copy.
-
*args
(Any
, default:()
) –The positional arguments.
-
**kwargs
(Any
, default:{}
) –The keyword arguments.
Returns:
Source code in src/duty/validation.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|