comparison env/lib/python3.9/site-packages/attr/__init__.pyi @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
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
17 # `import X as X` is required to make these public
18 from . import exceptions as exceptions
19 from . import filters as filters
20 from . import converters as converters
21 from . import setters as setters
22 from . import validators as validators
23
24 from ._version_info import VersionInfo
25
26 __version__: str
27 __version_info__: VersionInfo
28 __title__: str
29 __description__: str
30 __url__: str
31 __uri__: str
32 __author__: str
33 __email__: str
34 __license__: str
35 __copyright__: str
36
37 _T = TypeVar("_T")
38 _C = TypeVar("_C", bound=type)
39
40 _ValidatorType = Callable[[Any, Attribute[_T], _T], Any]
41 _ConverterType = Callable[[Any], Any]
42 _FilterType = Callable[[Attribute[_T], _T], bool]
43 _ReprType = Callable[[Any], str]
44 _ReprArgType = Union[bool, _ReprType]
45 _OnSetAttrType = Callable[[Any, Attribute[Any], Any], Any]
46 _OnSetAttrArgType = Union[
47 _OnSetAttrType, List[_OnSetAttrType], setters._NoOpType
48 ]
49 _FieldTransformer = Callable[[type, List[Attribute]], List[Attribute]]
50 # FIXME: in reality, if multiple validators are passed they must be in a list
51 # or tuple, but those are invariant and so would prevent subtypes of
52 # _ValidatorType from working when passed in a list or tuple.
53 _ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
54
55 # _make --
56
57 NOTHING: object
58
59 # NOTE: Factory lies about its return type to make this possible:
60 # `x: List[int] # = Factory(list)`
61 # Work around mypy issue #4554 in the common case by using an overload.
62 @overload
63 def Factory(factory: Callable[[], _T]) -> _T: ...
64 @overload
65 def Factory(
66 factory: Union[Callable[[Any], _T], Callable[[], _T]],
67 takes_self: bool = ...,
68 ) -> _T: ...
69
70 class Attribute(Generic[_T]):
71 name: str
72 default: Optional[_T]
73 validator: Optional[_ValidatorType[_T]]
74 repr: _ReprArgType
75 cmp: bool
76 eq: bool
77 order: bool
78 hash: Optional[bool]
79 init: bool
80 converter: Optional[_ConverterType]
81 metadata: Dict[Any, Any]
82 type: Optional[Type[_T]]
83 kw_only: bool
84 on_setattr: _OnSetAttrType
85
86 # NOTE: We had several choices for the annotation to use for type arg:
87 # 1) Type[_T]
88 # - Pros: Handles simple cases correctly
89 # - Cons: Might produce less informative errors in the case of conflicting
90 # TypeVars e.g. `attr.ib(default='bad', type=int)`
91 # 2) Callable[..., _T]
92 # - Pros: Better error messages than #1 for conflicting TypeVars
93 # - Cons: Terrible error messages for validator checks.
94 # e.g. attr.ib(type=int, validator=validate_str)
95 # -> error: Cannot infer function type argument
96 # 3) type (and do all of the work in the mypy plugin)
97 # - Pros: Simple here, and we could customize the plugin with our own errors.
98 # - Cons: Would need to write mypy plugin code to handle all the cases.
99 # We chose option #1.
100
101 # `attr` lies about its return type to make the following possible:
102 # attr() -> Any
103 # attr(8) -> int
104 # attr(validator=<some callable>) -> Whatever the callable expects.
105 # This makes this type of assignments possible:
106 # x: int = attr(8)
107 #
108 # This form catches explicit None or no default but with no other arguments
109 # returns Any.
110 @overload
111 def attrib(
112 default: None = ...,
113 validator: None = ...,
114 repr: _ReprArgType = ...,
115 cmp: Optional[bool] = ...,
116 hash: Optional[bool] = ...,
117 init: bool = ...,
118 metadata: Optional[Mapping[Any, Any]] = ...,
119 type: None = ...,
120 converter: None = ...,
121 factory: None = ...,
122 kw_only: bool = ...,
123 eq: Optional[bool] = ...,
124 order: Optional[bool] = ...,
125 on_setattr: Optional[_OnSetAttrArgType] = ...,
126 ) -> Any: ...
127
128 # This form catches an explicit None or no default and infers the type from the
129 # other arguments.
130 @overload
131 def attrib(
132 default: None = ...,
133 validator: Optional[_ValidatorArgType[_T]] = ...,
134 repr: _ReprArgType = ...,
135 cmp: Optional[bool] = ...,
136 hash: Optional[bool] = ...,
137 init: bool = ...,
138 metadata: Optional[Mapping[Any, Any]] = ...,
139 type: Optional[Type[_T]] = ...,
140 converter: Optional[_ConverterType] = ...,
141 factory: Optional[Callable[[], _T]] = ...,
142 kw_only: bool = ...,
143 eq: Optional[bool] = ...,
144 order: Optional[bool] = ...,
145 on_setattr: Optional[_OnSetAttrArgType] = ...,
146 ) -> _T: ...
147
148 # This form catches an explicit default argument.
149 @overload
150 def attrib(
151 default: _T,
152 validator: Optional[_ValidatorArgType[_T]] = ...,
153 repr: _ReprArgType = ...,
154 cmp: Optional[bool] = ...,
155 hash: Optional[bool] = ...,
156 init: bool = ...,
157 metadata: Optional[Mapping[Any, Any]] = ...,
158 type: Optional[Type[_T]] = ...,
159 converter: Optional[_ConverterType] = ...,
160 factory: Optional[Callable[[], _T]] = ...,
161 kw_only: bool = ...,
162 eq: Optional[bool] = ...,
163 order: Optional[bool] = ...,
164 on_setattr: Optional[_OnSetAttrArgType] = ...,
165 ) -> _T: ...
166
167 # This form covers type=non-Type: e.g. forward references (str), Any
168 @overload
169 def attrib(
170 default: Optional[_T] = ...,
171 validator: Optional[_ValidatorArgType[_T]] = ...,
172 repr: _ReprArgType = ...,
173 cmp: Optional[bool] = ...,
174 hash: Optional[bool] = ...,
175 init: bool = ...,
176 metadata: Optional[Mapping[Any, Any]] = ...,
177 type: object = ...,
178 converter: Optional[_ConverterType] = ...,
179 factory: Optional[Callable[[], _T]] = ...,
180 kw_only: bool = ...,
181 eq: Optional[bool] = ...,
182 order: Optional[bool] = ...,
183 on_setattr: Optional[_OnSetAttrArgType] = ...,
184 ) -> Any: ...
185 @overload
186 def field(
187 *,
188 default: None = ...,
189 validator: None = ...,
190 repr: _ReprArgType = ...,
191 hash: Optional[bool] = ...,
192 init: bool = ...,
193 metadata: Optional[Mapping[Any, Any]] = ...,
194 converter: None = ...,
195 factory: None = ...,
196 kw_only: bool = ...,
197 eq: Optional[bool] = ...,
198 order: Optional[bool] = ...,
199 on_setattr: Optional[_OnSetAttrArgType] = ...,
200 ) -> Any: ...
201
202 # This form catches an explicit None or no default and infers the type from the
203 # other arguments.
204 @overload
205 def field(
206 *,
207 default: None = ...,
208 validator: Optional[_ValidatorArgType[_T]] = ...,
209 repr: _ReprArgType = ...,
210 hash: Optional[bool] = ...,
211 init: bool = ...,
212 metadata: Optional[Mapping[Any, Any]] = ...,
213 converter: Optional[_ConverterType] = ...,
214 factory: Optional[Callable[[], _T]] = ...,
215 kw_only: bool = ...,
216 eq: Optional[bool] = ...,
217 order: Optional[bool] = ...,
218 on_setattr: Optional[_OnSetAttrArgType] = ...,
219 ) -> _T: ...
220
221 # This form catches an explicit default argument.
222 @overload
223 def field(
224 *,
225 default: _T,
226 validator: Optional[_ValidatorArgType[_T]] = ...,
227 repr: _ReprArgType = ...,
228 hash: Optional[bool] = ...,
229 init: bool = ...,
230 metadata: Optional[Mapping[Any, Any]] = ...,
231 converter: Optional[_ConverterType] = ...,
232 factory: Optional[Callable[[], _T]] = ...,
233 kw_only: bool = ...,
234 eq: Optional[bool] = ...,
235 order: Optional[bool] = ...,
236 on_setattr: Optional[_OnSetAttrArgType] = ...,
237 ) -> _T: ...
238
239 # This form covers type=non-Type: e.g. forward references (str), Any
240 @overload
241 def field(
242 *,
243 default: Optional[_T] = ...,
244 validator: Optional[_ValidatorArgType[_T]] = ...,
245 repr: _ReprArgType = ...,
246 hash: Optional[bool] = ...,
247 init: bool = ...,
248 metadata: Optional[Mapping[Any, Any]] = ...,
249 converter: Optional[_ConverterType] = ...,
250 factory: Optional[Callable[[], _T]] = ...,
251 kw_only: bool = ...,
252 eq: Optional[bool] = ...,
253 order: Optional[bool] = ...,
254 on_setattr: Optional[_OnSetAttrArgType] = ...,
255 ) -> Any: ...
256 @overload
257 def attrs(
258 maybe_cls: _C,
259 these: Optional[Dict[str, Any]] = ...,
260 repr_ns: Optional[str] = ...,
261 repr: bool = ...,
262 cmp: Optional[bool] = ...,
263 hash: Optional[bool] = ...,
264 init: bool = ...,
265 slots: bool = ...,
266 frozen: bool = ...,
267 weakref_slot: bool = ...,
268 str: bool = ...,
269 auto_attribs: bool = ...,
270 kw_only: bool = ...,
271 cache_hash: bool = ...,
272 auto_exc: bool = ...,
273 eq: Optional[bool] = ...,
274 order: Optional[bool] = ...,
275 auto_detect: bool = ...,
276 collect_by_mro: bool = ...,
277 getstate_setstate: Optional[bool] = ...,
278 on_setattr: Optional[_OnSetAttrArgType] = ...,
279 field_transformer: Optional[_FieldTransformer] = ...,
280 ) -> _C: ...
281 @overload
282 def attrs(
283 maybe_cls: None = ...,
284 these: Optional[Dict[str, Any]] = ...,
285 repr_ns: Optional[str] = ...,
286 repr: bool = ...,
287 cmp: Optional[bool] = ...,
288 hash: Optional[bool] = ...,
289 init: bool = ...,
290 slots: bool = ...,
291 frozen: bool = ...,
292 weakref_slot: bool = ...,
293 str: bool = ...,
294 auto_attribs: bool = ...,
295 kw_only: bool = ...,
296 cache_hash: bool = ...,
297 auto_exc: bool = ...,
298 eq: Optional[bool] = ...,
299 order: Optional[bool] = ...,
300 auto_detect: bool = ...,
301 collect_by_mro: bool = ...,
302 getstate_setstate: Optional[bool] = ...,
303 on_setattr: Optional[_OnSetAttrArgType] = ...,
304 field_transformer: Optional[_FieldTransformer] = ...,
305 ) -> Callable[[_C], _C]: ...
306 @overload
307 def define(
308 maybe_cls: _C,
309 *,
310 these: Optional[Dict[str, Any]] = ...,
311 repr: bool = ...,
312 hash: Optional[bool] = ...,
313 init: bool = ...,
314 slots: bool = ...,
315 frozen: bool = ...,
316 weakref_slot: bool = ...,
317 str: bool = ...,
318 auto_attribs: bool = ...,
319 kw_only: bool = ...,
320 cache_hash: bool = ...,
321 auto_exc: bool = ...,
322 eq: Optional[bool] = ...,
323 order: Optional[bool] = ...,
324 auto_detect: bool = ...,
325 getstate_setstate: Optional[bool] = ...,
326 on_setattr: Optional[_OnSetAttrArgType] = ...,
327 field_transformer: Optional[_FieldTransformer] = ...,
328 ) -> _C: ...
329 @overload
330 def define(
331 maybe_cls: None = ...,
332 *,
333 these: Optional[Dict[str, Any]] = ...,
334 repr: bool = ...,
335 hash: Optional[bool] = ...,
336 init: bool = ...,
337 slots: bool = ...,
338 frozen: bool = ...,
339 weakref_slot: bool = ...,
340 str: bool = ...,
341 auto_attribs: bool = ...,
342 kw_only: bool = ...,
343 cache_hash: bool = ...,
344 auto_exc: bool = ...,
345 eq: Optional[bool] = ...,
346 order: Optional[bool] = ...,
347 auto_detect: bool = ...,
348 getstate_setstate: Optional[bool] = ...,
349 on_setattr: Optional[_OnSetAttrArgType] = ...,
350 field_transformer: Optional[_FieldTransformer] = ...,
351 ) -> Callable[[_C], _C]: ...
352
353 mutable = define
354 frozen = define # they differ only in their defaults
355
356 # TODO: add support for returning NamedTuple from the mypy plugin
357 class _Fields(Tuple[Attribute[Any], ...]):
358 def __getattr__(self, name: str) -> Attribute[Any]: ...
359
360 def fields(cls: type) -> _Fields: ...
361 def fields_dict(cls: type) -> Dict[str, Attribute[Any]]: ...
362 def validate(inst: Any) -> None: ...
363 def resolve_types(
364 cls: _C,
365 globalns: Optional[Dict[str, Any]] = ...,
366 localns: Optional[Dict[str, Any]] = ...,
367 ) -> _C: ...
368
369 # TODO: add support for returning a proper attrs class from the mypy plugin
370 # we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
371 # [attr.ib()])` is valid
372 def make_class(
373 name: str,
374 attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
375 bases: Tuple[type, ...] = ...,
376 repr_ns: Optional[str] = ...,
377 repr: bool = ...,
378 cmp: Optional[bool] = ...,
379 hash: Optional[bool] = ...,
380 init: bool = ...,
381 slots: bool = ...,
382 frozen: bool = ...,
383 weakref_slot: bool = ...,
384 str: bool = ...,
385 auto_attribs: bool = ...,
386 kw_only: bool = ...,
387 cache_hash: bool = ...,
388 auto_exc: bool = ...,
389 eq: Optional[bool] = ...,
390 order: Optional[bool] = ...,
391 collect_by_mro: bool = ...,
392 on_setattr: Optional[_OnSetAttrArgType] = ...,
393 field_transformer: Optional[_FieldTransformer] = ...,
394 ) -> type: ...
395
396 # _funcs --
397
398 # TODO: add support for returning TypedDict from the mypy plugin
399 # FIXME: asdict/astuple do not honor their factory args. Waiting on one of
400 # these:
401 # https://github.com/python/mypy/issues/4236
402 # https://github.com/python/typing/issues/253
403 def asdict(
404 inst: Any,
405 recurse: bool = ...,
406 filter: Optional[_FilterType[Any]] = ...,
407 dict_factory: Type[Mapping[Any, Any]] = ...,
408 retain_collection_types: bool = ...,
409 value_serializer: Optional[Callable[[type, Attribute, Any], Any]] = ...,
410 ) -> Dict[str, Any]: ...
411
412 # TODO: add support for returning NamedTuple from the mypy plugin
413 def astuple(
414 inst: Any,
415 recurse: bool = ...,
416 filter: Optional[_FilterType[Any]] = ...,
417 tuple_factory: Type[Sequence[Any]] = ...,
418 retain_collection_types: bool = ...,
419 ) -> Tuple[Any, ...]: ...
420 def has(cls: type) -> bool: ...
421 def assoc(inst: _T, **changes: Any) -> _T: ...
422 def evolve(inst: _T, **changes: Any) -> _T: ...
423
424 # _config --
425
426 def set_run_validators(run: bool) -> None: ...
427 def get_run_validators() -> bool: ...
428
429 # aliases --
430
431 s = attributes = attrs
432 ib = attr = attrib
433 dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)