Skip to content

Commit 0b1ee93

Browse files
authored
More accurate dataclasses.dataclass overloads (python#14095)
1 parent f60b86e commit 0b1ee93

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

stdlib/@tests/test_cases/check_dataclasses.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ def check_other_isdataclass_overloads(x: type, y: object) -> None:
9191
dc.replace(y)
9292

9393

94+
class _D: ...
95+
96+
97+
custom_dc = dc.dataclass(_D, init=True)
98+
assert_type(custom_dc, type[_D])
99+
100+
custom_dc_2 = dc.dataclass(None, init=True)(_D)
101+
assert_type(custom_dc_2, type[_D])
102+
103+
94104
# Regression test for #11653
95105
D = dc.make_dataclass(
96106
"D", [("a", Union[int, None]), "y", ("z", Annotated[FrozenSet[bytes], "metadata"], dc.field(default=frozenset({b"foo"})))]

stdlib/dataclasses.pyi

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,28 @@ def asdict(obj: DataclassInstance, *, dict_factory: Callable[[list[tuple[str, An
7171
def astuple(obj: DataclassInstance) -> tuple[Any, ...]: ...
7272
@overload
7373
def astuple(obj: DataclassInstance, *, tuple_factory: Callable[[list[Any]], _T]) -> _T: ...
74-
@overload
75-
def dataclass(cls: None, /) -> Callable[[type[_T]], type[_T]]: ...
76-
@overload
77-
def dataclass(cls: type[_T], /) -> type[_T]: ...
7874

7975
if sys.version_info >= (3, 11):
8076
@overload
8177
def dataclass(
78+
cls: type[_T],
79+
/,
80+
*,
81+
init: bool = True,
82+
repr: bool = True,
83+
eq: bool = True,
84+
order: bool = False,
85+
unsafe_hash: bool = False,
86+
frozen: bool = False,
87+
match_args: bool = True,
88+
kw_only: bool = False,
89+
slots: bool = False,
90+
weakref_slot: bool = False,
91+
) -> type[_T]: ...
92+
@overload
93+
def dataclass(
94+
cls: None = None,
95+
/,
8296
*,
8397
init: bool = True,
8498
repr: bool = True,
@@ -95,6 +109,23 @@ if sys.version_info >= (3, 11):
95109
elif sys.version_info >= (3, 10):
96110
@overload
97111
def dataclass(
112+
cls: type[_T],
113+
/,
114+
*,
115+
init: bool = True,
116+
repr: bool = True,
117+
eq: bool = True,
118+
order: bool = False,
119+
unsafe_hash: bool = False,
120+
frozen: bool = False,
121+
match_args: bool = True,
122+
kw_only: bool = False,
123+
slots: bool = False,
124+
) -> type[_T]: ...
125+
@overload
126+
def dataclass(
127+
cls: None = None,
128+
/,
98129
*,
99130
init: bool = True,
100131
repr: bool = True,
@@ -110,6 +141,20 @@ elif sys.version_info >= (3, 10):
110141
else:
111142
@overload
112143
def dataclass(
144+
cls: type[_T],
145+
/,
146+
*,
147+
init: bool = True,
148+
repr: bool = True,
149+
eq: bool = True,
150+
order: bool = False,
151+
unsafe_hash: bool = False,
152+
frozen: bool = False,
153+
) -> type[_T]: ...
154+
@overload
155+
def dataclass(
156+
cls: None = None,
157+
/,
113158
*,
114159
init: bool = True,
115160
repr: bool = True,

0 commit comments

Comments
 (0)