1
- from typing import Any , Callable , Dict , Generic , List , Optional , Sequence , Mapping , Tuple , Type , TypeVar , Union , overload
1
+ from typing import (
2
+ Any ,
3
+ Callable ,
4
+ Dict ,
5
+ Generic ,
6
+ List ,
7
+ Optional ,
8
+ Sequence ,
9
+ Mapping ,
10
+ Tuple ,
11
+ Type ,
12
+ TypeVar ,
13
+ Union ,
14
+ overload ,
15
+ )
16
+
2
17
# `import X as X` is required to make these public
3
18
from . import exceptions as exceptions
4
19
from . import filters as filters
5
20
from . import converters as converters
6
21
from . import validators as validators
7
22
8
- _T = TypeVar ('_T' )
9
- _C = TypeVar ('_C' , bound = type )
23
+ _T = TypeVar ("_T" )
24
+ _C = TypeVar ("_C" , bound = type )
10
25
11
- _ValidatorType = Callable [[Any , Attribute , _T ], Any ]
26
+ _ValidatorType = Callable [[Any , Attribute [ _T ] , _T ], Any ]
12
27
_ConverterType = Callable [[Any ], _T ]
13
- _FilterType = Callable [[Attribute , Any ], bool ]
28
+ _FilterType = Callable [[Attribute [ _T ], _T ], bool ]
14
29
# FIXME: in reality, if multiple validators are passed they must be in a list or tuple,
15
30
# but those are invariant and so would prevent subtypes of _ValidatorType from working
16
31
# when passed in a list or tuple.
@@ -25,7 +40,10 @@ NOTHING: object
25
40
@overload
26
41
def Factory (factory : Callable [[], _T ]) -> _T : ...
27
42
@overload
28
- def Factory (factory : Union [Callable [[Any ], _T ], Callable [[], _T ]], takes_self : bool = ...) -> _T : ...
43
+ def Factory (
44
+ factory : Union [Callable [[Any ], _T ], Callable [[], _T ]],
45
+ takes_self : bool = ...,
46
+ ) -> _T : ...
29
47
30
48
class Attribute (Generic [_T ]):
31
49
name : str
@@ -38,30 +56,26 @@ class Attribute(Generic[_T]):
38
56
converter : Optional [_ConverterType [_T ]]
39
57
metadata : Dict [Any , Any ]
40
58
type : Optional [Type [_T ]]
41
- def __lt__ ( self , x : Attribute ) -> bool : ...
42
- def __le__ (self , x : Attribute ) -> bool : ...
43
- def __gt__ (self , x : Attribute ) -> bool : ...
44
- def __ge__ (self , x : Attribute ) -> bool : ...
45
-
59
+ kw_only : bool
60
+ def __lt__ (self , x : Attribute [ _T ] ) -> bool : ...
61
+ def __le__ (self , x : Attribute [ _T ] ) -> bool : ...
62
+ def __gt__ (self , x : Attribute [ _T ] ) -> bool : ...
63
+ def __ge__ ( self , x : Attribute [ _T ]) -> bool : ...
46
64
47
65
# NOTE: We had several choices for the annotation to use for type arg:
48
66
# 1) Type[_T]
49
- # - Pros: works in PyCharm without plugin support
50
- # - Cons: produces less informative error in the case of conflicting TypeVars
51
- # e.g. `attr.ib(default='bad', type=int)`
67
+ # - Pros: Handles simple cases correctly
68
+ # - Cons: Might produce less informative errors in the case of conflicting TypeVars
69
+ # e.g. `attr.ib(default='bad', type=int)`
52
70
# 2) Callable[..., _T]
53
- # - Pros: more informative errors than #1
54
- # - Cons: validator tests results in confusing error.
55
- # e.g. `attr.ib(type=int, validator=validate_str)`
71
+ # - Pros: Better error messages than #1 for conflicting TypeVars
72
+ # - Cons: Terrible error messages for validator checks.
73
+ # e.g. attr.ib(type=int, validator=validate_str)
74
+ # -> error: Cannot infer function type argument
56
75
# 3) type (and do all of the work in the mypy plugin)
57
- # - Pros: in mypy, the behavior of type argument is exactly the same as with
58
- # annotations.
59
- # - Cons: completely disables type inspections in PyCharm when using the
60
- # type arg.
61
- # We chose option #1 until either PyCharm adds support for attrs, or python 2
62
- # reaches EOL.
63
-
64
- # NOTE: If you update these, update `ib` and `attr` below.
76
+ # - Pros: Simple here, and we could customize the plugin with our own errors.
77
+ # - Cons: Would need to write mypy plugin code to handle all the cases.
78
+ # We chose option #1.
65
79
66
80
# `attr` lies about its return type to make the following possible:
67
81
# attr() -> Any
@@ -72,129 +86,156 @@ class Attribute(Generic[_T]):
72
86
#
73
87
# This form catches explicit None or no default but with no other arguments returns Any.
74
88
@overload
75
- def attrib (default : None = ...,
76
- validator : None = ...,
77
- repr : bool = ...,
78
- cmp : bool = ...,
79
- hash : Optional [bool ] = ...,
80
- init : bool = ...,
81
- convert : None = ...,
82
- metadata : Optional [Mapping [Any , Any ]] = ...,
83
- type : None = ...,
84
- converter : None = ...,
85
- factory : None = ...,
86
- ) -> Any : ...
89
+ def attrib (
90
+ default : None = ...,
91
+ validator : None = ...,
92
+ repr : bool = ...,
93
+ cmp : bool = ...,
94
+ hash : Optional [bool ] = ...,
95
+ init : bool = ...,
96
+ convert : None = ...,
97
+ metadata : Optional [Mapping [Any , Any ]] = ...,
98
+ type : None = ...,
99
+ converter : None = ...,
100
+ factory : None = ...,
101
+ kw_only : bool = ...,
102
+ ) -> Any : ...
103
+
87
104
# This form catches an explicit None or no default and infers the type from the other arguments.
88
105
@overload
89
- def attrib (default : None = ...,
90
- validator : Optional [_ValidatorArgType [_T ]] = ...,
91
- repr : bool = ...,
92
- cmp : bool = ...,
93
- hash : Optional [bool ] = ...,
94
- init : bool = ...,
95
- convert : Optional [_ConverterType [_T ]] = ...,
96
- metadata : Optional [Mapping [Any , Any ]] = ...,
97
- type : Optional [Type [_T ]] = ...,
98
- converter : Optional [_ConverterType [_T ]] = ...,
99
- factory : Optional [Callable [[], _T ]] = ...,
100
- ) -> _T : ...
106
+ def attrib (
107
+ default : None = ...,
108
+ validator : Optional [_ValidatorArgType [_T ]] = ...,
109
+ repr : bool = ...,
110
+ cmp : bool = ...,
111
+ hash : Optional [bool ] = ...,
112
+ init : bool = ...,
113
+ convert : Optional [_ConverterType [_T ]] = ...,
114
+ metadata : Optional [Mapping [Any , Any ]] = ...,
115
+ type : Optional [Type [_T ]] = ...,
116
+ converter : Optional [_ConverterType [_T ]] = ...,
117
+ factory : Optional [Callable [[], _T ]] = ...,
118
+ kw_only : bool = ...,
119
+ ) -> _T : ...
120
+
101
121
# This form catches an explicit default argument.
102
122
@overload
103
- def attrib (default : _T ,
104
- validator : Optional [_ValidatorArgType [_T ]] = ...,
105
- repr : bool = ...,
106
- cmp : bool = ...,
107
- hash : Optional [bool ] = ...,
108
- init : bool = ...,
109
- convert : Optional [_ConverterType [_T ]] = ...,
110
- metadata : Optional [Mapping [Any , Any ]] = ...,
111
- type : Optional [Type [_T ]] = ...,
112
- converter : Optional [_ConverterType [_T ]] = ...,
113
- factory : Optional [Callable [[], _T ]] = ...,
114
- ) -> _T : ...
123
+ def attrib (
124
+ default : _T ,
125
+ validator : Optional [_ValidatorArgType [_T ]] = ...,
126
+ repr : bool = ...,
127
+ cmp : bool = ...,
128
+ hash : Optional [bool ] = ...,
129
+ init : bool = ...,
130
+ convert : Optional [_ConverterType [_T ]] = ...,
131
+ metadata : Optional [Mapping [Any , Any ]] = ...,
132
+ type : Optional [Type [_T ]] = ...,
133
+ converter : Optional [_ConverterType [_T ]] = ...,
134
+ factory : Optional [Callable [[], _T ]] = ...,
135
+ kw_only : bool = ...,
136
+ ) -> _T : ...
137
+
115
138
# This form covers type=non-Type: e.g. forward references (str), Any
116
139
@overload
117
- def attrib (default : Optional [_T ] = ...,
118
- validator : Optional [_ValidatorArgType [_T ]] = ...,
119
- repr : bool = ...,
120
- cmp : bool = ...,
121
- hash : Optional [bool ] = ...,
122
- init : bool = ...,
123
- convert : Optional [_ConverterType [_T ]] = ...,
124
- metadata : Optional [Mapping [Any , Any ]] = ...,
125
- type : object = ...,
126
- converter : Optional [_ConverterType [_T ]] = ...,
127
- factory : Optional [Callable [[], _T ]] = ...,
128
- ) -> Any : ...
129
-
130
-
131
- # NOTE: If you update these, update `s` and `attributes` below.
140
+ def attrib (
141
+ default : Optional [_T ] = ...,
142
+ validator : Optional [_ValidatorArgType [_T ]] = ...,
143
+ repr : bool = ...,
144
+ cmp : bool = ...,
145
+ hash : Optional [bool ] = ...,
146
+ init : bool = ...,
147
+ convert : Optional [_ConverterType [_T ]] = ...,
148
+ metadata : Optional [Mapping [Any , Any ]] = ...,
149
+ type : object = ...,
150
+ converter : Optional [_ConverterType [_T ]] = ...,
151
+ factory : Optional [Callable [[], _T ]] = ...,
152
+ kw_only : bool = ...,
153
+ ) -> Any : ...
132
154
@overload
133
- def attrs (maybe_cls : _C ,
134
- these : Optional [Dict [str , Any ]] = ...,
135
- repr_ns : Optional [str ] = ...,
136
- repr : bool = ...,
137
- cmp : bool = ...,
138
- hash : Optional [bool ] = ...,
139
- init : bool = ...,
140
- slots : bool = ...,
141
- frozen : bool = ...,
142
- str : bool = ...,
143
- auto_attribs : bool = ...) -> _C : ...
155
+ def attrs (
156
+ maybe_cls : _C ,
157
+ these : Optional [Dict [str , Any ]] = ...,
158
+ repr_ns : Optional [str ] = ...,
159
+ repr : bool = ...,
160
+ cmp : bool = ...,
161
+ hash : Optional [bool ] = ...,
162
+ init : bool = ...,
163
+ slots : bool = ...,
164
+ frozen : bool = ...,
165
+ weakref_slot : bool = ...,
166
+ str : bool = ...,
167
+ auto_attribs : bool = ...,
168
+ kw_only : bool = ...,
169
+ cache_hash : bool = ...,
170
+ ) -> _C : ...
144
171
@overload
145
- def attrs (maybe_cls : None = ...,
146
- these : Optional [Dict [str , Any ]] = ...,
147
- repr_ns : Optional [str ] = ...,
148
- repr : bool = ...,
149
- cmp : bool = ...,
150
- hash : Optional [bool ] = ...,
151
- init : bool = ...,
152
- slots : bool = ...,
153
- frozen : bool = ...,
154
- str : bool = ...,
155
- auto_attribs : bool = ...) -> Callable [[_C ], _C ]: ...
156
-
172
+ def attrs (
173
+ maybe_cls : None = ...,
174
+ these : Optional [Dict [str , Any ]] = ...,
175
+ repr_ns : Optional [str ] = ...,
176
+ repr : bool = ...,
177
+ cmp : bool = ...,
178
+ hash : Optional [bool ] = ...,
179
+ init : bool = ...,
180
+ slots : bool = ...,
181
+ frozen : bool = ...,
182
+ weakref_slot : bool = ...,
183
+ str : bool = ...,
184
+ auto_attribs : bool = ...,
185
+ kw_only : bool = ...,
186
+ cache_hash : bool = ...,
187
+ ) -> Callable [[_C ], _C ]: ...
157
188
158
189
# TODO: add support for returning NamedTuple from the mypy plugin
159
- class _Fields (Tuple [Attribute , ...]):
160
- def __getattr__ (self , name : str ) -> Attribute : ...
190
+ class _Fields (Tuple [Attribute [ Any ] , ...]):
191
+ def __getattr__ (self , name : str ) -> Attribute [ Any ] : ...
161
192
162
193
def fields (cls : type ) -> _Fields : ...
163
- def fields_dict (cls : type ) -> Dict [str , Attribute ]: ...
194
+ def fields_dict (cls : type ) -> Dict [str , Attribute [ Any ] ]: ...
164
195
def validate (inst : Any ) -> None : ...
165
196
166
197
# TODO: add support for returning a proper attrs class from the mypy plugin
167
198
# we use Any instead of _CountingAttr so that e.g. `make_class('Foo', [attr.ib()])` is valid
168
- def make_class (name : str ,
169
- attrs : Union [List [str ], Tuple [str , ...], Dict [str , Any ]],
170
- bases : Tuple [type , ...] = ...,
171
- repr_ns : Optional [str ] = ...,
172
- repr : bool = ...,
173
- cmp : bool = ...,
174
- hash : Optional [bool ] = ...,
175
- init : bool = ...,
176
- slots : bool = ...,
177
- frozen : bool = ...,
178
- str : bool = ...,
179
- auto_attribs : bool = ...) -> type : ...
199
+ def make_class (
200
+ name : str ,
201
+ attrs : Union [List [str ], Tuple [str , ...], Dict [str , Any ]],
202
+ bases : Tuple [type , ...] = ...,
203
+ repr_ns : Optional [str ] = ...,
204
+ repr : bool = ...,
205
+ cmp : bool = ...,
206
+ hash : Optional [bool ] = ...,
207
+ init : bool = ...,
208
+ slots : bool = ...,
209
+ frozen : bool = ...,
210
+ weakref_slot : bool = ...,
211
+ str : bool = ...,
212
+ auto_attribs : bool = ...,
213
+ kw_only : bool = ...,
214
+ cache_hash : bool = ...,
215
+ ) -> type : ...
180
216
181
217
# _funcs --
182
218
183
219
# TODO: add support for returning TypedDict from the mypy plugin
184
220
# FIXME: asdict/astuple do not honor their factory args. waiting on one of these:
185
221
# https://github.com/python/mypy/issues/4236
186
222
# https://github.com/python/typing/issues/253
187
- def asdict (inst : Any ,
188
- recurse : bool = ...,
189
- filter : Optional [_FilterType ] = ...,
190
- dict_factory : Type [Mapping [Any , Any ]] = ...,
191
- retain_collection_types : bool = ...) -> Dict [str , Any ]: ...
223
+ def asdict (
224
+ inst : Any ,
225
+ recurse : bool = ...,
226
+ filter : Optional [_FilterType [Any ]] = ...,
227
+ dict_factory : Type [Mapping [Any , Any ]] = ...,
228
+ retain_collection_types : bool = ...,
229
+ ) -> Dict [str , Any ]: ...
230
+
192
231
# TODO: add support for returning NamedTuple from the mypy plugin
193
- def astuple (inst : Any ,
194
- recurse : bool = ...,
195
- filter : Optional [_FilterType ] = ...,
196
- tuple_factory : Type [Sequence ] = ...,
197
- retain_collection_types : bool = ...) -> Tuple [Any , ...]: ...
232
+ def astuple (
233
+ inst : Any ,
234
+ recurse : bool = ...,
235
+ filter : Optional [_FilterType [Any ]] = ...,
236
+ tuple_factory : Type [Sequence [Any ]] = ...,
237
+ retain_collection_types : bool = ...,
238
+ ) -> Tuple [Any , ...]: ...
198
239
def has (cls : type ) -> bool : ...
199
240
def assoc (inst : _T , ** changes : Any ) -> _T : ...
200
241
def evolve (inst : _T , ** changes : Any ) -> _T : ...
@@ -204,7 +245,6 @@ def evolve(inst: _T, **changes: Any) -> _T: ...
204
245
def set_run_validators (run : bool ) -> None : ...
205
246
def get_run_validators () -> bool : ...
206
247
207
-
208
248
# aliases --
209
249
210
250
s = attributes = attrs
0 commit comments