From b8253794312e9e783fb2bbb1ee880c2d80f6d268 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Fri, 15 Jan 2021 23:07:11 -0800 Subject: [PATCH 1/6] dataclasses: various fixes --- stdlib/3.7/dataclasses.pyi | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 1f5b6b4d35b9..c7c0af3587b1 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -18,9 +18,9 @@ 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]: ... +def dataclass(__cls: Type[_T]) -> Type[_T]: ... @overload -def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ... +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 = ... @@ -36,6 +36,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,6 +86,8 @@ def is_dataclass(obj: Any) -> bool: ... class FrozenInstanceError(AttributeError): ... class InitVar(Generic[_T]): + type: Type + def __init__(self, type: Type) -> None: ... if sys.version_info >= (3, 9): def __class_getitem__(cls, type: Any) -> GenericAlias: ... @@ -92,4 +104,4 @@ def make_dataclass( unsafe_hash: bool = ..., frozen: bool = ..., ) -> type: ... -def replace(obj: _T, **changes: Any) -> _T: ... +def replace(__obj: _T, **changes: Any) -> _T: ... From ce56245fd9524cb744e2ca79c9ec60b6a06142c8 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sat, 16 Jan 2021 11:32:01 -0800 Subject: [PATCH 2/6] fix for python 3.7 --- stdlib/3.7/dataclasses.pyi | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index c7c0af3587b1..f191463d9754 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -17,14 +17,24 @@ 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): + @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 From 447cba348d4f04810017db885295e6394cb786d7 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sat, 16 Jan 2021 11:37:23 -0800 Subject: [PATCH 3/6] blacken --- stdlib/3.7/dataclasses.pyi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index f191463d9754..2a0e37aa1836 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -17,6 +17,7 @@ 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: ... + if sys.version_info >= (3, 8): @overload def dataclass(__cls: Type[_T]) -> Type[_T]: ... @@ -26,6 +27,7 @@ if sys.version_info >= (3, 8): 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]: ... From 16b720c977d689006acf2c0629c8f67213593ce1 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sun, 17 Jan 2021 10:50:52 -0800 Subject: [PATCH 4/6] fix InitVar --- stdlib/3.7/dataclasses.pyi | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index 2a0e37aa1836..f2df0740c548 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -98,10 +98,13 @@ def is_dataclass(obj: Any) -> bool: ... class FrozenInstanceError(AttributeError): ... class InitVar(Generic[_T]): - type: Type - def __init__(self, type: Type) -> None: ... + 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, From b707d4f8f40b615fc44c1a45d2841886b860fae6 Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sun, 17 Jan 2021 10:51:30 -0800 Subject: [PATCH 5/6] add comment --- stdlib/3.7/dataclasses.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/3.7/dataclasses.pyi b/stdlib/3.7/dataclasses.pyi index f2df0740c548..678e6bcb85a1 100644 --- a/stdlib/3.7/dataclasses.pyi +++ b/stdlib/3.7/dataclasses.pyi @@ -19,6 +19,7 @@ def astuple(obj: Any) -> Tuple[Any, ...]: ... def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T]) -> _T: ... if sys.version_info >= (3, 8): + # cls argument is now positional-only @overload def dataclass(__cls: Type[_T]) -> Type[_T]: ... @overload From 422a662e855beff037c6e107f81618151feefeed Mon Sep 17 00:00:00 2001 From: hauntsaninja <> Date: Sun, 17 Jan 2021 10:57:34 -0800 Subject: [PATCH 6/6] allowlist --- tests/stubtest_whitelists/py39.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/stubtest_whitelists/py39.txt b/tests/stubtest_whitelists/py39.txt index 25309a9e61a3..b7584cb59739 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_