Skip to content

Proposal: nice way to write ad-hoc validators #143

@bluetech

Description

@bluetech

Hopefully this wasn't proposed before.

@attr.s
class C(object):
    x = attr.ib()
    y = attr.ib()

    @x.validator                                               
    def x_smaller_than_y(self, attribute, value):
        if value >= self.y:
            raise ValueError("'x' has to be smaller than 'y'!")

If you managed to guess what this does it means the syntax is intuitive enough :)

Similar syntax familiar from property.deleter and friends can also be useful for other parts, but for validators it feels like a natural fit (especially with the recent change to allow multiple validators).

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