v23.2.0
23.2.0 (2023-11-17)
Welcome to cattrs 23.2.0! Thanks to all our wonderful contributors, this release happens to have the largest changelog so far.
Here are some of the noteworthy additions, see below for the entire changelog.
More Powerful Unions in preconf
Courtesy of the union passthrough strategy, the following class (and others like it, this is just a complex example) will work out-of-the-box on any preconf converter:
@define
class MyClass:
my_field: str | Literal[1] | MyOtherClass
The strategy has been preapplied to all preconf converters, but it can be manually applied to any converter.
Default Disambiguation via Literals
When structuring a union of attrs classes, cattrs will default to the default union strategy.
This strategy works by finding required unique fields in the given classes.
It has been enhanced with support for matching on fields annotated as Literals.
from typing import Literal
@define
class ClassA:
field_one: Literal["one"]
@define
class ClassB:
field_one: Literal["two"] = "two"
Un/structuring Hooks on Classes
cattrs defaults to having un/structuring hooks separate from the models.
With the use class methods strategy, you are free to defy this design decision and configure a converter to look for hooks on the models themselves.
The Complete Changelog
- Potentially breaking: skip attrs fields marked as
init=False
by default. This change is potentially breaking for unstructuring.
See here for instructions on how to restore the old behavior.
(#40 #395) - Potentially breaking:
cattrs.gen.make_dict_structure_fn
andcattrs.gen.typeddicts.make_dict_structure_fn
will use the values for thedetailed_validation
andforbid_extra_keys
parameters 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 values directly.
(#410 #411) - Potentially breaking: The default union structuring strategy will also use fields annotated as
typing.Literal
to help guide structuring.
See here for instructions on how to restore the old behavior.
(#391) - Python 3.12 is now supported. Python 3.7 is no longer supported; use older releases there.
(#424) - Implement the
union passthrough
strategy, enabling much richer union handling for preconfigured converters. Learn more here. - Introduce the
use_class_methods
strategy. Learn more here.
(#405) - The
omit
parameter ofcattrs.override
is now of typebool | None
(frombool
).
None
is the new default and means to apply default cattrs handling to the attribute, which is to omit the attribute if it's marked asinit=False
, and keep it otherwise. - Converters can now be initialized with custom fallback hook factories for un/structuring.
(#331 #441) - Add support for
date
to preconfigured converters.
(#420) - Add support for
datetime.date
s to the PyYAML preconfigured converter.
(#393) - Fix
format_exception()
parameter working for recursive calls totransform_error()
.
(#389) - attrs aliases are now supported, although aliased fields still map to their attribute name instead of their alias by default when un/structuring.
(#322 #391) - Fix TypedDicts with periods in their field names.
(#376 #377) - Optimize and improve unstructuring of
Optional
(unions of one type andNone
).
(#380 #381) - Fix
format_exception
andtransform_error
type annotations. - Improve the implementation of
cattrs._compat.is_typeddict
. The implementation is now simpler, and relies on fewer private implementation details fromtyping
and typing_extensions.
(#384) - Improve handling of TypedDicts with forward references.
- Speed up generated attrs and TypedDict structuring functions by changing their signature slightly.
(#388) - Fix copying of converters with function hooks.
(#398 #399) - Broaden
cattrs.preconf.orjson.OrjsonConverter.loads
type definition for the preconf orjson converter.
(#400) AttributeValidationNote
andIterableValidationNote
are now picklable.
(#408)- Fix structuring
Final
lists.
(#412) - Fix certain cases of structuring
Annotated
types.
(#418) - Fix the tagged union strategy to work with
forbid_extra_keys
.
(#402 #443) - Use PDM instead of Poetry.
- cattrs is now linted with Ruff.
- Remove some unused lines in the unstructuring code.
(#416) - Fix handling classes inheriting from non-generic protocols.
(#374 #436) - The documentation Makefile now supports the
htmlview
andhtmllive
targets. (#442) - cattrs is now published using PyPI Trusted Publishers, and
main
branch commits are automatically deployed to Test PyPI.