Skip to content

dataclasses: various fixes #4937

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
Jan 18, 2021
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
48 changes: 38 additions & 10 deletions stdlib/3.7/dataclasses.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ def asdict(obj: Any, *, dict_factory: Callable[[List[Tuple[str, Any]]], _T]) ->
def astuple(obj: Any) -> Tuple[Any, ...]: ...
@overload
def astuple(obj: Any, *, tuple_factory: Callable[[List[Any]], _T]) -> _T: ...
@overload
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
@overload
def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
@overload
def dataclass(
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
) -> Callable[[Type[_T]], Type[_T]]: ...

if sys.version_info >= (3, 8):
# cls argument is now positional-only
@overload
def dataclass(__cls: Type[_T]) -> Type[_T]: ...
Copy link
Member

Choose a reason for hiding this comment

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

Took me a long time to figure out that the difference between the branches is __cls vs. _cls. Maybe it's worth a comment.

@overload
def dataclass(__cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
@overload
def dataclass(
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
) -> Callable[[Type[_T]], Type[_T]]: ...

else:
@overload
def dataclass(_cls: Type[_T]) -> Type[_T]: ...
@overload
def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
@overload
def dataclass(
*, init: bool = ..., repr: bool = ..., eq: bool = ..., order: bool = ..., unsafe_hash: bool = ..., frozen: bool = ...
) -> Callable[[Type[_T]], Type[_T]]: ...

class Field(Generic[_T]):
name: str
Expand All @@ -36,6 +49,16 @@ class Field(Generic[_T]):
init: bool
compare: bool
metadata: Mapping[str, Any]
def __init__(
self,
default: _T,
default_factory: Callable[[], _T],
init: bool,
repr: bool,
hash: Optional[bool],
compare: bool,
metadata: Mapping[str, Any],
) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, item: Any) -> GenericAlias: ...

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

class InitVar(Generic[_T]):
Copy link
Member

Choose a reason for hiding this comment

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

Why is this Generic? Should type be of type Type[_T]?

type: Type[_T]
def __init__(self, type: Type[_T]) -> None: ...
if sys.version_info >= (3, 9):
def __class_getitem__(cls, type: Any) -> GenericAlias: ...
@overload
def __class_getitem__(cls, type: Type[_T]) -> InitVar[_T]: ...
@overload
def __class_getitem__(cls, type: Any) -> InitVar[Any]: ...

def make_dataclass(
cls_name: str,
Expand All @@ -92,4 +120,4 @@ def make_dataclass(
unsafe_hash: bool = ...,
frozen: bool = ...,
) -> type: ...
def replace(obj: _T, **changes: Any) -> _T: ...
def replace(__obj: _T, **changes: Any) -> _T: ...
1 change: 1 addition & 0 deletions tests/stubtest_whitelists/py39.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ copy.PyStringMap
dataclasses.Field.__init__
dataclasses.InitVar.__init__
dataclasses.field
dataclasses.InitVar.__class_getitem__ # stubtest bug. doesn't do the right thing with overload + implicit classmethod __class_getitem__
dummy_threading
email.message.MIMEPart.as_string
enum.Enum._generate_next_value_
Expand Down