From 4bfdba71cba6599befae40c4344a839e5a550b78 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Tue, 25 May 2021 05:43:06 -0700 Subject: [PATCH] dataclasses: update for py310 (#5525) --- stdlib/dataclasses.pyi | 180 +++++++++++++++++++++++++++++------------ 1 file changed, 127 insertions(+), 53 deletions(-) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 1e619516f..8d4ab1b1e 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -10,6 +10,10 @@ class _MISSING_TYPE: ... MISSING: _MISSING_TYPE +if sys.version_info >= (3, 10): + class _KW_ONLY_TYPE: ... + KW_ONLY: _KW_ONLY_TYPE + @overload def asdict(obj: Any) -> Dict[str, Any]: ... @overload @@ -20,7 +24,6 @@ def astuple(obj: Any) -> Tuple[Any, ...]: ... def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T]) -> _T: ... if sys.version_info >= (3, 10): - # Added match_args @overload def dataclass(__cls: Type[_T]) -> Type[_T]: ... @overload @@ -35,6 +38,8 @@ if sys.version_info >= (3, 10): unsafe_hash: bool = ..., frozen: bool = ..., match_args: bool = ..., + kw_only: bool = ..., + slots: bool = ..., ) -> Callable[[Type[_T]], Type[_T]]: ... elif sys.version_info >= (3, 8): @@ -68,50 +73,100 @@ class Field(Generic[_T]): init: bool compare: bool metadata: Mapping[str, Any] - def __init__( - self, - default: _T, - default_factory: Callable[[], _T], - init: bool, - repr: bool, - hash: Optional[bool], - compare: bool, - metadata: Mapping[str, Any], - ) -> None: ... + if sys.version_info >= (3, 10): + kw_only: bool + def __init__( + self, + default: _T, + default_factory: Callable[[], _T], + init: bool, + repr: bool, + hash: Optional[bool], + compare: bool, + metadata: Mapping[str, Any], + kw_only: bool, + ) -> None: ... + else: + def __init__( + self, + default: _T, + default_factory: Callable[[], _T], + init: bool, + repr: bool, + hash: Optional[bool], + compare: bool, + metadata: Mapping[str, Any], + ) -> None: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... # NOTE: Actual return type is 'Field[_T]', but we want to help type checkers # to understand the magic that happens at runtime. -@overload # `default` and `default_factory` are optional and mutually exclusive. -def field( - *, - default: _T, - init: bool = ..., - repr: bool = ..., - hash: Optional[bool] = ..., - compare: bool = ..., - metadata: Optional[Mapping[str, Any]] = ..., -) -> _T: ... -@overload -def field( - *, - default_factory: Callable[[], _T], - init: bool = ..., - repr: bool = ..., - hash: Optional[bool] = ..., - compare: bool = ..., - metadata: Optional[Mapping[str, Any]] = ..., -) -> _T: ... -@overload -def field( - *, - init: bool = ..., - repr: bool = ..., - hash: Optional[bool] = ..., - compare: bool = ..., - metadata: Optional[Mapping[str, Any]] = ..., -) -> Any: ... +if sys.version_info >= (3, 10): + @overload # `default` and `default_factory` are optional and mutually exclusive. + def field( + *, + default: _T, + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + kw_only: bool = ..., + ) -> _T: ... + @overload + def field( + *, + default_factory: Callable[[], _T], + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + kw_only: bool = ..., + ) -> _T: ... + @overload + def field( + *, + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + kw_only: bool = ..., + ) -> Any: ... + +else: + @overload # `default` and `default_factory` are optional and mutually exclusive. + def field( + *, + default: _T, + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + ) -> _T: ... + @overload + def field( + *, + default_factory: Callable[[], _T], + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + ) -> _T: ... + @overload + def field( + *, + init: bool = ..., + repr: bool = ..., + hash: Optional[bool] = ..., + compare: bool = ..., + metadata: Optional[Mapping[str, Any]] = ..., + ) -> Any: ... + def fields(class_or_instance: Any) -> Tuple[Field[Any], ...]: ... def is_dataclass(obj: Any) -> bool: ... @@ -126,17 +181,36 @@ class InitVar(Generic[_T]): @overload def __class_getitem__(cls, type: Any) -> InitVar[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 = ..., - unsafe_hash: bool = ..., - frozen: bool = ..., -) -> type: ... +if sys.version_info >= (3, 10): + 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 = ..., + unsafe_hash: bool = ..., + frozen: bool = ..., + match_args: bool = ..., + slots: bool = ..., + ) -> type: ... + +else: + 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 = ..., + unsafe_hash: bool = ..., + frozen: bool = ..., + ) -> type: ... + def replace(__obj: _T, **changes: Any) -> _T: ...