Skip to content

21.3.0

Compare
Choose a tag to compare
@hynek hynek released this 28 Dec 06:58
· 587 commits to main since this release
21.3.0

This is a big release in the history of attrs and finishes an arc that took way too long and also delayed this very overdue release. But it's done: import attrs that has been talked about for years1, but fell victim to “just this one more thing” has finally landed.

From now on, modern attrs code looks like this:

from attrs import define

@define
class HelloWorld:
    modern: bool = True

The define/field APIs have been around for over a year and were very popular, now the rest of the package followed suit. I'm very excited that attrs remains relevant and keeps evolving over now more than half a decade. If you're curious about some of the background, the docs now contain a short explanation and history lesson. As long as our users keep pushing us, we will keep pushing forward class generation in Python!

Big thanks to my GitHub Sponsors, Tidelift subscribers, and Ko-fi buyers that help me mustering the motivation for such long-running project!


Since the release took so long, there's more highlights than we can enumerate here, we'd just like to point out a breaking change in the new APIs: converters now run on setting attributes by default. If this is causing problems to you, you can disable that behavior by setting @define(on_setattr=[]).

Full Changelog

Backward-incompatible Changes

  • When using @define, converters are now run by default when setting an attribute on an instance -- additionally to validators. I.e. the new default is on_setattr=[attrs.setters.convert, attrs.setters.validate].

    This is unfortunately a breaking change, but it was an oversight, impossible to raise a DeprecationWarning about, and it's better to fix it now while the APIs are very fresh with few users. #835, #886

  • import attrs has finally landed! As of this release, you can finally import attrs using its proper name.

    Not all names from the attr namespace have been transferred; most notably attr.s and attr.ib are missing. See attrs.define and attrs.field if you haven't seen our next-generation APIs yet. A more elaborate explanation can be found On The Core API Names

    This feature is at least for one release provisional. We don't plan on changing anything, but such a big change is unlikely to go perfectly on the first strike.

    The API docs have been mostly updated, but it will be an ongoing effort to change everything to the new APIs. Please note that we have not moved -- or even removed -- anything from attr!

    Please do report any bugs or documentation inconsistencies! #887

Changes

  • attr.asdict(retain_collection_types=False) (default) dumps collection-esque keys as tuples. #646, #888
  • __match_args__ are now generated to support Python 3.10's Structural Pattern Matching. This can be controlled by the match_args argument to the class decorators on Python 3.10 and later. On older versions, it is never added and the argument is ignored. #815
  • If the class-level on_setattr is set to attrs.setters.validate (default in @define and @mutable) but no field defines a validator, pretend that it's not set. #817
  • The generated __repr__ is significantly faster on Pythons with f-strings. #819
  • Attributes transformed via field_transformer are wrapped with AttrsClass again. #824
  • Generated source code is now cached more efficiently for identical classes. #828
  • Added attrs.converters.to_bool(). #830
  • attrs.resolve_types() now resolves types of subclasses after the parents are resolved. #842 #843
  • Added new validators: lt(val) (< val), le(va) (≤ val), ge(val) (≥ val), gt(val) (> val), and maxlen(n). #845
  • attrs classes are now fully compatible with cloudpickle (no need to disable repr anymore). #857
  • Added new context manager attrs.validators.disabled() and functions attrs.validators.(set|get)_disabled(). They deprecate attrs.(set|get)_run_validators(). All functions are interoperable and modify the same internal state. They are not – and never were – thread-safe, though. #859
  • attrs.validators.matches_re() now accepts pre-compiled regular expressions in addition to pattern strings. #877
  1. I have an issue from 2018 that I wanted to "come back the moment this lands".