Skip to content

Commit fc0a3f9

Browse files
committed
dataclass.pyi improvements: _DictFactory and _TupleFactory are now callables; add MISSING; field typed with overloads; namespace is optional.
1 parent 660bbc9 commit fc0a3f9

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

stdlib/3.7/dataclasses.pyi

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ from typing import overload, Any, Callable, Dict, Generic, Iterable, Mapping, Op
33

44
_T = TypeVar('_T')
55

6-
_DictType = TypeVar('_DictType', bound=dict)
7-
_TupleType = TypeVar('_TupleType', bound=tuple)
6+
_DictFactory = Callable[List[Tuple[str, Any]], _T]
7+
_TupleFactory = Callable[List[Any], _T]
88

99
class _MISSING_TYPE: ...
10+
MISSING: _MISSING_TYPE
1011

1112
class _InitVarMeta(type): ...
1213

13-
def asdict(obj: Any, *, dict_factory: _DictType = ...) -> _DictType: ...
14+
def asdict(obj: Any, *, dict_factory: _DictFactory = ...) -> _T: ...
15+
16+
def astuple(obj: Any, *, tuple_factory: _TupleFactory = ...) -> _T: ...
1417

15-
def astuple(obj: Any, *, tuple_factory: _TupleType = ...) -> _TupleType: ...
1618

1719
@overload
1820
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
@@ -34,20 +36,32 @@ class Field(Generic[_T]):
3436
metadata: Optional[Mapping[str, Any]]
3537

3638

37-
def field(*, default: Union[_T, _MISSING_TYPE] = ..., default_factory: Union[Callable[[], _T], _MISSING_TYPE] = ...,
39+
@overload # `default` and `default_factory` are optional and mutually exclusive.
40+
def field(*, default: _T = ..., default_factory: _MISSING_TYPE = ...,
41+
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
42+
metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ...
43+
44+
@overload
45+
def field(*, default: _MISSING_TYPE = ..., default_factory: Callable[[], _T] = ...,
46+
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
47+
metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ...
48+
49+
@overload
50+
def field(*, default: _MISSING_TYPE = ..., default_factory: _MISSING_TYPE = ...,
3851
init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ...,
39-
metadata: Optional[Mapping[str, Any]] = ...) -> Field: ...
52+
metadata: Optional[Mapping[str, Any]] = ...) -> Field[Any]: ...
53+
4054

41-
def fields(class_or_instance: Type) -> Tuple[Field, ...]: ...
55+
def fields(class_or_instance: Any) -> Tuple[Field[Any], ...]: ...
4256

4357
def is_dataclass(obj: Any) -> bool: ...
4458

4559
class FrozenInstanceError(AttributeError): ...
4660

4761
class InitVar(metaclass=_InitVarMeta): ...
4862

49-
def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field]]], *,
50-
bases: Tuple[type, ...] = ..., namespace: Dict[str, Any] = ...,
63+
def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field[Any]]]], *,
64+
bases: Tuple[type, ...] = ..., namespace: Optional[Dict[str, Any]] = ...,
5165
init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., hash: bool = ..., frozen: bool = ...): ...
5266

5367
def replace(obj: _T, **changes: Any) -> _T: ...

0 commit comments

Comments
 (0)