Skip to main content

Integrating extra metadata into attr.ib()

Project description

PyPI version supported Python versions Travis build status source on GitHub

Integrating extra metadata into attr.ib()


With a little luck a better example will be provided later but for now, here’s something.

A dev in #python was interested in having click build them attrs-defined configuration objects. Here’s a basic solution for that with the click options being defined on the attrs class attributes via a custom exttr keyword argument click=.

import collections
import sys

import attr
import click
import exttr


class Configuration:
    foo = exttr.ib(click=click.option('--red'))

def main(configuration):

def clicked_fields(cls):
    fields = collections.OrderedDict()

    for field in attr.fields(cls):
        decorator = exttr.get(cls,, 'click')

        if decorator is None:

        fields[] = decorator

    return fields

def build_click(f, cls, command_or_group):
    fields = clicked_fields(cls)

    def cli(*args, **kwargs):
        configuration = cls(*args, **kwargs)

        return f(configuration)

    for name, decorator in reversed(fields.items()):
        before = getattr(cli, '__click_params__', [])

        cli = decorator(cli)

        after = getattr(cli, '__click_params__', [])
        new = after[len(before):]

        if len(new) == 1:
            new, = new
   = name

    return command_or_group(cli)

click_main = build_click(

sys.argv[1:] = ['--red', 'burgundy']
except SystemExit:



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for exttr, version 2019.2.2
Filename, size File type Python version Upload date Hashes
Filename, size exttr-2019.2.2-py3-none-any.whl (6.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size exttr-2019.2.2.tar.gz (20.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page