Skip to content

More accurate dataclasses.dataclass overloads #14095

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions stdlib/@tests/test_cases/check_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
dc.replace(y)


class _D: ...


custom_dc = dc.dataclass(_D, init=True)
assert_type(custom_dc, type[_D])

custom_dc_2 = dc.dataclass(None, init=True)(_D)
assert_type(custom_dc_2, type[_D])


# Regression test for #11653
D = dc.make_dataclass(
"D", [("a", Union[int, None]), "y", ("z", Annotated[FrozenSet[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))]
Expand Down
53 changes: 49 additions & 4 deletions stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,28 @@ def asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, An
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]]: ...
@overload
def dataclass(cls: 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 = None,
/,
*,
init: bool = True,
repr: bool = True,
Expand All @@ -95,6 +109,23 @@ if sys.version_info >= (3, 11):
elif sys.version_info >= (3, 10):
@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,
) -> type[_T]: ...
@overload
def dataclass(
cls: None = None,
/,
*,
init: bool = True,
repr: bool = True,
Expand All @@ -110,6 +141,20 @@ elif sys.version_info >= (3, 10):
else:
@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,
) -> type[_T]: ...
@overload
def dataclass(
cls: None = None,
/,
*,
init: bool = True,
repr: bool = True,
Expand Down