attrs 16.3.0

Hynek Schlawack hs at ox.cx
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” <https://glyph.twistedmatrix.com/2016/08/attrs.html> 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: <https://12factor.net/config>):

```
@app_config(prefix="APP", vault_prefix="WHOIS_{env}")
class WhoisConfig:
    @app_config
    class Prometheus:
        address = env_var(default="127.0.0.1")
        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)

    print(ac)
```

Running it gives you:

```
$ env APP_ENV=dev APP_PROMETHEUS_PORT=7000 SECRET_WHOIS_DEV_APP_PROMETHEUS_CONSUL_TOKEN=abc python app.py
WhoisConfig(env='dev', prometheus=Prometheus(address='127.0.0.1', 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 <https://gist.github.com/hynek/78fef47fea3cbc6b683cd8ab0fb68567> – 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:  <https://github.com/hynek/attrs/issues/68>) 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


Links
====

PyPI: <https://pypi.org/project/attrs/>
Documentation: <http://attrs.org/>
GitHub: <https://github.com/hynek/attrs>

Cheers,
Hynek


More information about the Python-announce-list mailing list