|
1 | 1 | """Cattrs validation.""" |
2 | 2 |
|
3 | | -from typing import Any, Callable, List, TypeVar, Union, overload |
| 3 | +from typing import Any, Callable, List, Tuple, Type, TypeVar, Union, overload |
4 | 4 |
|
5 | 5 | from attrs import NOTHING, frozen |
6 | 6 |
|
|
38 | 38 | class VAnnotation: |
39 | 39 | """Use this with Annotated to get validation.""" |
40 | 40 |
|
41 | | - validators: tuple[Callable[[Any], Any]] |
| 41 | + validators: Tuple[Callable[[Any], Any]] |
| 42 | + |
| 43 | + def __init__(self, *validators: Callable[[Any], Any]): |
| 44 | + self.__attrs_init__(validators) |
42 | 45 |
|
43 | 46 |
|
44 | 47 | def format_exception(exc: BaseException, type: Union[type, None]) -> str: |
@@ -154,18 +157,18 @@ def transform_error( |
154 | 157 |
|
155 | 158 | @overload |
156 | 159 | def ensure( |
157 | | - type: type[list[T]], *validators: Callable[[list[T]], Any], elems: type[E] |
158 | | -) -> type[list[E]]: ... |
| 160 | + type: Type[List[T]], *validators: Callable[[List[T]], Any], elems: Type[E] |
| 161 | +) -> Type[List[E]]: ... |
159 | 162 |
|
160 | 163 |
|
161 | 164 | @overload |
162 | | -def ensure(type: type[T], *validators: Callable[[T], Any]) -> type[T]: ... |
| 165 | +def ensure(type: Type[T], *validators: Callable[[T], Any]) -> Type[T]: ... |
163 | 166 |
|
164 | 167 |
|
165 | 168 | def ensure(type: Any, *validators: Any, elems: Any = NOTHING) -> Any: |
166 | 169 | if elems is not NOTHING: |
167 | 170 | # These are lists. |
168 | 171 | if not validators: |
169 | 172 | return type[elems] |
170 | | - return Annotated[type, VAnnotation(validators)] |
171 | | - return Annotated[type, VAnnotation(validators)] |
| 173 | + return Annotated[type, VAnnotation(*validators)] |
| 174 | + return Annotated[type, VAnnotation(*validators)] |
0 commit comments