cattrs.preconf package#

cattrs.preconf.validate_datetime(v, _)[source]#
cattrs.preconf.wrap(_)[source]#

Wrap a Converter __init__ in a type-safe way.

Parameters:

_ (Callable[[~P], Any]) –

Return type:

Callable[[Callable[[…], T]], Callable[[~P], T]]

Submodules#

cattrs.preconf.bson module#

Preconfigured converters for bson.

class cattrs.preconf.bson.Base85Bytes[source]#

Bases: bytes

A subclass to help with binary key encoding/decoding.

class cattrs.preconf.bson.BsonConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, check_keys=False, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.UNSPECIFIED, unicode_decode_error_handler='strict', tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None), datetime_conversion=DatetimeConversion.DATETIME))[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • check_keys (bool) –

  • codec_options (CodecOptions) –

Return type:

bytes

loads(data, cl, codec_options=CodecOptions(document_class=dict, tz_aware=False, uuid_representation=UuidRepresentation.UNSPECIFIED, unicode_decode_error_handler='strict', tzinfo=None, type_registry=TypeRegistry(type_codecs=[], fallback_encoder=None), datetime_conversion=DatetimeConversion.DATETIME))[source]#
Parameters:
  • data (bytes) –

  • cl (Type[T]) –

  • codec_options (CodecOptions) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.bson.configure_converter(converter)[source]#

Configure the converter for use with the bson library.

  • sets are serialized as lists

  • byte mapping keys are base85-encoded into strings when unstructuring, and reverse

  • non-string, non-byte mapping keys are coerced into strings when unstructuring

  • a deserialization hook is registered for bson.ObjectId by default

Parameters:

converter (BaseConverter) –

cattrs.preconf.bson.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

BsonConverter

cattrs.preconf.cbor2 module#

Preconfigured converters for cbor2.

class cattrs.preconf.cbor2.Cbor2Converter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

bytes

loads(data, cl, **kwargs)[source]#
Parameters:
  • data (bytes) –

  • cl (Type[T]) –

  • kwargs (Any) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.cbor2.configure_converter(converter)[source]#

Configure the converter for use with the cbor2 library.

  • datetimes are serialized as timestamp floats

  • sets are serialized as lists

Parameters:

converter (BaseConverter) –

cattrs.preconf.cbor2.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

Cbor2Converter

cattrs.preconf.json module#

Preconfigured converters for the stdlib json.

class cattrs.preconf.json.JsonConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

str

loads(data, cl, **kwargs)[source]#
Parameters:
  • data (bytes | str) –

  • cl (Type[T]) –

  • kwargs (Any) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.json.configure_converter(converter)[source]#

Configure the converter for use with the stdlib json module.

  • bytes are serialized as base85 strings

  • datetimes are serialized as ISO 8601

  • counters are serialized as dicts

  • sets are serialized as lists

  • union passthrough is configured for unions of strings, bools, ints, floats and None

Parameters:

converter (BaseConverter) –

cattrs.preconf.json.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

JsonConverter

cattrs.preconf.msgpack module#

Preconfigured converters for msgpack.

class cattrs.preconf.msgpack.MsgpackConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

bytes

loads(data, cl, **kwargs)[source]#
Parameters:
  • data (bytes) –

  • cl (Type[T]) –

  • kwargs (Any) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.msgpack.configure_converter(converter)[source]#

Configure the converter for use with the msgpack library.

  • datetimes are serialized as timestamp floats

  • sets are serialized as lists

Parameters:

converter (BaseConverter) –

cattrs.preconf.msgpack.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

MsgpackConverter

cattrs.preconf.msgspec module#

Preconfigured converters for msgspec.

class cattrs.preconf.msgspec.MsgspecJsonConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

A converter specialized for the _msgspec_ library.

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

encoder: Encoder = <msgspec.json.Encoder object>#

The msgspec encoder for dumping.

dumps(obj, unstructure_as=None, **kwargs)[source]#

Unstructure and encode obj into JSON bytes.

Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

bytes

get_dumps_hook(unstructure_as, **kwargs)[source]#

Produce a dumps hook for the given type.

Parameters:
  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

Callable[[Any], bytes]

loads(data, cl, **kwargs)[source]#

Decode and structure cl from the provided JSON bytes.

Parameters:
  • data (bytes) –

  • cl (type[~T]) –

  • kwargs (Any) –

Return type:

T

get_loads_hook(cl)[source]#

Produce a loads hook for the given type.

Parameters:

cl (type[~T]) –

Return type:

Callable[[bytes], T]

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.msgspec.configure_converter(converter)[source]#

Configure the converter for the msgspec library.

  • bytes are serialized as base64 strings, directly by msgspec

  • datetimes and dates are passed through to be serialized as RFC 3339 directly

  • enums are passed through to msgspec directly

  • union passthrough configured for str, bool, int, float and None

Parameters:

converter (Converter) –

Return type:

None

cattrs.preconf.msgspec.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

MsgspecJsonConverter

cattrs.preconf.orjson module#

Preconfigured converters for orjson.

class cattrs.preconf.orjson.OrjsonConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

bytes

loads(data, cl)[source]#
Parameters:
  • data (bytes | bytearray | memoryview | str) –

  • cl (Type[T]) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.orjson.configure_converter(converter)[source]#

Configure the converter for use with the orjson library.

  • bytes are serialized as base85 strings

  • datetimes and dates are passed through to be serialized as RFC 3339 by orjson

  • typed namedtuples are serialized as lists

  • sets are serialized as lists

  • string enum mapping keys have special handling

  • mapping keys are coerced into strings when unstructuring

Parameters:

converter (BaseConverter) –

cattrs.preconf.orjson.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

OrjsonConverter

cattrs.preconf.pyyaml module#

Preconfigured converters for pyyaml.

cattrs.preconf.pyyaml.validate_date(v, _)[source]#
class cattrs.preconf.pyyaml.PyyamlConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

str

loads(data, cl)[source]#
Parameters:
  • data (str) –

  • cl (Type[T]) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.pyyaml.configure_converter(converter)[source]#

Configure the converter for use with the pyyaml library.

  • frozensets are serialized as lists

  • string enums are converted into strings explicitly

  • datetimes and dates are validated

  • typed namedtuples are serialized as lists

Parameters:

converter (BaseConverter) –

cattrs.preconf.pyyaml.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

PyyamlConverter

cattrs.preconf.tomlkit module#

Preconfigured converters for tomlkit.

class cattrs.preconf.tomlkit.TomlkitConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

str

loads(data, cl)[source]#
Parameters:
  • data (str) –

  • cl (Type[T]) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.tomlkit.configure_converter(converter)[source]#

Configure the converter for use with the tomlkit library.

  • bytes are serialized as base85 strings

  • sets are serialized as lists

  • tuples are serializas as lists

  • mapping keys are coerced into strings when unstructuring

Parameters:

converter (BaseConverter) –

cattrs.preconf.tomlkit.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

TomlkitConverter

cattrs.preconf.ujson module#

Preconfigured converters for ujson.

class cattrs.preconf.ujson.UjsonConverter(dict_factory=<class 'dict'>, unstruct_strat=UnstructureStrategy.AS_DICT, omit_if_default=False, forbid_extra_keys=False, type_overrides={}, unstruct_collection_overrides={}, prefer_attrib_converters=False, detailed_validation=True, unstructure_fallback_factory=<function Converter.<lambda>>, structure_fallback_factory=<function Converter.<lambda>>)[source]#

Bases: Converter

Parameters:
  • detailed_validation (bool) – Whether to use a slightly slower mode for detailed validation errors.

  • unstructure_fallback_factory (HookFactory[UnstructureHook]) – A hook factory to be called when no registered unstructuring hooks match.

  • structure_fallback_factory (HookFactory[StructureHook]) – A hook factory to be called when no registered structuring hooks match.

  • dict_factory (Callable[[], Any]) –

  • unstruct_strat (UnstructureStrategy) –

  • omit_if_default (bool) –

  • forbid_extra_keys (bool) –

  • type_overrides (Mapping[type, AttributeOverride]) –

  • unstruct_collection_overrides (Mapping[type, Callable]) –

  • prefer_attrib_converters (bool) –

New in version 23.2.0: unstructure_fallback_factory

New in version 23.2.0: structure_fallback_factory

dumps(obj, unstructure_as=None, **kwargs)[source]#
Parameters:
  • obj (Any) –

  • unstructure_as (Any) –

  • kwargs (Any) –

Return type:

str

loads(data, cl, **kwargs)[source]#
Parameters:
  • data (AnyStr) –

  • cl (Type[T]) –

  • kwargs (Any) –

Return type:

T

omit_if_default#
forbid_extra_keys#
type_overrides#
cattrs.preconf.ujson.configure_converter(converter)[source]#

Configure the converter for use with the ujson library.

  • bytes are serialized as base64 strings

  • datetimes are serialized as ISO 8601

  • sets are serialized as lists

Parameters:

converter (BaseConverter) –

cattrs.preconf.ujson.make_converter(*args, **kwargs)[source]#
Parameters:
  • args (Any) –

  • kwargs (Any) –

Return type:

UjsonConverter