From 89f22e91fdc89f5830281dd921cda04ff275ad37 Mon Sep 17 00:00:00 2001 From: Ivan Sitkin Date: Wed, 4 Dec 2024 13:40:26 +0100 Subject: [PATCH 1/5] fix(dataclass): unify and clarify overloads for cls None --- stdlib/@tests/test_cases/check_dataclasses.py | 10 ++++ stdlib/dataclasses.pyi | 54 +++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index 4582e14ae26b..e61848619d58 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -99,3 +99,13 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None: # in case a type checker decides to add some special-casing for # `make_dataclass` in the future) assert_type(D.__mro__, Tuple[type, ...]) + + +@dc.dataclass(frozen=True) +class Bar: + attr: str + +b = Bar(attr="attr") + +if dc.is_dataclass(b): + assert_type(b, Bar) diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 3295b1c1f835..744067af5018 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -55,13 +55,51 @@ def astuple(obj: DataclassInstance) -> tuple[Any, ...]: ... @overload def astuple(obj: DataclassInstance, *, tuple_factory: Callable[[list[Any]], _T]) -> _T: ... @overload -def dataclass(cls: None, /) -> Callable[[type[_T]], type[_T]]: ... +def dataclass( + cls: type[_T], + /, + *, + init: bool = True, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, +) -> type[_T]: ... @overload -def dataclass(cls: type[_T], /) -> type[_T]: ... +def dataclass( + cls: None = ..., + /, + *, + init: bool = True, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, +) -> Callable[[type[_T]], type[_T]]: ... if sys.version_info >= (3, 11): @overload def dataclass( + cls: type[_T], + /, + *, + init: bool = True, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args: bool = True, + kw_only: bool = False, + slots: bool = False, + weakref_slot: bool = False, + ) -> type[_T]: ... + @overload + def dataclass( + cls: None, + /, *, init: bool = True, repr: bool = True, @@ -75,9 +113,12 @@ if sys.version_info >= (3, 11): weakref_slot: bool = False, ) -> Callable[[type[_T]], type[_T]]: ... + elif sys.version_info >= (3, 10): @overload def dataclass( + cls: type[_T], + /, *, init: bool = True, repr: bool = True, @@ -88,11 +129,11 @@ elif sys.version_info >= (3, 10): match_args: bool = True, kw_only: bool = False, slots: bool = False, - ) -> Callable[[type[_T]], type[_T]]: ... - -else: + ) -> type[_T]: ... @overload def dataclass( + cls: None, + /, *, init: bool = True, repr: bool = True, @@ -100,6 +141,9 @@ else: order: bool = False, unsafe_hash: bool = False, frozen: bool = False, + match_args: bool = True, + kw_only: bool = False, + slots: bool = False, ) -> Callable[[type[_T]], type[_T]]: ... # See https://github.com/python/mypy/issues/10750 From 3109071903348f30c2eafa48ebc153504b5fb8de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:43:57 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/@tests/test_cases/check_dataclasses.py | 1 + stdlib/dataclasses.pyi | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index e61848619d58..8a74911e779a 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -105,6 +105,7 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None: class Bar: attr: str + b = Bar(attr="attr") if dc.is_dataclass(b): diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 744067af5018..027c7b5d66eb 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -113,7 +113,6 @@ if sys.version_info >= (3, 11): weakref_slot: bool = False, ) -> Callable[[type[_T]], type[_T]]: ... - elif sys.version_info >= (3, 10): @overload def dataclass( From 0385b898e81edc5eff53fe95743b5f207030b99b Mon Sep 17 00:00:00 2001 From: Ivan Sitkin Date: Wed, 4 Dec 2024 13:52:51 +0100 Subject: [PATCH 3/5] fix(dataclass): unify and clarify overloads for cls None --- stdlib/@tests/test_cases/check_dataclasses.py | 4 +++- stdlib/dataclasses.pyi | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index 8a74911e779a..36357f08903c 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -101,7 +101,9 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None: assert_type(D.__mro__, Tuple[type, ...]) -@dc.dataclass(frozen=True) +decorator = dc.dataclass(None, frozen=True) + +@decorator class Bar: attr: str diff --git a/stdlib/dataclasses.pyi b/stdlib/dataclasses.pyi index 027c7b5d66eb..c8b8c61eb93b 100644 --- a/stdlib/dataclasses.pyi +++ b/stdlib/dataclasses.pyi @@ -98,7 +98,7 @@ if sys.version_info >= (3, 11): ) -> type[_T]: ... @overload def dataclass( - cls: None, + cls: None = ..., /, *, init: bool = True, @@ -131,7 +131,7 @@ elif sys.version_info >= (3, 10): ) -> type[_T]: ... @overload def dataclass( - cls: None, + cls: None = ..., /, *, init: bool = True, From e9fe795144997bd8f091372f2fb867fc64465699 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:58:27 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/@tests/test_cases/check_dataclasses.py | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index 36357f08903c..a56c26b48eb1 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -103,6 +103,7 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None: decorator = dc.dataclass(None, frozen=True) + @decorator class Bar: attr: str From 6b9118f94b023dbf4d2136dde480d7576fee771c Mon Sep 17 00:00:00 2001 From: Ivan Sitkin Date: Wed, 4 Dec 2024 14:04:09 +0100 Subject: [PATCH 5/5] fix(dataclass): unify and clarify overloads for cls None --- stdlib/@tests/test_cases/check_dataclasses.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index 36357f08903c..af40324d7905 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -101,9 +101,7 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None: assert_type(D.__mro__, Tuple[type, ...]) -decorator = dc.dataclass(None, frozen=True) - -@decorator +@dc.dataclass(None, frozen=True) class Bar: attr: str