Debian Codegraph @

test-data/unit/lib-stub/dataclasses.pyi

raw
1 from typing import Any, Callable, Generic, Literal, Mapping, Optional, TypeVar, overload, Type, \
2 Protocol, ClassVar
3 from typing_extensions import TypeGuard
4
5 # DataclassInstance is in _typeshed.pyi normally, but alas we can't do the same for lib-stub
6 # due to test-data/unit/lib-stub/builtins.pyi not having 'tuple'.
7 class DataclassInstance(Protocol):
8 __dataclass_fields__: ClassVar[dict[str, Field[Any]]]
9
10 _T = TypeVar('_T')
11 _DataclassT = TypeVar("_DataclassT", bound=DataclassInstance)
12
13 class InitVar(Generic[_T]):
14 ...
15
16 class KW_ONLY: ...
17
18 @overload
19 def dataclass(_cls: Type[_T]) -> Type[_T]: ...
20
21 @overload
22 def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ...,
23 unsafe_hash: bool = ..., frozen: bool = ..., match_args: bool = ...,
24 kw_only: bool = ..., slots: bool = ...) -> Callable[[Type[_T]], Type[_T]]: ...
25
26 @overload
27 def field(*, default: _T,
28 init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
29 metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...,) -> _T: ...
30
31 @overload
32 def field(*, default_factory: Callable[[], _T],
33 init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
34 metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...,) -> _T: ...
35
36 @overload
37 def field(*,
38 init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
39 metadata: Optional[Mapping[str, Any]] = ..., kw_only: bool = ...,) -> Any: ...
40
41
42 class Field(Generic[_T]): pass
43
44 @overload
45 def is_dataclass(obj: DataclassInstance) -> Literal[True]: ...
46 @overload
47 def is_dataclass(obj: type) -> TypeGuard[type[DataclassInstance]]: ...
48 @overload
49 def is_dataclass(obj: object) -> TypeGuard[DataclassInstance | type[DataclassInstance]]: ...
50
51
52 def replace(__obj: _DataclassT, **changes: Any) -> _DataclassT: ...