Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1496.change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix type annotations for `attrs.validators.optional()`, so it no longer rejects tuples with more than one validator.
2 changes: 1 addition & 1 deletion src/attr/validators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def optional(
validator: (
_ValidatorType[_T]
| list[_ValidatorType[_T]]
| tuple[_ValidatorType[_T]]
| tuple[_ValidatorType[_T], ...]
),
) -> _ValidatorType[_T | None]: ...
def in_(options: Container[_T]) -> _ValidatorType[_T]: ...
Expand Down
9 changes: 9 additions & 0 deletions typing-examples/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ class Validated:
num: int = attrs.field(validator=attrs.validators.ge(0))


@attrs.define
class ValidatedOptionalOverTuple:
num: int | None = attrs.field(
validator=attrs.validators.optional(
(attrs.validators.instance_of(int), attrs.validators.ge(0)) # ty:ignore [invalid-argument-type]
)
)


@attrs.define
class ValidatedInconsistentOr:
num: int | str = attrs.field(
Expand Down
Loading