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

dataclasses: various fixes #4937

Merged
merged 6 commits into from
Jan 18, 2021
Merged

dataclasses: various fixes #4937

merged 6 commits into from
Jan 18, 2021

Conversation

hauntsaninja
Copy link
Collaborator

No description provided.

Comment on lines 21 to 23
def dataclass(__cls: Type[_T]) -> Type[_T]: ...
@overload
def dataclass(_cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
def dataclass(__cls: None) -> Callable[[Type[_T]], Type[_T]]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

The stubtest complaints are valid. The following is horrible, but possible:

from dataclasses import dataclass

class Foo:
    x: int
    y: str
dataclass(_cls=Foo)

print(Foo(42, ""))

@@ -76,6 +98,8 @@ 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]?


if sys.version_info >= (3, 8):
@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.

@hauntsaninja
Copy link
Collaborator Author

Thanks, addressed (also fixed up __class_getitem__ since it doesn't return GenericAlias)

hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this pull request Jan 17, 2021
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this pull request Jan 17, 2021
__class_getitem__ and __init_subclass__ are always classmethods, even if
they aren't decorated. We previously only handled this correctly if
these methods weren't overloaded.

This came up in python/typeshed#4937
@JelleZijlstra JelleZijlstra merged commit 5df4c3d into python:master Jan 18, 2021
@hauntsaninja hauntsaninja deleted the data branch January 18, 2021 01:10
JelleZijlstra pushed a commit to python/mypy that referenced this pull request Jan 18, 2021
…hod (#9921)

__class_getitem__ and __init_subclass__ are always classmethods, even if
they aren't decorated. We previously only handled this correctly if
these methods weren't overloaded.

This came up in python/typeshed#4937

Co-authored-by: hauntsaninja <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants