cattrs.gen package#
Submodules#
cattrs.gen.typeddicts module#
- cattrs.gen.typeddicts.make_dict_structure_fn(cl, converter, _cattrs_forbid_extra_keys='from_converter', _cattrs_use_linecache=True, _cattrs_detailed_validation='from_converter', **kwargs)[source]#
Generate a specialized dict structuring function for typed dicts.
- Parameters:
cl (Any) – A TypedDict class.
converter (BaseConverter) – A Converter instance to use for structuring nested fields.
kwargs (AttributeOverride) – A mapping of field names to an AttributeOverride, for customization.
_cattrs_detailed_validation (bool | Literal['from_converter']) – Whether to use a slower mode that produces more detailed errors.
_cattrs_forbid_extra_keys (bool | Literal['from_converter']) – Whether the structuring function should raise a ForbiddenExtraKeysError if unknown keys are encountered.
_cattrs_detailed_validation – Whether to store the generated code in the _linecache_, for easier debugging and better stack traces.
_cattrs_use_linecache (bool) –
- Return type:
Callable[[dict, Any], Any]
Changed in version 23.2.0: The _cattrs_forbid_extra_keys and _cattrs_detailed_validation parameters take their values from the given converter by default.
- cattrs.gen.typeddicts.make_dict_unstructure_fn(cl, converter, _cattrs_use_linecache=True, **kwargs)[source]#
Generate a specialized dict unstructuring function for a TypedDict.
- Parameters:
cl (type[T]) – A TypedDict class.
converter (BaseConverter) – A Converter instance to use for unstructuring nested fields.
kwargs (AttributeOverride) – A mapping of field names to an AttributeOverride, for customization.
_cattrs_detailed_validation – Whether to store the generated code in the _linecache_, for easier debugging and better stack traces.
_cattrs_use_linecache (bool) –
- Return type:
Callable[[T], dict[str, Any]]
Module contents#
- cattrs.gen.make_dict_structure_fn(cl, converter, _cattrs_forbid_extra_keys='from_converter', _cattrs_use_linecache=True, _cattrs_prefer_attrib_converters=False, _cattrs_detailed_validation='from_converter', _cattrs_use_alias=False, _cattrs_include_init_false=False, **kwargs)[source]#
Generate a specialized dict structuring function for an attrs class or dataclass.
- Parameters:
_cattrs_forbid_extra_keys (bool | Literal['from_converter']) – Whether the structuring function should raise a ForbiddenExtraKeysError if unknown keys are encountered.
_cattrs_detailed_validation (bool | Literal['from_converter']) – Whether to use a slower mode that produces more detailed errors.
_cattrs_use_alias (bool) – If true, the attribute alias will be used as the dictionary key by default.
_cattrs_include_init_false (bool) – If true, _attrs_ fields marked as init=False will be included.
cl (type[T]) –
converter (BaseConverter) –
_cattrs_use_linecache (bool) –
_cattrs_prefer_attrib_converters (bool) –
kwargs (AttributeOverride) –
- Return type:
DictStructureFn[T]
New in version 23.2.0: _cattrs_use_alias
New in version 23.2.0: _cattrs_include_init_false
Changed in version 23.2.0: The _cattrs_forbid_extra_keys and _cattrs_detailed_validation parameters take their values from the given converter by default.
- cattrs.gen.make_dict_unstructure_fn(cl, converter, _cattrs_omit_if_default=False, _cattrs_use_linecache=True, _cattrs_use_alias=False, _cattrs_include_init_false=False, **kwargs)[source]#
Generate a specialized dict unstructuring function for an attrs class or a dataclass.
- Parameters:
_cattrs_omit_if_default (bool) – if true, attributes equal to their default values will be omitted in the result dictionary.
_cattrs_use_alias (bool) – If true, the attribute alias will be used as the dictionary key by default.
_cattrs_include_init_false (bool) – If true, _attrs_ fields marked as init=False will be included.
cl (type[T]) –
converter (BaseConverter) –
_cattrs_use_linecache (bool) –
kwargs (AttributeOverride) –
- Return type:
Callable[[T], dict[str, Any]]
New in version 23.2.0: _cattrs_use_alias
New in version 23.2.0: _cattrs_include_init_false
- cattrs.gen.make_hetero_tuple_unstructure_fn(cl, converter, unstructure_to=None)[source]#
Generate a specialized unstructure function for a heterogenous tuple.
- Parameters:
cl (Any) –
converter (BaseConverter) –
unstructure_to (Any) –
- Return type:
HeteroTupleUnstructureFn
- cattrs.gen.make_iterable_unstructure_fn(cl, converter, unstructure_to=None)[source]#
Generate a specialized unstructure function for an iterable.
- Parameters:
cl (Any) –
converter (BaseConverter) –
unstructure_to (Any) –
- Return type:
IterableUnstructureFn
- cattrs.gen.make_mapping_structure_fn(cl, converter, structure_to=<class 'dict'>, key_type=_Nothing.NOTHING, val_type=_Nothing.NOTHING, detailed_validation=True)[source]#
Generate a specialized unstructure function for a mapping.
- Parameters:
cl (type[T]) –
converter (BaseConverter) –
structure_to (type) –
detailed_validation (bool) –
- Return type:
MappingStructureFn[T]
- cattrs.gen.make_mapping_unstructure_fn(cl, converter, unstructure_to=None, key_handler=None)[source]#
Generate a specialized unstructure function for a mapping.
- Parameters:
cl (Any) –
converter (BaseConverter) –
unstructure_to (Any) –
key_handler (Callable[[Any, Any | None], Any] | None) –
- Return type:
MappingUnstructureFn
- cattrs.gen.override(omit_if_default=None, rename=None, omit=None, struct_hook=None, unstruct_hook=None)[source]#
Override how a particular field is handled.
- Parameters:
omit (bool | None) – Whether to skip the field or not. None means apply default handling.
omit_if_default (bool | None) –
rename (str | None) –
struct_hook (Callable[[Any, Any], Any] | None) –
unstruct_hook (Callable[[Any], Any] | None) –
- Return type:
AttributeOverride