Skip to content
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

fix(dataclass): unify and clarify overloads for cls None #13190

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions stdlib/@tests/test_cases/check_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what is_dataclass adds to this test case.

assert_type(b, Bar)
53 changes: 48 additions & 5 deletions stdlib/dataclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now duplicates the overload under if sys.version_info >= (3, 11) branch below.

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,
Expand All @@ -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,
Expand All @@ -88,18 +128,21 @@ 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,
eq: bool = True,
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
Expand Down
Loading