Skip to content

Releases

v26.2.1

Choose a tag to compare

@Tinche Tinche released this 26 Sep 20:52

What's Changed

  • Fix heterogeneous tuples and NamedTuples with a member type containing a quote in its repr, like tuple[Literal["a"], int], crashing structuring code generation with SyntaxError; the index note is now embedded with repr. (#777)
  • Fix transform_error listing the extra keys of a ForbiddenExtraKeysError in set iteration order, which made the message differ between runs; the keys are now sorted, like the error's own __str__ already sorts them. (#776)

New Contributors

Full Changelog: v26.2.0...v26.2.1

v26.2.0

Choose a tag to compare

@Tinche Tinche released this 06 Sep 20:31

What's Changed

  • Fix the msgpack and cbor2 converters unstructuring naive datetimes as local time, which made the serialized value depend on the timezone of the machine doing the unstructuring; naive datetimes are now assumed to be UTC, matching what the structure hooks already read back. (#774 #775)
  • Fix override(rename=...) targets containing a quote (or other characters not safe in a bare string literal) crashing code generation with SyntaxError; the rename key is now embedded with repr. (#771)
  • Fix Counter keys not being unstructured with the key type's own hook; the single-type-arg branch passed the whole type-args tuple to the key hook lookup instead of the key type. (#768)
  • Fix create_default_dis_func (aka create_uniq_field_dis_func) failing to disambiguate valid unions depending on the order of the member classes; unique fields are now resolved iteratively to a fixpoint. (#230 #765)
  • Support more recursive types on 3.14+ with specialized factories for annotationlib.ForwardRef. (#740 #741)
  • Converter now uses specialized hook factories to generate hooks for tuples, increasing speed. (#737)
  • Fix an AttributeError in cattrs internals that could be triggered by using the include_subclasses strategy in a structure_hook_factory (#721, #722)
  • Fix TypedDict codegen when keys contain single quotes. (#769)
  • Add CattrsError exception type: all exceptions raised by cattrs inherit from this.
    Literal and date-time validation raise this directly, instead of Exception. (#728)
  • Fix the detailed_validation parameter being passed under the wrong name in namedtuple_dict_structure_factory, causing it to be silently ignored. (#723)
  • cattrs is now autoformatted using Ruff. (#732)
  • Support running the test suite without cbor2 installed. (#748)
  • The union passthrough strategy now supports PEP 695 type aliases as union members. (#753)
  • BaseConverter.register_structure_hook_factory and BaseConverter.register_unstructure_hook_factory now properly return the factory when used as decorators. (#724)
  • The msgspec preconf converter now properly handles recursive classes on Python 3.14+. (#757)

New Contributors

Full Changelog: v26.1.0...v26.2.0

v26.1.0

Choose a tag to compare

@Tinche Tinche released this 18 Feb 22:14

v26.1.0

  • Add the tomllib preconf converter. See here for details. (#716)
  • Customizing un/structuring of attrs classes, dataclasses, TypedDicts and dict NamedTuples is now possible by using Annotated[T, override()] on fields. See here for more details. (#717)
  • Fix structuring of nested generic classes with stringified annotations. (#688)
  • Python 3.9 is no longer supported, as it is end-of-life. Use previous versions on this Python version. (#698)
  • Apply the attrs converter to the default value before checking if it is equal to the attribute's value, when omit_if_default is true and an attrs converter is specified. (#696)
  • Use the optional _value_ type hint to structure and unstructure enums if present. (#699)
  • Aliases (when in use) now properly generate rename metadata in generated hooks. (#706 #710)
  • cattrs now tracks performance using codspeed. (#703)
  • The tomlkit preconf converter now properly handles native date objects when structuring. (#707 #708)
  • The tomlkit preconf converter now passes date objects directly to tomlkit for unstructuring. (#707 #708)
  • Enum handling has been optimized by switching to hook factories, improving performance especially for plain enums. (#705)
  • Fix cattrs.strategies.include_subclasses when used with cattrs.strategies.configure_tagged_union and classes using diamond inheritance. (#685 #713)
  • Fix cattrs.strategies.configure_tagged_union when used with recursive type aliases. (#678 #714)

New Contributors

Full Changelog: v25.3.0...v26.1.0

v25.3.0

Choose a tag to compare

@Tinche Tinche released this 07 Oct 12:25
  • Potentially breaking: Abstract sets are now structured into frozensets.
    This allows hashability, better immutability and is more consistent with the collections.abc.Set type.
    See Migrations for steps to restore legacy behavior. (#686)
  • Python 3.14 is now supported and part of the test matrix. (#653)
  • Fix unstructuring NewTypes with the {class}BaseConverter. (#684)
  • Make some Hypothesis tests more robust. (#684)
  • {func}cattrs.strategies.include_subclasses now works with generic parent classes and the tagged union strategy. (#683)

New Contributors

Full Changelog: v25.2.0...v25.3.0

v25.2.0

Choose a tag to compare

@Tinche Tinche released this 31 Aug 20:41
  • Potentially breaking: Sequences are now structured into tuples.
    This allows hashability, better immutability and is more consistent with the collections.abc.Sequence type. See Migrations for steps to restore legacy behavior. (#663)
  • Add a use_alias parameter to cattrs.Converter.
    cattrs.gen.make_dict_unstructure_fn_from_attrs, cattrs.gen.make_dict_unstructure_fn,
    cattrs.gen.make_dict_structure_fn_from_attrs, cattrs.gen.make_dict_structure_fn
    and cattrs.gen.typeddicts.make_dict_structure_fn will use the value for the use_alias parameter from the given converter by default now. If you're using these functions directly, the old behavior can be restored by passing in the desired value directly. (#596 #660)
  • The union passthrough strategy now by default accepts ints for unions that contain floats but not ints,
    when configured to be able to handle both ints and floats. This more closely matches the current typing behavior. (#656 #668)
  • Fix unstructuring of generic classes with stringified annotations. (#661 #662)
  • For cattrs.errors.StructureHandlerNotFoundError and cattrs.errors.ForbiddenExtraKeysError
    correctly set BaseException.args in super() and hence make them pickable. (#666)
  • The default disambiguation hook factory is now only enabled for converters with unstructure_strat=AS_DICT (the default).
    Since the strategy doesn't support tuples, it is skipped for unstructure_strat=AS_TUPLE converters. (#673)
  • Switch to uv and just in lieu of PDM, tox and Make. See the Contributing section for new workflow instructions. (#671)

New Contributors

Full Changelog: v25.1.1...v25.2.0

v25.1.1

Choose a tag to compare

@Tinche Tinche released this 04 Jun 20:26
  • Fixed AttributeError: no attribute '__parameters__' while structuring attrs classes that inherit from parametrized generic aliases from collections.abc.
    (#654 #655)

v25.1.0

Choose a tag to compare

@Tinche Tinche released this 31 May 09:57
  • Potentially breaking: The converters raise StructureHandlerNotFoundError more eagerly (on hook creation, instead of on hook use). This helps surfacing problems with missing hooks sooner. See Migrations for steps to restore legacy behavior.
    (#577)
  • Add a Migrations page, with instructions on migrating changed behavior for each version.
    (#577)
  • typing.Self is now supported in attrs classes, dataclasses, TypedDicts and the dict NamedTuple factories. See typing.Self for details.
    (#299 #627)
  • PEP 695 type aliases can now be used with BaseConverter.register_structure_hook and BaseConverter.register_unstructure_hook. Previously, they required the use of BaseConverter.register_structure_hook_func (which is still supported).
    (#647)
  • Expose cattrs.cols.mapping_unstructure_factory through cattrs.cols.
  • Some defaultdicts are now supported by default, and
    cattrs.cols.is_defaultdict and cattrs.cols.defaultdict_structure_factory are exposed through cattrs.cols.
    (#519 #588)
  • Generic PEP 695 type aliases are now supported.
    (#611 #618)
  • The tagged union strategy now also supports type aliases of unions.
    (#649)
  • Converter.copy and BaseConverter.copy are correctly annotated as returning Self.
    (#644)
  • Many preconf converters (bson, stdlib JSON, cbor2, msgpack, msgspec, orjson, ujson) skip unstructuring int and str enums, leaving them to the underlying libraries to handle with greater efficiency.
    (#598)
  • The msgspec JSON preconf converter now handles dataclasses with private attributes more efficiently.
    (#624)
  • Literals containing enums are now unstructured properly, and their unstructuring is greatly optimized in the bson, stdlib JSON, cbor2, msgpack, msgspec, orjson and ujson preconf converters.
    (#598)
  • Preconf converters now handle dictionaries with literal keys properly.
    (#599)
  • Structuring TypedDicts from invalid inputs now properly raises a ClassValidationError.
    (#615 #616)
  • cattrs.strategies.include_subclasses now properly works with generic parent classes.
    (#649)
  • Replace cattrs.gen.MappingStructureFn with cattrs.SimpleStructureHook.
  • Python 3.13 is now supported.
    (#543 #547)
  • Python 3.8 is no longer supported, as it is end-of-life. Use previous versions on this Python version.
    (#591)
  • Change type of Converter.__init__.unstruct_collection_overrides from Callable to Mapping[type, UnstructureHook]
    (#594).
  • Adopt the Contributor Covenant Code of Conduct (just like attrs).

v24.1.3

Choose a tag to compare

@Tinche Tinche released this 25 Mar 15:00
  • Fix structuring of keyword-only dataclass fields when not using detailed validation.
    (#637 #638)

v24.1.2

Choose a tag to compare

@Tinche Tinche released this 22 Sep 14:57
  • Fix BaseConverter.register_structure_hook and BaseConverter.register_unstructure_hook type hints.
    (#581 #582)

24.1.1

Choose a tag to compare

@Tinche Tinche released this 11 Sep 12:07

24.1.1 (2024-09-11)

  • Fix BaseConverter.register_structure_hook_factory and BaseConverter.register_unstructure_hook_factory type hints.
    (#578 #579)