Skip to content

Commit eccd966

Browse files
finite-state-machinepre-commit-ci[bot]hynek
authored
Fix optional validator to accept tuples of len > 1 (#1496)
* Fix `optional` validator to accept tuples of len > 1 * Add changelog * Add `optional` validator over 2-tuple test case to `baseline.py` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Work around `ty` false positive `invalid-argument-type` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Revert "fixes" from pre-commit.com hooks [skip pre-commit.ci] This reverts commit 5b41382. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Silence ty ref astral-sh/ty#3016 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hynek Schlawack <hs@ox.cx>
1 parent e92fe52 commit eccd966

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

changelog.d/1496.change.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix type annotations for `attrs.validators.optional()`, so it no longer rejects tuples with more than one validator.

src/attr/validators.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def optional(
5454
validator: (
5555
_ValidatorType[_T]
5656
| list[_ValidatorType[_T]]
57-
| tuple[_ValidatorType[_T]]
57+
| tuple[_ValidatorType[_T], ...]
5858
),
5959
) -> _ValidatorType[_T | None]: ...
6060
def in_(options: Container[_T]) -> _ValidatorType[_T]: ...

typing-examples/baseline.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ class Validated:
9494
num: int = attrs.field(validator=attrs.validators.ge(0))
9595

9696

97+
@attrs.define
98+
class ValidatedOptionalOverTuple:
99+
num: int | None = attrs.field(
100+
validator=attrs.validators.optional(
101+
(attrs.validators.instance_of(int), attrs.validators.ge(0)) # ty:ignore [invalid-argument-type]
102+
)
103+
)
104+
105+
97106
@attrs.define
98107
class ValidatedInconsistentOr:
99108
num: int | str = attrs.field(

0 commit comments

Comments
 (0)