From 5df4c3d2756e36011c6fb71d5f62eb88d28f3a1a Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 17 Jan 2021 16:54:08 -0800 Subject: [PATCH] dataclasses: various fixes (#4937) --- stdlib/3.7/dataclasses.pyi | 48 +++++++++++++++++++++++------- tests/stubtest_whitelists/py39.txt | 1 + 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 1f5b6b4d3..678e6bcb8 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -17,14 +17,27 @@ def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) -> def astuple(obj: Any) -> Tuple[Any, ...]: ... @overload def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T]) -> _T: ... -@overload -def dataclass(_cls: Type[_T]) -> Type[_T]: ... -@overload -def dataclass(_cls: None) -> Callable[[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]]: ... + +if sys.version_info >= (3, 8): + # cls argument is now positional-only + @overload + def dataclass(__cls: Type[_T]) -> Type[_T]: ... + @overload + def dataclass(__cls: None) -> Callable[[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]]: ... + +else: + @overload + def dataclass(_cls: Type[_T]) -> Type[_T]: ... + @overload + def dataclass(_cls: None) -> Callable[[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 @@ -36,6 +49,16 @@ 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, 9): def __class_getitem__(cls, item: Any) -> GenericAlias: ... @@ -76,8 +99,13 @@ def is_dataclass(obj: Any) -> bool: ... class FrozenInstanceError(AttributeError): ... class InitVar(Generic[_T]): + type: Type[_T] + def __init__(self, type: Type[_T]) -> None: ... if sys.version_info >= (3, 9): - def __class_getitem__(cls, type: Any) -> GenericAlias: ... + @overload + def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ... + @overload + def __class_getitem__(cls, type: Any) -> InitVar[Any]: ... def make_dataclass( cls_name: str, @@ -92,4 +120,4 @@ def make_dataclass( unsafe_hash: bool = ..., frozen: bool = ..., ) -> type: ... -def replace(obj: _T, **changes: Any) -> _T: ... +def replace(__obj: _T, **changes: Any) -> _T: ... diff --git a/tests/stubtest_whitelists/py39.txt b/tests/stubtest_whitelists/py39.txt index 25309a9e6..b7584cb59 100644 --- a/tests/stubtest_whitelists/py39.txt +++ b/tests/stubtest_whitelists/py39.txt @@ -37,6 +37,7 @@ copy.PyStringMap dataclasses.Field.__init__ dataclasses.InitVar.__init__ dataclasses.field +dataclasses.InitVar.__class_getitem__ # stubtest bug. doesn't do the right thing with overload + implicit classmethod __class_getitem__ dummy_threading email.message.MIMEPart.as_string enum.Enum._generate_next_value_