API reference
fvattrs works by decorating a function with @define_io and defining how to convert and validate the function's argument(s) and output. It also offer define to convert/validate inline.
Core
define(value, converter=None, validator=None)
Apply converters and validators to a single value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Value to transform. |
required |
converter
|
None | Callable | Iterable[Callable]
|
Converter(s) to apply. |
None
|
validator
|
None | Callable | Iterable[Callable]
|
Validator(s) to apply after conversion. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
object |
object
|
The transformed value. |
Source code in src/fvattrs/_core.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
define_io(*parameters)
Decorate a callable to apply argument conversion and validation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*parameters
|
PositionalArgument | KeywordArgument | Output
|
Positional arguments, keyword arguments, or output descriptors describing how to process the wrapped callable. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
callable |
Callable
|
A decorator that wraps the target callable. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If duplicate indices, duplicate names, or multiple outputs are provided, or if any argument index is invalid or conflicts with keyword names. |
NotImplementedError
|
If the signature contains variable positional arguments or variable keyword arguments. |
Source code in src/fvattrs/_core.py
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | |
PositionalArgument
Bases: _Variable
Represent a positional argument in a callable signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Zero-based position of the argument in the wrapped callable. Defaults to 0. |
required |
converter
|
callable or iterable of callables
|
Converter(s) applied in order to the input value. |
required |
validator
|
callable or iterable of callables
|
Validator(s) applied to the transformed value. |
required |
Source code in src/fvattrs/_core.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
KeywordArgument
Bases: _Variable
Represent a keyword argument in a callable signature.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the keyword argument. |
required |
converter
|
callable or iterable of callables
|
Converter(s) applied in order to the input value. |
required |
validator
|
callable or iterable of callables
|
Validator(s) applied to the transformed value. |
required |
Source code in src/fvattrs/_core.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
Output
Bases: _Variable
Represent the output value produced by a callable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
converter
|
callable or iterable of callables
|
Converter(s) applied in order to the input value. |
required |
validator
|
callable or iterable of callables
|
Validator(s) applied to the transformed value. |
required |
Source code in src/fvattrs/_core.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
Converters
to_path(value)
Convert a value to a filesystem path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Value to convert. Must be a string or any object accepted by
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
path |
Path
|
A |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Source code in src/fvattrs/converters.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
to_date(value)
Convert a value to a datetime object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Value to convert. If this is already a |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
Parsed datetime value. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/fvattrs/converters.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | |
Validators
is_file(instance, attribute, value)
Validate that a value points to an existing file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
object
|
The instance being validated. |
required |
attribute
|
object
|
The attribute descriptor associated with the value. |
required |
value
|
object
|
Candidate value. Must be a |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/fvattrs/validators.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
is_folder(instance, attribute, value)
Validate that a value points to an existing directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
object
|
The instance being validated. |
required |
attribute
|
object
|
The attribute descriptor associated with the value. |
required |
value
|
object
|
Candidate value. Must be a |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in src/fvattrs/validators.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |