From 49986e2295db66a90eef39059accda3b856cfdd8 Mon Sep 17 00:00:00 2001 From: George King Date: Mon, 5 Mar 2018 21:36:58 -0500 Subject: [PATCH 1/7] Preliminary implementation of stdlib/3.7/dataclasses.pyi. --- stdlib/3.7/dataclasses.pyi | 53 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 stdlib/3.7/dataclasses.pyi diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi new file mode 100644 index 000000000000..f5e68d4d4f3e --- /dev/null +++ b/stdlib/3.7/dataclasses.pyi @@ -0,0 +1,53 @@ +from typing import overload, Any, Callable, Dict, Generic, Iterable, Mapping, Optional, Tuple, Type, TypeVar, Union + + +_T = TypeVar('_T') + +_DictType = TypeVar('_DictType', bound=dict) +_TupleType = TypeVar('_TupleType', bound=tuple) + +class _MISSING_TYPE: ... + +class _InitVarMeta(type): ... + +def asdict(obj: Any, *, dict_factory: _DictType = ...) -> _DictType: ... + +def astuple(obj: Any, *, tuple_factory: _TupleType = ...) -> _TupleType: ... + +@overload +def dataclass(_cls: Type[_T]) -> Type[_T]: ... + +@overload +def dataclass(*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., + unsafe_hash: bool = ..., frozen: bool = ...) -> Callable[[Type[_T]], Type[_T]]: ... + + +class Field(Generic[_T]): + name: str + type: Type[_T] + default: _T + default_factory: Callable[[], _T] + repr: bool + hash: Optional[bool] + init: bool + compare: bool + metadata: Optional[Mapping[str, Any]] + + +def field(*, default: Union[_T, _MISSING_TYPE] = ..., default_factory: Union[Callable[[], _T], _MISSING_TYPE] = ..., + init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ...) -> Field: ... + +def fields(class_or_instance: Type) -> Tuple[Field, ...]: ... + +def is_dataclass(obj: Any) -> bool: ... + +class FrozenInstanceError(AttributeError): ... + +class InitVar(metaclass=_InitVarMeta): ... + +def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field]]], *, + bases: Tuple[type, ...] = ..., namespace: Dict[str, Any] = ..., + init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., hash: bool = ..., frozen: bool = ...): ... + +def replace(obj: _T, **changes: Any) -> _T: ... From 5ecafd6d4208be078d8fe02694e3ff8cdc450584 Mon Sep 17 00:00:00 2001 From: George King Date: Mon, 28 May 2018 22:53:55 -0400 Subject: [PATCH 2/7] dataclass.pyi improvements: _DictFactory and _TupleFactory are now callables; add MISSING; field typed with overloads; namespace is optional. --- stdlib/3.7/dataclasses.pyi | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index f5e68d4d4f3e..81538954fcfb 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -3,16 +3,18 @@ from typing import overload, Any, Callable, Dict, Generic, Iterable, Mapping, Op _T = TypeVar('_T') -_DictType = TypeVar('_DictType', bound=dict) -_TupleType = TypeVar('_TupleType', bound=tuple) +_DictFactory = Callable[List[Tuple[str, Any]], _T] +_TupleFactory = Callable[List[Any], _T] class _MISSING_TYPE: ... +MISSING: _MISSING_TYPE class _InitVarMeta(type): ... -def asdict(obj: Any, *, dict_factory: _DictType = ...) -> _DictType: ... +def asdict(obj: Any, *, dict_factory: _DictFactory = ...) -> _T: ... + +def astuple(obj: Any, *, tuple_factory: _TupleFactory = ...) -> _T: ... -def astuple(obj: Any, *, tuple_factory: _TupleType = ...) -> _TupleType: ... @overload def dataclass(_cls: Type[_T]) -> Type[_T]: ... @@ -34,11 +36,23 @@ class Field(Generic[_T]): metadata: Optional[Mapping[str, Any]] -def field(*, default: Union[_T, _MISSING_TYPE] = ..., default_factory: Union[Callable[[], _T], _MISSING_TYPE] = ..., +@overload # `default` and `default_factory` are optional and mutually exclusive. +def field(*, default: _T = ..., default_factory: _MISSING_TYPE = ..., + init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ... + +@overload +def field(*, default: _MISSING_TYPE = ..., default_factory: Callable[[], _T] = ..., + init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ... + +@overload +def field(*, default: _MISSING_TYPE = ..., default_factory: _MISSING_TYPE = ..., init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., - metadata: Optional[Mapping[str, Any]] = ...) -> Field: ... + metadata: Optional[Mapping[str, Any]] = ...) -> Field[Any]: ... + -def fields(class_or_instance: Type) -> Tuple[Field, ...]: ... +def fields(class_or_instance: Any) -> Tuple[Field[Any], ...]: ... def is_dataclass(obj: Any) -> bool: ... @@ -46,8 +60,8 @@ class FrozenInstanceError(AttributeError): ... class InitVar(metaclass=_InitVarMeta): ... -def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field]]], *, - bases: Tuple[type, ...] = ..., namespace: Dict[str, Any] = ..., +def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field[Any]]]], *, + bases: Tuple[type, ...] = ..., namespace: Optional[Dict[str, Any]] = ..., init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., hash: bool = ..., frozen: bool = ...): ... def replace(obj: _T, **changes: Any) -> _T: ... From 4b4362e29ad99a04c1dbcfb426ac2503b08dab6f Mon Sep 17 00:00:00 2001 From: George King Date: Tue, 29 May 2018 08:25:07 -0400 Subject: [PATCH 3/7] dataclass.pyi typos: missing List; additional brackets around callable. --- stdlib/3.7/dataclasses.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 81538954fcfb..ba2a786856c5 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -1,10 +1,10 @@ -from typing import overload, Any, Callable, Dict, Generic, Iterable, Mapping, Optional, Tuple, Type, TypeVar, Union +from typing import overload, Any, Callable, Dict, Generic, Iterable, List, Mapping, Optional, Tuple, Type, TypeVar, Union _T = TypeVar('_T') -_DictFactory = Callable[List[Tuple[str, Any]], _T] -_TupleFactory = Callable[List[Any], _T] +_DictFactory = Callable[[List[Tuple[str, Any]]], _T] +_TupleFactory = Callable[[List[Any]], _T] class _MISSING_TYPE: ... MISSING: _MISSING_TYPE From 3ce43b5b66bc0de1d3e96235d1eb2f0edc382de8 Mon Sep 17 00:00:00 2001 From: George King Date: Tue, 29 May 2018 08:26:22 -0400 Subject: [PATCH 4/7] dataclasses improvement: inline single use of _DictFactory, _TupleFactory. --- stdlib/3.7/dataclasses.pyi | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index ba2a786856c5..d6ffd159406b 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -3,17 +3,14 @@ from typing import overload, Any, Callable, Dict, Generic, Iterable, List, Mappi _T = TypeVar('_T') -_DictFactory = Callable[[List[Tuple[str, Any]]], _T] -_TupleFactory = Callable[[List[Any]], _T] - class _MISSING_TYPE: ... MISSING: _MISSING_TYPE class _InitVarMeta(type): ... -def asdict(obj: Any, *, dict_factory: _DictFactory = ...) -> _T: ... +def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T] = ...) -> _T: ... -def astuple(obj: Any, *, tuple_factory: _TupleFactory = ...) -> _T: ... +def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T] = ...) -> _T: ... @overload From fe8fd7fa9feee83ab20db0052d95f8c58e7b6270 Mon Sep 17 00:00:00 2001 From: George King Date: Tue, 29 May 2018 08:44:45 -0400 Subject: [PATCH 5/7] dataclasses flake8 fix: two spaces before inline comment. --- stdlib/3.7/dataclasses.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index d6ffd159406b..4a70cd13b8d8 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -33,7 +33,7 @@ class Field(Generic[_T]): metadata: Optional[Mapping[str, Any]] -@overload # `default` and `default_factory` are optional and mutually exclusive. +@overload # `default` and `default_factory` are optional and mutually exclusive. def field(*, default: _T = ..., default_factory: _MISSING_TYPE = ..., init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ... From f78cd56e2cf4ce219bbfc29c87700d85a7aa29f5 Mon Sep 17 00:00:00 2001 From: George King Date: Tue, 29 May 2018 09:09:43 -0400 Subject: [PATCH 6/7] dataclasses: remove excessive default parameters from overloads of `field`. --- stdlib/3.7/dataclasses.pyi | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 4a70cd13b8d8..7eb248ff49ce 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -34,17 +34,17 @@ class Field(Generic[_T]): @overload # `default` and `default_factory` are optional and mutually exclusive. -def field(*, default: _T = ..., default_factory: _MISSING_TYPE = ..., +def field(*, default: _T, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ... @overload -def field(*, default: _MISSING_TYPE = ..., default_factory: Callable[[], _T] = ..., +def field(*, default_factory: Callable[[], _T], init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Field[_T]: ... @overload -def field(*, default: _MISSING_TYPE = ..., default_factory: _MISSING_TYPE = ..., +def field(*, init: bool = ..., repr: bool = ..., hash: Optional[bool] = ..., compare: bool = ..., metadata: Optional[Mapping[str, Any]] = ...) -> Field[Any]: ... From 971430a91c0d1e3629513fe473f08fb5877e56c7 Mon Sep 17 00:00:00 2001 From: George King Date: Wed, 30 May 2018 15:35:23 -0400 Subject: [PATCH 7/7] dataclasses improvement: InitVar is now Generic[_T]; remove _InitVarMeta. --- stdlib/3.7/dataclasses.pyi | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 7eb248ff49ce..db032733521c 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -6,8 +6,6 @@ _T = TypeVar('_T') class _MISSING_TYPE: ... MISSING: _MISSING_TYPE -class _InitVarMeta(type): ... - def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T] = ...) -> _T: ... def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T] = ...) -> _T: ... @@ -55,7 +53,7 @@ def is_dataclass(obj: Any) -> bool: ... class FrozenInstanceError(AttributeError): ... -class InitVar(metaclass=_InitVarMeta): ... +class InitVar(Generic[_T]): ... def make_dataclass(cls_name: str, fields: Iterable[Union[str, Tuple[str, type], Tuple[str, type, Field[Any]]]], *, bases: Tuple[type, ...] = ..., namespace: Optional[Dict[str, Any]] = ...,