python-attrs / python-attrs/attrs

Mockup of quick expression validator syntax

Open
#346 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Thinking
Dominant language
Python
Stars
5.8k
Forks
480
Avg merge
2h 15m
Merged PRs (30d)
2

Description

Here's a quick mockup of the idea.

The idea is to provide a special token VAL that constructs validators using magic methods so you can write:

@attr.s
class Test:
    positive = attr.ib(validator=[VAL.isa(int, float), VAL > 0.0])
    nonempty = attr.ib(validator=[VAL.len() >= 1])

And, of course, it can report the expression in the exception it raises.

Things to support in a real version:

  • __getitem__ to check keys
  • __getattr__ to allow property and method checks
  • the other operators, especially in and such
  • __call__ validates in the mockup, but attr.ib should tell the object to convert itself to a validator

I'd like to see what the community thinks before putting together a proper PR.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked mockup and the existing attr.ib validator behavior. No implementation files or tests are named, and the requested scope includes multiple magic methods, operators, and conversion behavior. Done would require community agreement on the syntax and a properly scoped implementation with tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.