Skip to content

User-supplied metadata for attr.ib #63

@wearpants

Description

@wearpants

It might be desirable to allow user-supplied metadata dict as part of an Attribute for use later during introspection. Something like:

@attr.s
class A:

    boring = attr.ib()
    i_am_special = attr.ib(metadata = {'special': True})

    def do_things(self):
        for a in attr.fields(type(self)):
            if a.metadata.get('special', False):
                self.do_something(a.name)
            else:
                self.do_something_else(b.name)

(above example is assuming attrs populates metadata with an empty dict if not provided.

Perhaps this is encourages more metaprogramming than is healthy, but I had a need for this in my first use of attrs; the alternative was keeping an explicit list of special attribute names, which is not very DRY. Just looking for design/desirability feedback before working on a patch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions