Skip to content

Commit c1f89a9

Browse files
garyvdmJelleZijlstra
authored andcommitted
Make dataclasses available as a backported third_party library. (#2354)
1 parent 9a01b81 commit c1f89a9

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

tests/check_consistent.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
{'stdlib/3/concurrent/futures/_base.pyi', 'third_party/2/concurrent/futures/_base.pyi'},
2323
{'stdlib/3/concurrent/futures/thread.pyi', 'third_party/2/concurrent/futures/thread.pyi'},
2424
{'stdlib/3/concurrent/futures/process.pyi', 'third_party/2/concurrent/futures/process.pyi'},
25+
{'stdlib/3.7/dataclasses.pyi', 'third_party/3/dataclasses.pyi'},
2526
]
2627

2728
def main():

third_party/3/dataclasses.pyi

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from typing import overload, Any, Callable, Dict, Generic, Iterable, List, Mapping, Optional, Tuple, Type, TypeVar, Union
2+
3+
4+
_T = TypeVar('_T')
5+
6+
class _MISSING_TYPE: ...
7+
MISSING: _MISSING_TYPE
8+
9+
def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T] = ...) -> _T: ...
10+
11+
def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T] = ...) -> _T: ...
12+
13+
14+
@overload
15+
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
16+
17+
@overload
18+
def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ...,
19+
unsafe_hash: bool = ..., frozen: bool = ...) -> Callable[[Type[_T]], Type[_T]]: ...
20+
21+
22+
class Field(Generic[_T]):
23+
name: str
24+
type: Type[_T]
25+
default: _T
26+
default_factory: Callable[[], _T]
27+
repr: bool
28+
hash: Optional[bool]
29+
init: bool
30+
compare: bool
31+
metadata: Optional[Mapping[str, Any]]
32+
33+
34+
# NOTE: Actual return type is 'Field[_T]', but we want to help type checkers
35+
# to understand the magic that happens at runtime.
36+
@overload # `default` and `default_factory` are optional and mutually exclusive.
37+
def field(*, default: _T,
38+
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
39+
metadata: Optional[Mapping[str, Any]] = ...) -> _T: ...
40+
41+
@overload
42+
def field(*, default_factory: Callable[[], _T],
43+
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
44+
metadata: Optional[Mapping[str, Any]] = ...) -> _T: ...
45+
46+
@overload
47+
def field(*,
48+
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
49+
metadata: Optional[Mapping[str, Any]] = ...) -> Any: ...
50+
51+
52+
def fields(class_or_instance: Any) -> Tuple[Field[Any], ...]: ...
53+
54+
def is_dataclass(obj: Any) -> bool: ...
55+
56+
class FrozenInstanceError(AttributeError): ...
57+
58+
class InitVar(Generic[_T]): ...
59+
60+
def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field[Any]]]], *,
61+
bases: Tuple[type, ...] = ..., namespace: Optional[Dict[str, Any]] = ...,
62+
init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., hash: bool = ..., frozen: bool = ...): ...
63+
64+
def replace(obj: _T, **changes: Any) -> _T: ...

0 commit comments

Comments
 (0)