Debian Codegraph @

third_party/python/attrs/attr/__init__.pyi

raw
1 import enum
2 import sys
3
4 from typing import (
5 Any,
6 Callable,
7 Dict,
8 Generic,
9 List,
10 Mapping,
11 Optional,
12 Protocol,
13 Sequence,
14 Tuple,
15 Type,
16 TypeVar,
17 Union,
18 overload,
19 )
20
21 # `import X as X` is required to make these public
22 from . import converters as converters
23 from . import exceptions as exceptions
24 from . import filters as filters
25 from . import setters as setters
26 from . import validators as validators
27 from ._cmp import cmp_using as cmp_using
28 from ._typing_compat import AttrsInstance_
29 from ._version_info import VersionInfo
30
31 if sys.version_info >= (3, 10):
32 from typing import TypeGuard
33 else:
34 from typing_extensions import TypeGuard
35
36 __version__: str
37 __version_info__: VersionInfo
38 __title__: str
39 __description__: str
40 __url__: str
41 __uri__: str
42 __author__: str
43 __email__: str
44 __license__: str
45 __copyright__: str
46
47 _T = TypeVar("_T")
48 _C = TypeVar("_C", bound=type)
49
50 _EqOrderType = Union[bool, Callable[[Any], Any]]
51 _ValidatorType = Callable[[Any, "Attribute[_T]", _T], Any]
52 _ConverterType = Callable[[Any], Any]
53 _FilterType = Callable[["Attribute[_T]", _T], bool]
54 _ReprType = Callable[[Any], str]
55 _ReprArgType = Union[bool, _ReprType]
56 _OnSetAttrType = Callable[[Any, "Attribute[Any]", Any], Any]
57 _OnSetAttrArgType = Union[
58 _OnSetAttrType, List[_OnSetAttrType], setters._NoOpType
59 ]
60 _FieldTransformer = Callable[
61 [type, List["Attribute[Any]"]], List["Attribute[Any]"]
62 ]
63 # FIXME: in reality, if multiple validators are passed they must be in a list
64 # or tuple, but those are invariant and so would prevent subtypes of
65 # _ValidatorType from working when passed in a list or tuple.
66 _ValidatorArgType = Union[_ValidatorType[_T], Sequence[_ValidatorType[_T]]]
67
68 # We subclass this here to keep the protocol's qualified name clean.
69 class AttrsInstance(AttrsInstance_, Protocol):
70 pass
71
72 _A = TypeVar("_A", bound=AttrsInstance)
73 # _make --
74
75 class _Nothing(enum.Enum):
76 NOTHING = enum.auto()
77
78 NOTHING = _Nothing.NOTHING
79
80 # NOTE: Factory lies about its return type to make this possible:
81 # `x: List[int] # = Factory(list)`
82 # Work around mypy issue #4554 in the common case by using an overload.
83 if sys.version_info >= (3, 8):
84 from typing import Literal
85 @overload
86 def Factory(factory: Callable[[], _T]) -> _T: ...
87 @overload
88 def Factory(
89 factory: Callable[[Any], _T],
90 takes_self: Literal[True],
91 ) -> _T: ...
92 @overload
93 def Factory(
94 factory: Callable[[], _T],
95 takes_self: Literal[False],
96 ) -> _T: ...
97
98 else:
99 @overload
100 def Factory(factory: Callable[[], _T]) -> _T: ...
101 @overload
102 def Factory(
103 factory: Union[Callable[[Any], _T], Callable[[], _T]],
104 takes_self: bool = ...,
105 ) -> _T: ...
106
107 # Static type inference support via __dataclass_transform__ implemented as per:
108 # https://github.com/microsoft/pyright/blob/1.1.135/specs/dataclass_transforms.md
109 # This annotation must be applied to all overloads of "define" and "attrs"
110 #
111 # NOTE: This is a typing construct and does not exist at runtime. Extensions
112 # wrapping attrs decorators should declare a separate __dataclass_transform__
113 # signature in the extension module using the specification linked above to
114 # provide pyright support.
115 def __dataclass_transform__(
116 *,
117 eq_default: bool = True,
118 order_default: bool = False,
119 kw_only_default: bool = False,
120 frozen_default: bool = False,
121 field_descriptors: Tuple[Union[type, Callable[..., Any]], ...] = (()),
122 ) -> Callable[[_T], _T]: ...
123
124 class Attribute(Generic[_T]):
125 name: str
126 default: Optional[_T]
127 validator: Optional[_ValidatorType[_T]]
128 repr: _ReprArgType
129 cmp: _EqOrderType
130 eq: _EqOrderType
131 order: _EqOrderType
132 hash: Optional[bool]
133 init: bool
134 converter: Optional[_ConverterType]
135 metadata: Dict[Any, Any]
136 type: Optional[Type[_T]]
137 kw_only: bool
138 on_setattr: _OnSetAttrType
139 alias: Optional[str]
140
141 def evolve(self, **changes: Any) -> "Attribute[Any]": ...
142
143 # NOTE: We had several choices for the annotation to use for type arg:
144 # 1) Type[_T]
145 # - Pros: Handles simple cases correctly
146 # - Cons: Might produce less informative errors in the case of conflicting
147 # TypeVars e.g. `attr.ib(default='bad', type=int)`
148 # 2) Callable[..., _T]
149 # - Pros: Better error messages than #1 for conflicting TypeVars
150 # - Cons: Terrible error messages for validator checks.
151 # e.g. attr.ib(type=int, validator=validate_str)
152 # -> error: Cannot infer function type argument
153 # 3) type (and do all of the work in the mypy plugin)
154 # - Pros: Simple here, and we could customize the plugin with our own errors.
155 # - Cons: Would need to write mypy plugin code to handle all the cases.
156 # We chose option #1.
157
158 # `attr` lies about its return type to make the following possible:
159 # attr() -> Any
160 # attr(8) -> int
161 # attr(validator=<some callable>) -> Whatever the callable expects.
162 # This makes this type of assignments possible:
163 # x: int = attr(8)
164 #
165 # This form catches explicit None or no default but with no other arguments
166 # returns Any.
167 @overload
168 def attrib(
169 default: None = ...,
170 validator: None = ...,
171 repr: _ReprArgType = ...,
172 cmp: Optional[_EqOrderType] = ...,
173 hash: Optional[bool] = ...,
174 init: bool = ...,
175 metadata: Optional[Mapping[Any, Any]] = ...,
176 type: None = ...,
177 converter: None = ...,
178 factory: None = ...,
179 kw_only: bool = ...,
180 eq: Optional[_EqOrderType] = ...,
181 order: Optional[_EqOrderType] = ...,
182 on_setattr: Optional[_OnSetAttrArgType] = ...,
183 alias: Optional[str] = ...,
184 ) -> Any: ...
185
186 # This form catches an explicit None or no default and infers the type from the
187 # other arguments.
188 @overload
189 def attrib(
190 default: None = ...,
191 validator: Optional[_ValidatorArgType[_T]] = ...,
192 repr: _ReprArgType = ...,
193 cmp: Optional[_EqOrderType] = ...,
194 hash: Optional[bool] = ...,
195 init: bool = ...,
196 metadata: Optional[Mapping[Any, Any]] = ...,
197 type: Optional[Type[_T]] = ...,
198 converter: Optional[_ConverterType] = ...,
199 factory: Optional[Callable[[], _T]] = ...,
200 kw_only: bool = ...,
201 eq: Optional[_EqOrderType] = ...,
202 order: Optional[_EqOrderType] = ...,
203 on_setattr: Optional[_OnSetAttrArgType] = ...,
204 alias: Optional[str] = ...,
205 ) -> _T: ...
206
207 # This form catches an explicit default argument.
208 @overload
209 def attrib(
210 default: _T,
211 validator: Optional[_ValidatorArgType[_T]] = ...,
212 repr: _ReprArgType = ...,
213 cmp: Optional[_EqOrderType] = ...,
214 hash: Optional[bool] = ...,
215 init: bool = ...,
216 metadata: Optional[Mapping[Any, Any]] = ...,
217 type: Optional[Type[_T]] = ...,
218 converter: Optional[_ConverterType] = ...,
219 factory: Optional[Callable[[], _T]] = ...,
220 kw_only: bool = ...,
221 eq: Optional[_EqOrderType] = ...,
222 order: Optional[_EqOrderType] = ...,
223 on_setattr: Optional[_OnSetAttrArgType] = ...,
224 alias: Optional[str] = ...,
225 ) -> _T: ...
226
227 # This form covers type=non-Type: e.g. forward references (str), Any
228 @overload
229 def attrib(
230 default: Optional[_T] = ...,
231 validator: Optional[_ValidatorArgType[_T]] = ...,
232 repr: _ReprArgType = ...,
233 cmp: Optional[_EqOrderType] = ...,
234 hash: Optional[bool] = ...,
235 init: bool = ...,
236 metadata: Optional[Mapping[Any, Any]] = ...,
237 type: object = ...,
238 converter: Optional[_ConverterType] = ...,
239 factory: Optional[Callable[[], _T]] = ...,
240 kw_only: bool = ...,
241 eq: Optional[_EqOrderType] = ...,
242 order: Optional[_EqOrderType] = ...,
243 on_setattr: Optional[_OnSetAttrArgType] = ...,
244 alias: Optional[str] = ...,
245 ) -> Any: ...
246 @overload
247 def field(
248 *,
249 default: None = ...,
250 validator: None = ...,
251 repr: _ReprArgType = ...,
252 hash: Optional[bool] = ...,
253 init: bool = ...,
254 metadata: Optional[Mapping[Any, Any]] = ...,
255 converter: None = ...,
256 factory: None = ...,
257 kw_only: bool = ...,
258 eq: Optional[bool] = ...,
259 order: Optional[bool] = ...,
260 on_setattr: Optional[_OnSetAttrArgType] = ...,
261 alias: Optional[str] = ...,
262 type: Optional[type] = ...,
263 ) -> Any: ...
264
265 # This form catches an explicit None or no default and infers the type from the
266 # other arguments.
267 @overload
268 def field(
269 *,
270 default: None = ...,
271 validator: Optional[_ValidatorArgType[_T]] = ...,
272 repr: _ReprArgType = ...,
273 hash: Optional[bool] = ...,
274 init: bool = ...,
275 metadata: Optional[Mapping[Any, Any]] = ...,
276 converter: Optional[_ConverterType] = ...,
277 factory: Optional[Callable[[], _T]] = ...,
278 kw_only: bool = ...,
279 eq: Optional[_EqOrderType] = ...,
280 order: Optional[_EqOrderType] = ...,
281 on_setattr: Optional[_OnSetAttrArgType] = ...,
282 alias: Optional[str] = ...,
283 type: Optional[type] = ...,
284 ) -> _T: ...
285
286 # This form catches an explicit default argument.
287 @overload
288 def field(
289 *,
290 default: _T,
291 validator: Optional[_ValidatorArgType[_T]] = ...,
292 repr: _ReprArgType = ...,
293 hash: Optional[bool] = ...,
294 init: bool = ...,
295 metadata: Optional[Mapping[Any, Any]] = ...,
296 converter: Optional[_ConverterType] = ...,
297 factory: Optional[Callable[[], _T]] = ...,
298 kw_only: bool = ...,
299 eq: Optional[_EqOrderType] = ...,
300 order: Optional[_EqOrderType] = ...,
301 on_setattr: Optional[_OnSetAttrArgType] = ...,
302 alias: Optional[str] = ...,
303 type: Optional[type] = ...,
304 ) -> _T: ...
305
306 # This form covers type=non-Type: e.g. forward references (str), Any
307 @overload
308 def field(
309 *,
310 default: Optional[_T] = ...,
311 validator: Optional[_ValidatorArgType[_T]] = ...,
312 repr: _ReprArgType = ...,
313 hash: Optional[bool] = ...,
314 init: bool = ...,
315 metadata: Optional[Mapping[Any, Any]] = ...,
316 converter: Optional[_ConverterType] = ...,
317 factory: Optional[Callable[[], _T]] = ...,
318 kw_only: bool = ...,
319 eq: Optional[_EqOrderType] = ...,
320 order: Optional[_EqOrderType] = ...,
321 on_setattr: Optional[_OnSetAttrArgType] = ...,
322 alias: Optional[str] = ...,
323 type: Optional[type] = ...,
324 ) -> Any: ...
325 @overload
326 @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field))
327 def attrs(
328 maybe_cls: _C,
329 these: Optional[Dict[str, Any]] = ...,
330 repr_ns: Optional[str] = ...,
331 repr: bool = ...,
332 cmp: Optional[_EqOrderType] = ...,
333 hash: Optional[bool] = ...,
334 init: bool = ...,
335 slots: bool = ...,
336 frozen: bool = ...,
337 weakref_slot: bool = ...,
338 str: bool = ...,
339 auto_attribs: bool = ...,
340 kw_only: bool = ...,
341 cache_hash: bool = ...,
342 auto_exc: bool = ...,
343 eq: Optional[_EqOrderType] = ...,
344 order: Optional[_EqOrderType] = ...,
345 auto_detect: bool = ...,
346 collect_by_mro: bool = ...,
347 getstate_setstate: Optional[bool] = ...,
348 on_setattr: Optional[_OnSetAttrArgType] = ...,
349 field_transformer: Optional[_FieldTransformer] = ...,
350 match_args: bool = ...,
351 unsafe_hash: Optional[bool] = ...,
352 ) -> _C: ...
353 @overload
354 @__dataclass_transform__(order_default=True, field_descriptors=(attrib, field))
355 def attrs(
356 maybe_cls: None = ...,
357 these: Optional[Dict[str, Any]] = ...,
358 repr_ns: Optional[str] = ...,
359 repr: bool = ...,
360 cmp: Optional[_EqOrderType] = ...,
361 hash: Optional[bool] = ...,
362 init: bool = ...,
363 slots: bool = ...,
364 frozen: bool = ...,
365 weakref_slot: bool = ...,
366 str: bool = ...,
367 auto_attribs: bool = ...,
368 kw_only: bool = ...,
369 cache_hash: bool = ...,
370 auto_exc: bool = ...,
371 eq: Optional[_EqOrderType] = ...,
372 order: Optional[_EqOrderType] = ...,
373 auto_detect: bool = ...,
374 collect_by_mro: bool = ...,
375 getstate_setstate: Optional[bool] = ...,
376 on_setattr: Optional[_OnSetAttrArgType] = ...,
377 field_transformer: Optional[_FieldTransformer] = ...,
378 match_args: bool = ...,
379 unsafe_hash: Optional[bool] = ...,
380 ) -> Callable[[_C], _C]: ...
381 @overload
382 @__dataclass_transform__(field_descriptors=(attrib, field))
383 def define(
384 maybe_cls: _C,
385 *,
386 these: Optional[Dict[str, Any]] = ...,
387 repr: bool = ...,
388 unsafe_hash: Optional[bool] = ...,
389 hash: Optional[bool] = ...,
390 init: bool = ...,
391 slots: bool = ...,
392 frozen: bool = ...,
393 weakref_slot: bool = ...,
394 str: bool = ...,
395 auto_attribs: bool = ...,
396 kw_only: bool = ...,
397 cache_hash: bool = ...,
398 auto_exc: bool = ...,
399 eq: Optional[bool] = ...,
400 order: Optional[bool] = ...,
401 auto_detect: bool = ...,
402 getstate_setstate: Optional[bool] = ...,
403 on_setattr: Optional[_OnSetAttrArgType] = ...,
404 field_transformer: Optional[_FieldTransformer] = ...,
405 match_args: bool = ...,
406 ) -> _C: ...
407 @overload
408 @__dataclass_transform__(field_descriptors=(attrib, field))
409 def define(
410 maybe_cls: None = ...,
411 *,
412 these: Optional[Dict[str, Any]] = ...,
413 repr: bool = ...,
414 unsafe_hash: Optional[bool] = ...,
415 hash: Optional[bool] = ...,
416 init: bool = ...,
417 slots: bool = ...,
418 frozen: bool = ...,
419 weakref_slot: bool = ...,
420 str: bool = ...,
421 auto_attribs: bool = ...,
422 kw_only: bool = ...,
423 cache_hash: bool = ...,
424 auto_exc: bool = ...,
425 eq: Optional[bool] = ...,
426 order: Optional[bool] = ...,
427 auto_detect: bool = ...,
428 getstate_setstate: Optional[bool] = ...,
429 on_setattr: Optional[_OnSetAttrArgType] = ...,
430 field_transformer: Optional[_FieldTransformer] = ...,
431 match_args: bool = ...,
432 ) -> Callable[[_C], _C]: ...
433
434 mutable = define
435
436 @overload
437 @__dataclass_transform__(
438 frozen_default=True, field_descriptors=(attrib, field)
439 )
440 def frozen(
441 maybe_cls: _C,
442 *,
443 these: Optional[Dict[str, Any]] = ...,
444 repr: bool = ...,
445 unsafe_hash: Optional[bool] = ...,
446 hash: Optional[bool] = ...,
447 init: bool = ...,
448 slots: bool = ...,
449 frozen: bool = ...,
450 weakref_slot: bool = ...,
451 str: bool = ...,
452 auto_attribs: bool = ...,
453 kw_only: bool = ...,
454 cache_hash: bool = ...,
455 auto_exc: bool = ...,
456 eq: Optional[bool] = ...,
457 order: Optional[bool] = ...,
458 auto_detect: bool = ...,
459 getstate_setstate: Optional[bool] = ...,
460 on_setattr: Optional[_OnSetAttrArgType] = ...,
461 field_transformer: Optional[_FieldTransformer] = ...,
462 match_args: bool = ...,
463 ) -> _C: ...
464 @overload
465 @__dataclass_transform__(
466 frozen_default=True, field_descriptors=(attrib, field)
467 )
468 def frozen(
469 maybe_cls: None = ...,
470 *,
471 these: Optional[Dict[str, Any]] = ...,
472 repr: bool = ...,
473 unsafe_hash: Optional[bool] = ...,
474 hash: Optional[bool] = ...,
475 init: bool = ...,
476 slots: bool = ...,
477 frozen: bool = ...,
478 weakref_slot: bool = ...,
479 str: bool = ...,
480 auto_attribs: bool = ...,
481 kw_only: bool = ...,
482 cache_hash: bool = ...,
483 auto_exc: bool = ...,
484 eq: Optional[bool] = ...,
485 order: Optional[bool] = ...,
486 auto_detect: bool = ...,
487 getstate_setstate: Optional[bool] = ...,
488 on_setattr: Optional[_OnSetAttrArgType] = ...,
489 field_transformer: Optional[_FieldTransformer] = ...,
490 match_args: bool = ...,
491 ) -> Callable[[_C], _C]: ...
492 def fields(cls: Type[AttrsInstance]) -> Any: ...
493 def fields_dict(cls: Type[AttrsInstance]) -> Dict[str, Attribute[Any]]: ...
494 def validate(inst: AttrsInstance) -> None: ...
495 def resolve_types(
496 cls: _A,
497 globalns: Optional[Dict[str, Any]] = ...,
498 localns: Optional[Dict[str, Any]] = ...,
499 attribs: Optional[List[Attribute[Any]]] = ...,
500 include_extras: bool = ...,
501 ) -> _A: ...
502
503 # TODO: add support for returning a proper attrs class from the mypy plugin
504 # we use Any instead of _CountingAttr so that e.g. `make_class('Foo',
505 # [attr.ib()])` is valid
506 def make_class(
507 name: str,
508 attrs: Union[List[str], Tuple[str, ...], Dict[str, Any]],
509 bases: Tuple[type, ...] = ...,
510 repr_ns: Optional[str] = ...,
511 repr: bool = ...,
512 cmp: Optional[_EqOrderType] = ...,
513 hash: Optional[bool] = ...,
514 init: bool = ...,
515 slots: bool = ...,
516 frozen: bool = ...,
517 weakref_slot: bool = ...,
518 str: bool = ...,
519 auto_attribs: bool = ...,
520 kw_only: bool = ...,
521 cache_hash: bool = ...,
522 auto_exc: bool = ...,
523 eq: Optional[_EqOrderType] = ...,
524 order: Optional[_EqOrderType] = ...,
525 collect_by_mro: bool = ...,
526 on_setattr: Optional[_OnSetAttrArgType] = ...,
527 field_transformer: Optional[_FieldTransformer] = ...,
528 ) -> type: ...
529
530 # _funcs --
531
532 # TODO: add support for returning TypedDict from the mypy plugin
533 # FIXME: asdict/astuple do not honor their factory args. Waiting on one of
534 # these:
535 # https://github.com/python/mypy/issues/4236
536 # https://github.com/python/typing/issues/253
537 # XXX: remember to fix attrs.asdict/astuple too!
538 def asdict(
539 inst: AttrsInstance,
540 recurse: bool = ...,
541 filter: Optional[_FilterType[Any]] = ...,
542 dict_factory: Type[Mapping[Any, Any]] = ...,
543 retain_collection_types: bool = ...,
544 value_serializer: Optional[
545 Callable[[type, Attribute[Any], Any], Any]
546 ] = ...,
547 tuple_keys: Optional[bool] = ...,
548 ) -> Dict[str, Any]: ...
549
550 # TODO: add support for returning NamedTuple from the mypy plugin
551 def astuple(
552 inst: AttrsInstance,
553 recurse: bool = ...,
554 filter: Optional[_FilterType[Any]] = ...,
555 tuple_factory: Type[Sequence[Any]] = ...,
556 retain_collection_types: bool = ...,
557 ) -> Tuple[Any, ...]: ...
558 def has(cls: type) -> TypeGuard[Type[AttrsInstance]]: ...
559 def assoc(inst: _T, **changes: Any) -> _T: ...
560 def evolve(inst: _T, **changes: Any) -> _T: ...
561
562 # _config --
563
564 def set_run_validators(run: bool) -> None: ...
565 def get_run_validators() -> bool: ...
566
567 # aliases --
568
569 s = attributes = attrs
570 ib = attr = attrib
571 dataclass = attrs # Technically, partial(attrs, auto_attribs=True) ;)