Skip to content

Commit 5df4c3d

Browse files
authored
dataclasses: various fixes (#4937)
1 parent 8de9b85 commit 5df4c3d

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

stdlib/3.7/dataclasses.pyi

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,27 @@ def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) ->
1717
def astuple(obj: Any) -> Tuple[Any, ...]: ...
1818
@overload
1919
def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T]) -> _T: ...
20-
@overload
21-
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
22-
@overload
23-
def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
24-
@overload
25-
def dataclass(
26-
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
27-
) -> Callable[[Type[_T]], Type[_T]]: ...
20+
21+
if sys.version_info >= (3, 8):
22+
# cls argument is now positional-only
23+
@overload
24+
def dataclass(__cls: Type[_T]) -> Type[_T]: ...
25+
@overload
26+
def dataclass(__cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
27+
@overload
28+
def dataclass(
29+
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
30+
) -> Callable[[Type[_T]], Type[_T]]: ...
31+
32+
else:
33+
@overload
34+
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
35+
@overload
36+
def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
37+
@overload
38+
def dataclass(
39+
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
40+
) -> Callable[[Type[_T]], Type[_T]]: ...
2841

2942
class Field(Generic[_T]):
3043
name: str
@@ -36,6 +49,16 @@ class Field(Generic[_T]):
3649
init: bool
3750
compare: bool
3851
metadata: Mapping[str, Any]
52+
def __init__(
53+
self,
54+
default: _T,
55+
default_factory: Callable[[], _T],
56+
init: bool,
57+
repr: bool,
58+
hash: Optional[bool],
59+
compare: bool,
60+
metadata: Mapping[str, Any],
61+
) -> None: ...
3962
if sys.version_info >= (3, 9):
4063
def __class_getitem__(cls, item: Any) -> GenericAlias: ...
4164

@@ -76,8 +99,13 @@ def is_dataclass(obj: Any) -> bool: ...
7699
class FrozenInstanceError(AttributeError): ...
77100

78101
class InitVar(Generic[_T]):
102+
type: Type[_T]
103+
def __init__(self, type: Type[_T]) -> None: ...
79104
if sys.version_info >= (3, 9):
80-
def __class_getitem__(cls, type: Any) -> GenericAlias: ...
105+
@overload
106+
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ...
107+
@overload
108+
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ...
81109

82110
def make_dataclass(
83111
cls_name: str,
@@ -92,4 +120,4 @@ def make_dataclass(
92120
unsafe_hash: bool = ...,
93121
frozen: bool = ...,
94122
) -> type: ...
95-
def replace(obj: _T, **changes: Any) -> _T: ...
123+
def replace(__obj: _T, **changes: Any) -> _T: ...

tests/stubtest_whitelists/py39.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ copy.PyStringMap
3737
dataclasses.Field.__init__
3838
dataclasses.InitVar.__init__
3939
dataclasses.field
40+
dataclasses.InitVar.__class_getitem__ # stubtest bug. doesn't do the right thing with overload + implicit classmethod __class_getitem__
4041
dummy_threading
4142
email.message.MIMEPart.as_string
4243
enum.Enum._generate_next_value_

0 commit comments

Comments
 (0)