Skip to content

Add ne validator#1571

Open
jamesbeith wants to merge 2 commits into
python-attrs:mainfrom
jamesbeith:jamesbeith/ne-validator
Open

Add ne validator#1571
jamesbeith wants to merge 2 commits into
python-attrs:mainfrom
jamesbeith:jamesbeith/ne-validator

Conversation

@jamesbeith
Copy link
Copy Markdown

@jamesbeith jamesbeith commented Jun 4, 2026

Summary

This change adds a validator that raises ValueError if the
initializer is called with a number equal to val.

An example use case. I want a field that is a signed int of any
value except for zero. I could do the following.

attrs.field(
    validator=attrs.validators.or_(
        attrs.validators.le(-1),
        attrs.validators.ge(1),
    )
)

With this proposed ne validator I could do.

attrs.field(validator=attrs.validators.ne(0))

Pull Request Check List

  • I acknowledge this project's AI policy.
  • This pull requests is not from my main branch.
  • There's tests for all new and changed code.
  • Changes or additions to public APIs are reflected in our type stubs (files ending in .pyi).
    • ...and used in the stub test file typing-examples/baseline.py or, if necessary, typing-examples/mypy.py.
    • If they've been added to attr/__init__.pyi, they've also been re-imported in attrs/__init__.pyi.
  • The documentation has been updated.
    • New functions/classes have to be added to docs/api.rst by hand.
    • Changes to the signatures of @attr.s() and @attrs.define() have to be added by hand too.
    • Changed/added classes/methods/functions have appropriate versionadded, versionchanged, or deprecated directives.
      The next version is the second number in the current release + 1.
      The first number represents the current year.
      So if the current version on PyPI is 26.2.0, the next version is gonna be 26.3.0.
      If the next version is the first in the new year, it'll be 27.1.0.
    • Documentation in .rst and .md files is written using semantic newlines.
  • Changes have news fragments in changelog.d.

This change adds a validator that raises `ValueError` if the
initializer is called with a number equal to *val*.

An example use case. I want a field that is a signed `int` of any
value except for zero. I could do the following.

```python
attrs.field(
    validator=attrs.validators.or_(
        attrs.validators.le(-1),
        attrs.validators.ge(1),
    )
)
```

With this proposed `ne` validator I could do.

```python
attrs.field(validator=attrs.validators.ne(0))
```
@jamesbeith jamesbeith force-pushed the jamesbeith/ne-validator branch from 80f4586 to 8538b59 Compare June 4, 2026 03:45
@jamesbeith jamesbeith marked this pull request as ready for review June 4, 2026 03:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant