Debian Codegraph @

jedi/third_party/typeshed/stdlib/dataclasses.pyi

raw symbols SCIP
1 import enum
2 import sys
3 import types
4 from _typeshed import DataclassInstance
5 from builtins import type as Type # alias to avoid name clashes with fields named "type"
6 from collections.abc import Callable, Iterable, Mapping
7 from types import GenericAlias
8 from typing import Any, Final, Generic, Literal, Protocol, TypeVar, overload, type_check_only
9 from typing_extensions import Never, TypeIs
10
11 _T = TypeVar("_T")
12 _T_co = TypeVar("_T_co", covariant=True)
13
14 __all__ = [
15 "dataclass",
16 "field",
17 "Field",
18 "FrozenInstanceError",
19 "InitVar",
20 "MISSING",
21 "fields",
22 "asdict",
23 "astuple",
24 "make_dataclass",
25 "replace",
26 "is_dataclass",
27 ]
28
29 if sys.version_info >= (3, 10):
30 __all__ += ["KW_ONLY"]
31
32 _DataclassT = TypeVar("_DataclassT", bound=DataclassInstance)
33
34 @type_check_only
35 class _DataclassFactory(Protocol):
36 def __call__(
37 self,
38 cls: type[_T],
39 /,
40 *,
41 init: bool = True,
42 repr: bool = True,
43 eq: bool = True,
44 order: bool = False,
45 unsafe_hash: bool = False,
46 frozen: bool = False,
47 match_args: bool = True,
48 kw_only: bool = False,
49 slots: bool = False,
50 weakref_slot: bool = False,
51 ) -> type[_T]: ...
52
53 # define _MISSING_TYPE as an enum within the type stubs,
54 # even though that is not really its type at runtime
55 # this allows us to use Literal[_MISSING_TYPE.MISSING]
56 # for background, see:
57 # https://github.com/python/typeshed/pull/5900#issuecomment-895513797
58 class _MISSING_TYPE(enum.Enum):
59 MISSING = enum.auto()
60
61 MISSING: Final = _MISSING_TYPE.MISSING
62
63 if sys.version_info >= (3, 10):
64 class KW_ONLY: ...
65
66 @overload
67 def asdict(obj: DataclassInstance) -> dict[str, Any]: ...
68 @overload
69 def asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, Any]]], _T]) -> _T: ...
70 @overload
71 def astuple(obj: DataclassInstance) -> tuple[Any, ...]: ...
72 @overload
73 def astuple(obj: DataclassInstance, *, tuple_factory: Callable[[list[Any]], _T]) -> _T: ...
74
75 if sys.version_info >= (3, 11):
76 @overload
77 def dataclass(
78 cls: type[_T],
79 /,
80 *,
81 init: bool = True,
82 repr: bool = True,
83 eq: bool = True,
84 order: bool = False,
85 unsafe_hash: bool = False,
86 frozen: bool = False,
87 match_args: bool = True,
88 kw_only: bool = False,
89 slots: bool = False,
90 weakref_slot: bool = False,
91 ) -> type[_T]: ...
92 @overload
93 def dataclass(
94 cls: None = None,
95 /,
96 *,
97 init: bool = True,
98 repr: bool = True,
99 eq: bool = True,
100 order: bool = False,
101 unsafe_hash: bool = False,
102 frozen: bool = False,
103 match_args: bool = True,
104 kw_only: bool = False,
105 slots: bool = False,
106 weakref_slot: bool = False,
107 ) -> Callable[[type[_T]], type[_T]]: ...
108
109 elif sys.version_info >= (3, 10):
110 @overload
111 def dataclass(
112 cls: type[_T],
113 /,
114 *,
115 init: bool = True,
116 repr: bool = True,
117 eq: bool = True,
118 order: bool = False,
119 unsafe_hash: bool = False,
120 frozen: bool = False,
121 match_args: bool = True,
122 kw_only: bool = False,
123 slots: bool = False,
124 ) -> type[_T]: ...
125 @overload
126 def dataclass(
127 cls: None = None,
128 /,
129 *,
130 init: bool = True,
131 repr: bool = True,
132 eq: bool = True,
133 order: bool = False,
134 unsafe_hash: bool = False,
135 frozen: bool = False,
136 match_args: bool = True,
137 kw_only: bool = False,
138 slots: bool = False,
139 ) -> Callable[[type[_T]], type[_T]]: ...
140
141 else:
142 @overload
143 def dataclass(
144 cls: type[_T],
145 /,
146 *,
147 init: bool = True,
148 repr: bool = True,
149 eq: bool = True,
150 order: bool = False,
151 unsafe_hash: bool = False,
152 frozen: bool = False,
153 ) -> type[_T]: ...
154 @overload
155 def dataclass(
156 cls: None = None,
157 /,
158 *,
159 init: bool = True,
160 repr: bool = True,
161 eq: bool = True,
162 order: bool = False,
163 unsafe_hash: bool = False,
164 frozen: bool = False,
165 ) -> Callable[[type[_T]], type[_T]]: ...
166
167 # See https://github.com/python/mypy/issues/10750
168 @type_check_only
169 class _DefaultFactory(Protocol[_T_co]):
170 def __call__(self) -> _T_co: ...
171
172 class Field(Generic[_T]):
173 if sys.version_info >= (3, 14):
174 __slots__ = (
175 "name",
176 "type",
177 "default",
178 "default_factory",
179 "repr",
180 "hash",
181 "init",
182 "compare",
183 "metadata",
184 "kw_only",
185 "doc",
186 "_field_type",
187 )
188 elif sys.version_info >= (3, 10):
189 __slots__ = (
190 "name",
191 "type",
192 "default",
193 "default_factory",
194 "repr",
195 "hash",
196 "init",
197 "compare",
198 "metadata",
199 "kw_only",
200 "_field_type",
201 )
202 else:
203 __slots__ = ("name", "type", "default", "default_factory", "repr", "hash", "init", "compare", "metadata", "_field_type")
204 name: str
205 type: Type[_T] | str | Any
206 default: _T | Literal[_MISSING_TYPE.MISSING]
207 default_factory: _DefaultFactory[_T] | Literal[_MISSING_TYPE.MISSING]
208 repr: bool
209 hash: bool | None
210 init: bool
211 compare: bool
212 metadata: types.MappingProxyType[Any, Any]
213
214 if sys.version_info >= (3, 14):
215 doc: str | None
216
217 if sys.version_info >= (3, 10):
218 kw_only: bool | Literal[_MISSING_TYPE.MISSING]
219
220 if sys.version_info >= (3, 14):
221 def __init__(
222 self,
223 default: _T,
224 default_factory: Callable[[], _T],
225 init: bool,
226 repr: bool,
227 hash: bool | None,
228 compare: bool,
229 metadata: Mapping[Any, Any],
230 kw_only: bool,
231 doc: str | None,
232 ) -> None: ...
233 elif sys.version_info >= (3, 10):
234 def __init__(
235 self,
236 default: _T,
237 default_factory: Callable[[], _T],
238 init: bool,
239 repr: bool,
240 hash: bool | None,
241 compare: bool,
242 metadata: Mapping[Any, Any],
243 kw_only: bool,
244 ) -> None: ...
245 else:
246 def __init__(
247 self,
248 default: _T,
249 default_factory: Callable[[], _T],
250 init: bool,
251 repr: bool,
252 hash: bool | None,
253 compare: bool,
254 metadata: Mapping[Any, Any],
255 ) -> None: ...
256
257 def __set_name__(self, owner: Type[Any], name: str) -> None: ...
258 def __class_getitem__(cls, item: Any, /) -> GenericAlias: ...
259
260 # NOTE: Actual return type is 'Field[_T]', but we want to help type checkers
261 # to understand the magic that happens at runtime.
262 if sys.version_info >= (3, 14):
263 @overload # `default` and `default_factory` are optional and mutually exclusive.
264 def field(
265 *,
266 default: _T,
267 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
268 init: bool = True,
269 repr: bool = True,
270 hash: bool | None = None,
271 compare: bool = True,
272 metadata: Mapping[Any, Any] | None = None,
273 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
274 doc: str | None = None,
275 ) -> _T: ...
276 @overload
277 def field(
278 *,
279 default: Literal[_MISSING_TYPE.MISSING] = ...,
280 default_factory: Callable[[], _T],
281 init: bool = True,
282 repr: bool = True,
283 hash: bool | None = None,
284 compare: bool = True,
285 metadata: Mapping[Any, Any] | None = None,
286 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
287 doc: str | None = None,
288 ) -> _T: ...
289 @overload
290 def field(
291 *,
292 default: Literal[_MISSING_TYPE.MISSING] = ...,
293 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
294 init: bool = True,
295 repr: bool = True,
296 hash: bool | None = None,
297 compare: bool = True,
298 metadata: Mapping[Any, Any] | None = None,
299 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
300 doc: str | None = None,
301 ) -> Any: ...
302
303 elif sys.version_info >= (3, 10):
304 @overload # `default` and `default_factory` are optional and mutually exclusive.
305 def field(
306 *,
307 default: _T,
308 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
309 init: bool = True,
310 repr: bool = True,
311 hash: bool | None = None,
312 compare: bool = True,
313 metadata: Mapping[Any, Any] | None = None,
314 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
315 ) -> _T: ...
316 @overload
317 def field(
318 *,
319 default: Literal[_MISSING_TYPE.MISSING] = ...,
320 default_factory: Callable[[], _T],
321 init: bool = True,
322 repr: bool = True,
323 hash: bool | None = None,
324 compare: bool = True,
325 metadata: Mapping[Any, Any] | None = None,
326 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
327 ) -> _T: ...
328 @overload
329 def field(
330 *,
331 default: Literal[_MISSING_TYPE.MISSING] = ...,
332 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
333 init: bool = True,
334 repr: bool = True,
335 hash: bool | None = None,
336 compare: bool = True,
337 metadata: Mapping[Any, Any] | None = None,
338 kw_only: bool | Literal[_MISSING_TYPE.MISSING] = ...,
339 ) -> Any: ...
340
341 else:
342 @overload # `default` and `default_factory` are optional and mutually exclusive.
343 def field(
344 *,
345 default: _T,
346 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
347 init: bool = True,
348 repr: bool = True,
349 hash: bool | None = None,
350 compare: bool = True,
351 metadata: Mapping[Any, Any] | None = None,
352 ) -> _T: ...
353 @overload
354 def field(
355 *,
356 default: Literal[_MISSING_TYPE.MISSING] = ...,
357 default_factory: Callable[[], _T],
358 init: bool = True,
359 repr: bool = True,
360 hash: bool | None = None,
361 compare: bool = True,
362 metadata: Mapping[Any, Any] | None = None,
363 ) -> _T: ...
364 @overload
365 def field(
366 *,
367 default: Literal[_MISSING_TYPE.MISSING] = ...,
368 default_factory: Literal[_MISSING_TYPE.MISSING] = ...,
369 init: bool = True,
370 repr: bool = True,
371 hash: bool | None = None,
372 compare: bool = True,
373 metadata: Mapping[Any, Any] | None = None,
374 ) -> Any: ...
375
376 def fields(class_or_instance: DataclassInstance | type[DataclassInstance]) -> tuple[Field[Any], ...]: ...
377
378 # HACK: `obj: Never` typing matches if object argument is using `Any` type.
379 @overload
380 def is_dataclass(obj: Never) -> TypeIs[DataclassInstance | type[DataclassInstance]]: ... # type: ignore[narrowed-type-not-subtype] # pyright: ignore[reportGeneralTypeIssues]
381 @overload
382 def is_dataclass(obj: type) -> TypeIs[type[DataclassInstance]]: ...
383 @overload
384 def is_dataclass(obj: object) -> TypeIs[DataclassInstance | type[DataclassInstance]]: ...
385
386 class FrozenInstanceError(AttributeError): ...
387
388 class InitVar(Generic[_T]):
389 __slots__ = ("type",)
390 type: Type[_T]
391 def __init__(self, type: Type[_T]) -> None: ...
392 @overload
393 def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... # pyright: ignore[reportInvalidTypeForm]
394 @overload
395 def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... # pyright: ignore[reportInvalidTypeForm]
396
397 if sys.version_info >= (3, 14):
398 def make_dataclass(
399 cls_name: str,
400 fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
401 *,
402 bases: tuple[type, ...] = (),
403 namespace: dict[str, Any] | None = None,
404 init: bool = True,
405 repr: bool = True,
406 eq: bool = True,
407 order: bool = False,
408 unsafe_hash: bool = False,
409 frozen: bool = False,
410 match_args: bool = True,
411 kw_only: bool = False,
412 slots: bool = False,
413 weakref_slot: bool = False,
414 module: str | None = None,
415 decorator: _DataclassFactory = ...,
416 ) -> type: ...
417
418 elif sys.version_info >= (3, 12):
419 def make_dataclass(
420 cls_name: str,
421 fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
422 *,
423 bases: tuple[type, ...] = (),
424 namespace: dict[str, Any] | None = None,
425 init: bool = True,
426 repr: bool = True,
427 eq: bool = True,
428 order: bool = False,
429 unsafe_hash: bool = False,
430 frozen: bool = False,
431 match_args: bool = True,
432 kw_only: bool = False,
433 slots: bool = False,
434 weakref_slot: bool = False,
435 module: str | None = None,
436 ) -> type: ...
437
438 elif sys.version_info >= (3, 11):
439 def make_dataclass(
440 cls_name: str,
441 fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
442 *,
443 bases: tuple[type, ...] = (),
444 namespace: dict[str, Any] | None = None,
445 init: bool = True,
446 repr: bool = True,
447 eq: bool = True,
448 order: bool = False,
449 unsafe_hash: bool = False,
450 frozen: bool = False,
451 match_args: bool = True,
452 kw_only: bool = False,
453 slots: bool = False,
454 weakref_slot: bool = False,
455 ) -> type: ...
456
457 elif sys.version_info >= (3, 10):
458 def make_dataclass(
459 cls_name: str,
460 fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
461 *,
462 bases: tuple[type, ...] = (),
463 namespace: dict[str, Any] | None = None,
464 init: bool = True,
465 repr: bool = True,
466 eq: bool = True,
467 order: bool = False,
468 unsafe_hash: bool = False,
469 frozen: bool = False,
470 match_args: bool = True,
471 kw_only: bool = False,
472 slots: bool = False,
473 ) -> type: ...
474
475 else:
476 def make_dataclass(
477 cls_name: str,
478 fields: Iterable[str | tuple[str, Any] | tuple[str, Any, Any]],
479 *,
480 bases: tuple[type, ...] = (),
481 namespace: dict[str, Any] | None = None,
482 init: bool = True,
483 repr: bool = True,
484 eq: bool = True,
485 order: bool = False,
486 unsafe_hash: bool = False,
487 frozen: bool = False,
488 ) -> type: ...
489
490 def replace(obj: _DataclassT, /, **changes: Any) -> _DataclassT: ...