Skip to content

Commit f8f7a2e

Browse files
ProfDoofpre-commit-ci[bot]hynek
authored
Improve type hinting of or_ (#1471)
* Improve type hinting of instance_of and or_ * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Moved _U to same location as all other TypeVar definitions Added a new TypeVar '_U' bound to UnionType for type validation. * Revert to UnionType * Add (failing) typing example * Oops, the type checkers were just smarter * Add news fragment --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Hynek Schlawack <[email protected]>
1 parent b1dc8f6 commit f8f7a2e

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

changelog.d/1474.change.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The type annotation for `attrs.validators.or_()` now allows for different types of validators.
2+
3+
This was only an issue on Pyright.

src/attr/validators.pyi

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ _T = TypeVar("_T")
2020
_T1 = TypeVar("_T1")
2121
_T2 = TypeVar("_T2")
2222
_T3 = TypeVar("_T3")
23+
_T4 = TypeVar("_T4")
24+
_T5 = TypeVar("_T5")
25+
_T6 = TypeVar("_T6")
2326
_I = TypeVar("_I", bound=Iterable)
2427
_K = TypeVar("_K")
2528
_V = TypeVar("_V")
@@ -90,4 +93,48 @@ def not_(
9093
msg: str | None = None,
9194
exc_types: type[Exception] | Iterable[type[Exception]] = ...,
9295
) -> _ValidatorType[_T]: ...
93-
def or_(*validators: _ValidatorType[_T]) -> _ValidatorType[_T]: ...
96+
@overload
97+
def or_(
98+
__v1: _ValidatorType[_T1],
99+
__v2: _ValidatorType[_T2],
100+
) -> _ValidatorType[_T1 | _T2]: ...
101+
@overload
102+
def or_(
103+
__v1: _ValidatorType[_T1],
104+
__v2: _ValidatorType[_T2],
105+
__v3: _ValidatorType[_T3],
106+
) -> _ValidatorType[_T1 | _T2 | _T3]: ...
107+
@overload
108+
def or_(
109+
__v1: _ValidatorType[_T1],
110+
__v2: _ValidatorType[_T2],
111+
__v3: _ValidatorType[_T3],
112+
__v4: _ValidatorType[_T4],
113+
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4]: ...
114+
@overload
115+
def or_(
116+
__v1: _ValidatorType[_T1],
117+
__v2: _ValidatorType[_T2],
118+
__v3: _ValidatorType[_T3],
119+
__v4: _ValidatorType[_T4],
120+
__v5: _ValidatorType[_T5],
121+
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5]: ...
122+
@overload
123+
def or_(
124+
__v1: _ValidatorType[_T1],
125+
__v2: _ValidatorType[_T2],
126+
__v3: _ValidatorType[_T3],
127+
__v4: _ValidatorType[_T4],
128+
__v5: _ValidatorType[_T5],
129+
__v6: _ValidatorType[_T6],
130+
) -> _ValidatorType[_T1 | _T2 | _T3 | _T4 | _T5 | _T6]: ...
131+
@overload
132+
def or_(
133+
__v1: _ValidatorType[Any],
134+
__v2: _ValidatorType[Any],
135+
__v3: _ValidatorType[Any],
136+
__v4: _ValidatorType[Any],
137+
__v5: _ValidatorType[Any],
138+
__v6: _ValidatorType[Any],
139+
*validators: _ValidatorType[Any],
140+
) -> _ValidatorType[Any]: ...

typing-examples/baseline_examples.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ class Validated:
9393
num: int = attrs.field(validator=attrs.validators.ge(0))
9494

9595

96+
@attrs.define
97+
class ValidatedInconsistentOr:
98+
num: int | str = attrs.field(
99+
validator=attrs.validators.or_(
100+
# Various types of validators.
101+
attrs.validators.ge(0),
102+
attrs.validators.instance_of(str),
103+
)
104+
)
105+
106+
96107
attrs.validators.set_disabled(True)
97108
attrs.validators.set_disabled(False)
98109

0 commit comments

Comments
 (0)