diff --git a/stdlib/@tests/test_cases/check_dataclasses.py b/stdlib/@tests/test_cases/check_dataclasses.py index 4582e14ae26b..af40324d7905 100644 --- a/stdlib/@tests/test_cases/check_dataclasses.py +++ b/stdlib/@tests/test_cases/check_dataclasses.py @@ -99,3 +99,14 @@ 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(None, 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..c8b8c61eb93b 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, @@ -78,6 +116,8 @@ if sys.version_info >= (3, 11): elif sys.version_info >= (3, 10): @overload def dataclass( + cls: type[_T], + /, *, init: bool = True, repr: bool = True, @@ -88,11 +128,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 +140,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