Attrs integration
provide.foundation.parsers.attrs_integration
¶
TODO: Add module docstring.
Functions¶
auto_parse
¶
Automatically parse value based on an attrs field's type and metadata.
This function first checks for a converter in the field's metadata, then falls back to type-based parsing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attr
|
Any
|
attrs field (from fields(Class)) |
required |
value
|
str
|
String value to parse |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Parsed value based on field type or converter |
Examples:
>>> from attrs import define, field, fields
>>> @define
... class Config:
... count: int = field()
... enabled: bool = field()
... custom: str = field(converter=lambda x: x.upper())
>>> c = Config(count=0, enabled=False, custom="")
>>> auto_parse(fields(Config).count, "42")
42
>>> auto_parse(fields(Config).enabled, "true")
True
>>> auto_parse(fields(Config).custom, "hello")
'HELLO'