|
1 |
| -from typing import TypeVar, overload, Callable, Any, Type, Optional |
| 1 | +from typing import TypeVar, overload, Callable, Any, Type, Optional, Union, Sequence, Mapping |
2 | 2 |
|
3 | 3 | _T = TypeVar('_T')
|
4 | 4 | _C = TypeVar('_C', bound=type)
|
5 | 5 |
|
| 6 | +_ValidatorType = Callable[[Any, Any, _T], Any] |
| 7 | +_ConverterType = Callable[[Any], _T] |
| 8 | +_FilterType = Callable[[Any, Any], bool] |
| 9 | +_ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]] |
| 10 | + |
| 11 | +# This form catches an explicit None or no default and infers the type from the other arguments. |
| 12 | +@overload |
| 13 | +def attrib(default: None = ..., |
| 14 | + validator: Optional[_ValidatorArgType[_T]] = ..., |
| 15 | + repr: bool = ..., |
| 16 | + cmp: bool = ..., |
| 17 | + hash: Optional[bool] = ..., |
| 18 | + init: bool = ..., |
| 19 | + convert: Optional[_ConverterType[_T]] = ..., |
| 20 | + metadata: Optional[Mapping[Any, Any]] = ..., |
| 21 | + type: Optional[Type[_T]] = ..., |
| 22 | + converter: Optional[_ConverterType[_T]] = ..., |
| 23 | + factory: Optional[Callable[[], _T]] = ..., |
| 24 | + ) -> _T: ... |
| 25 | +# This form catches an explicit default argument. |
6 | 26 | @overload
|
7 |
| -def attr(default: Optional[_T] = ..., |
8 |
| - validator: Optional[Any] = ..., |
9 |
| - repr: bool = ..., |
10 |
| - cmp: bool = ..., |
11 |
| - hash: Optional[bool] = ..., |
12 |
| - init: bool = ..., |
13 |
| - convert: Optional[Callable[[Any], _T]] = ..., |
14 |
| - metadata: Any = ..., |
15 |
| - type: Optional[Type[_T]] = ..., |
16 |
| - converter: Optional[Callable[[Any], _T]] = ...) -> _T: ... |
| 27 | +def attrib(default: _T, |
| 28 | + validator: Optional[_ValidatorArgType[_T]] = ..., |
| 29 | + repr: bool = ..., |
| 30 | + cmp: bool = ..., |
| 31 | + hash: Optional[bool] = ..., |
| 32 | + init: bool = ..., |
| 33 | + convert: Optional[_ConverterType[_T]] = ..., |
| 34 | + metadata: Optional[Mapping[Any, Any]] = ..., |
| 35 | + type: Optional[Type[_T]] = ..., |
| 36 | + converter: Optional[_ConverterType[_T]] = ..., |
| 37 | + factory: Optional[Callable[[], _T]] = ..., |
| 38 | + ) -> _T: ... |
| 39 | +# This form catches explicit None or no default but with no other arguments returns Any. |
17 | 40 | @overload
|
18 |
| -def attr(default: None = ..., |
19 |
| - validator: None = ..., |
20 |
| - repr: bool = ..., |
21 |
| - cmp: bool = ..., |
22 |
| - hash: Optional[bool] = ..., |
23 |
| - init: bool = ..., |
24 |
| - convert: Optional[Callable[[Any], _T]] = ..., |
25 |
| - metadata: Any = ..., |
26 |
| - type: None = ..., |
27 |
| - converter: None = ...) -> Any: ... |
| 41 | +def attrib(default: None = ..., |
| 42 | + validator: None = ..., |
| 43 | + repr: bool = ..., |
| 44 | + cmp: bool = ..., |
| 45 | + hash: Optional[bool] = ..., |
| 46 | + init: bool = ..., |
| 47 | + convert: None = ..., |
| 48 | + metadata: Optional[Mapping[Any, Any]] = ..., |
| 49 | + type: None = ..., |
| 50 | + converter: None = ..., |
| 51 | + factory: None = ..., |
| 52 | + ) -> Any: ... |
| 53 | +# This form covers type=non-Type: e.g. forward references (str), Any |
| 54 | +@overload |
| 55 | +def attrib(default: Optional[_T] = ..., |
| 56 | + validator: Optional[_ValidatorArgType[_T]] = ..., |
| 57 | + repr: bool = ..., |
| 58 | + cmp: bool = ..., |
| 59 | + hash: Optional[bool] = ..., |
| 60 | + init: bool = ..., |
| 61 | + convert: Optional[_ConverterType[_T]] = ..., |
| 62 | + metadata: Optional[Mapping[Any, Any]] = ..., |
| 63 | + type: object = ..., |
| 64 | + converter: Optional[_ConverterType[_T]] = ..., |
| 65 | + factory: Optional[Callable[[], _T]] = ..., |
| 66 | + ) -> Any: ... |
28 | 67 |
|
29 | 68 | @overload
|
30 |
| -def attributes(maybe_cls: _C, |
31 |
| - these: Optional[Any] = ..., |
32 |
| - repr_ns: Optional[str] = ..., |
33 |
| - repr: bool = ..., |
34 |
| - cmp: bool = ..., |
35 |
| - hash: Optional[bool] = ..., |
36 |
| - init: bool = ..., |
37 |
| - slots: bool = ..., |
38 |
| - frozen: bool = ..., |
39 |
| - str: bool = ..., |
40 |
| - auto_attribs: bool = ...) -> _C: ... |
| 69 | +def attrs(maybe_cls: _C, |
| 70 | + these: Optional[Mapping[str, Any]] = ..., |
| 71 | + repr_ns: Optional[str] = ..., |
| 72 | + repr: bool = ..., |
| 73 | + cmp: bool = ..., |
| 74 | + hash: Optional[bool] = ..., |
| 75 | + init: bool = ..., |
| 76 | + slots: bool = ..., |
| 77 | + frozen: bool = ..., |
| 78 | + str: bool = ..., |
| 79 | + auto_attribs: bool = ...) -> _C: ... |
41 | 80 | @overload
|
42 |
| -def attributes(maybe_cls: None = ..., |
43 |
| - these: Optional[Any] = ..., |
44 |
| - repr_ns: Optional[str] = ..., |
45 |
| - repr: bool = ..., |
46 |
| - cmp: bool = ..., |
47 |
| - hash: Optional[bool] = ..., |
48 |
| - init: bool = ..., |
49 |
| - slots: bool = ..., |
50 |
| - frozen: bool = ..., |
51 |
| - str: bool = ..., |
52 |
| - auto_attribs: bool = ...) -> Callable[[_C], _C]: ... |
| 81 | +def attrs(maybe_cls: None = ..., |
| 82 | + these: Optional[Mapping[str, Any]] = ..., |
| 83 | + repr_ns: Optional[str] = ..., |
| 84 | + repr: bool = ..., |
| 85 | + cmp: bool = ..., |
| 86 | + hash: Optional[bool] = ..., |
| 87 | + init: bool = ..., |
| 88 | + slots: bool = ..., |
| 89 | + frozen: bool = ..., |
| 90 | + str: bool = ..., |
| 91 | + auto_attribs: bool = ...) -> Callable[[_C], _C]: ... |
| 92 | + |
53 | 93 |
|
54 | 94 | # aliases
|
55 |
| -s = attrs = attributes |
56 |
| -ib = attrib = attr |
| 95 | +s = attributes = attrs |
| 96 | +ib = attr = attrib |
57 | 97 | dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)
|
0 commit comments