3 from typing import Any, Callable, Generic, Iterable, Mapping, Tuple, Type, TypeVar, overload
4 from typing_extensions import Protocol
6 if sys.version_info >= (3, 9):
7 from types import GenericAlias
10 _T_co = TypeVar("_T_co", covariant=True)
12 class _MISSING_TYPE: ...
14 MISSING: _MISSING_TYPE
16 if sys.version_info >= (3, 10):
20 def asdict(obj: Any) -> dict[str, Any]: ...
22 def asdict(obj: Any, *, dict_factory: Callable[[list[tuple[str, Any]]], _T]) -> _T: ...
24 def astuple(obj: Any) -> Tuple[Any, ...]: ...
26 def astuple(obj: Any, *, tuple_factory: Callable[[list[Any]], _T]) -> _T: ...
28 if sys.version_info >= (3, 10):
30 def dataclass(__cls: Type[_T]) -> Type[_T]: ...
32 def dataclass(__cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
40 unsafe_hash: bool = ...,
42 match_args: bool = ...,
45 ) -> Callable[[Type[_T]], Type[_T]]: ...
47 elif sys.version_info >= (3, 8):
48 # cls argument is now positional-only
50 def dataclass(__cls: Type[_T]) -> Type[_T]: ...
52 def dataclass(__cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
55 *, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
56 ) -> Callable[[Type[_T]], Type[_T]]: ...
60 def dataclass(_cls: Type[_T]) -> Type[_T]: ...
62 def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
65 *, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
66 ) -> Callable[[Type[_T]], Type[_T]]: ...
68 # See https://github.com/python/mypy/issues/10750
69 class _DefaultFactory(Protocol[_T_co]):
70 def __call__(self) -> _T_co: ...
72 class Field(Generic[_T]):
76 default_factory: _DefaultFactory[_T]
81 metadata: types.MappingProxyType[Any, Any]
82 if sys.version_info >= (3, 10):
87 default_factory: Callable[[], _T],
92 metadata: Mapping[Any, Any],
99 default_factory: Callable[[], _T],
104 metadata: Mapping[Any, Any],
106 if sys.version_info >= (3, 9):
107 def __class_getitem__(cls, item: Any) -> GenericAlias: ...
109 # NOTE: Actual return type is 'Field[_T]', but we want to help type checkers
110 # to understand the magic that happens at runtime.
111 if sys.version_info >= (3, 10):
112 @overload # `default` and `default_factory` are optional and mutually exclusive.
118 hash: bool | None = ...,
120 metadata: Mapping[Any, Any] | None = ...,
126 default_factory: Callable[[], _T],
129 hash: bool | None = ...,
131 metadata: Mapping[Any, Any] | None = ...,
139 hash: bool | None = ...,
141 metadata: Mapping[Any, Any] | None = ...,
146 @overload # `default` and `default_factory` are optional and mutually exclusive.
152 hash: bool | None = ...,
154 metadata: Mapping[Any, Any] | None = ...,
159 default_factory: Callable[[], _T],
162 hash: bool | None = ...,
164 metadata: Mapping[Any, Any] | None = ...,
171 hash: bool | None = ...,
173 metadata: Mapping[Any, Any] | None = ...,
176 def fields(class_or_instance: Any) -> Tuple[Field[Any], ...]: ...
177 def is_dataclass(obj: Any) -> bool: ...
179 class FrozenInstanceError(AttributeError): ...
181 class InitVar(Generic[_T]):
183 def __init__(self, type: Type[_T]) -> None: ...
184 if sys.version_info >= (3, 9):
186 def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ...
188 def __class_getitem__(cls, type: Any) -> InitVar[Any]: ...
190 if sys.version_info >= (3, 10):
193 fields: Iterable[str | tuple[str, type] | tuple[str, type, Field[Any]]],
195 bases: Tuple[type, ...] = ...,
196 namespace: dict[str, Any] | None = ...,
201 unsafe_hash: bool = ...,
203 match_args: bool = ...,
210 fields: Iterable[str | tuple[str, type] | tuple[str, type, Field[Any]]],
212 bases: Tuple[type, ...] = ...,
213 namespace: dict[str, Any] | None = ...,
218 unsafe_hash: bool = ...,
222 def replace(__obj: _T, **changes: Any) -> _T: ...