attrs 16.3.0

Hynek Schlawack hs at
Sat Nov 26 08:57:13 EST 2016

Dear fellow Pythonistas,

I’m very happy to announce attrs 16.3.0, the Python library the makes you love writing classes again!  If you need a refresher why you should use attrs, have a look at “The One Python Library Everyone Needs” <> written by no one less than Glyph himself.

It’s still 100% free of magic but 100% full of convenience!

This version comes with two major new features I’d like to point out:

1. Attribute Metadata

There is a much nicer way to extend attrs now by using attribute metadata.  If you’re thinking Golang’s struct tags, it’s close!

However instead of exposing them to the users, it’s much nicer to *wrap* attrs and use metadata to bring the declarative approach to new areas.

For example this code will automatically load its configuration from env variables (as per 12Factor App: <>):

@app_config(prefix="APP", vault_prefix="WHOIS_{env}")
class WhoisConfig:
    class Prometheus:
        address = env_var(default="")
        port = env_var(default="0")
        consul_token = env_var_from_vault()

    env = env_var()
    prometheus = env_var_group(Prometheus)

if __name__ == '__main__':
    ac = environ_to_app_config(WhoisConfig)


Running it gives you:

WhoisConfig(env='dev', prometheus=Prometheus(address='', port='7000', consul_token='abc'))

(If you’re interested in the implementation of app_config, env_var, env_var_from_vault, env_var_group, and environ_to_app_config, have a look at <> – it’s 87 LoC including empty lines.  I may or may not release a PyPI package one day)

2. Post Initialization Hooks

This is an often requested feature (just look at all those enthusiastic bikeshed painters:  <>) and allows you to define code that runs after the generated __init__ method.

This should make life easier when subclassing third party classes or if you want to derive attributes from others.

Full Changelog

-   Attributes now can have user-defined metadata which greatly improves attrs's extensibility. #96
-   Allow for a __attrs_post_init__ method that -- if defined -- will get called at the end of the attrs-generated __init__ method. #111
-   Add @attr.s(str=True) that will optionally create a __str__ method that is identical to __repr__. This is mainly useful with Exceptions and other classes that rely on a useful __str__ implementation but overwrite the default one through a poor own one. Default Python class behavior is to use __repr__ as __str__ anyways.

    If you tried using attrs with Exceptions and were puzzled by the tracebacks: this option is for you.
-   Don't overwrite __name__ with __qualname__ for attr.s(slots=True) classes. #99


PyPI: <>
Documentation: <>
GitHub: <>


More information about the Python-announce-list mailing list