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

[dataclass_transform] minimal implementation of dataclass_transform #14523

Merged

Conversation

wesleywright
Copy link
Collaborator

This is a very simple first step to implementing PEP 0681, which will allow MyPy to recognize user-defined types that behave similarly to dataclasses.

This initial implementation is very limited: we only support decorator-style use of typing.dataclass_transform and do not support passing additional options to the transform (such as freeze or init).

Within MyPy, we add a new is_dataclass_transform field to FuncBase which is populated during semantic analysis. When we check for plugin hooks later, we add new special cases to use the existing dataclasses plugin if a class decorator is marked with is_dataclass_transform. Ideally we would use a proper plugin API; the hacky special case here can be replaced in subsequent iterations.

… dataclasses plugin

This is a very simple first step to implementing [PEP
0681](https://peps.python.org/pep-0681/#decorator-function-example),
which will allow MyPy to recognize user-defined types that behave
similarly to dataclasses.

This initial implementation is very limited: we only support
decorator-style use of `typing.dataclass_transform` and do not support
passing additional options to the transform (such as `freeze` or
`init`).

Within MyPy, we add a new `is_dataclass_transform` field to
`FuncBase` which is populated during semantic analysis. When we check
for plugin hooks later, we add new special cases to use the dataclasses
plugin if a class decorator is marked with `is_dataclass_transform`.
Ideally we would use a proper plugin API; the hacky special case here
can be replaced in subsequent iterations.
@@ -19,7 +19,7 @@
Var,
)
from mypy.plugin import CheckerPluginInterface, ClassDefContext, SemanticAnalyzerPluginInterface
from mypy.semanal import ALLOW_INCOMPATIBLE_OVERRIDE, set_callable_name
from mypy.semanal_shared import ALLOW_INCOMPATIBLE_OVERRIDE, set_callable_name
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this move was needed to avoid a cyclic dependency between semanal.py and plugins/common.py

@tmke8
Copy link
Contributor

tmke8 commented Jan 25, 2023

I see only typing.dataclass_transform in the code; does this also work with typing_extensions.dataclass_transform?

@github-actions

This comment has been minimized.

@wesleywright
Copy link
Collaborator Author

I see only typing.dataclass_transform in the code; does this also work with typing_extensions.dataclass_transform?

I think this is a gap currently, thanks for the catch (this is my first PR adding this kind of feature haha)

Comment on lines +40 to +44
class list(Generic[_T], Sequence[_T]):
def __contains__(self, item: object) -> int: pass
def __getitem__(self, key: int) -> _T: pass
def __iter__(self) -> Iterator[_T]: pass

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

needed to use dataclasses.pyi and typing-full.pyi at the same time in a test case

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! This is solid basic implementation. You'll need to fix flake8, otherwise everything looks good.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja mentioned this pull request Jan 26, 2023
17 tasks
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great. Let's get this into 1.0. It's not complete, but it will result in strictly fewer errors for users of PEP 681 than current master.

@hauntsaninja hauntsaninja merged commit bac9e77 into python:master Jan 26, 2023
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this pull request Jan 26, 2023
…ython#14523)

This is a very simple first step to implementing [PEP
0681](https://peps.python.org/pep-0681/#decorator-function-example),
which will allow MyPy to recognize user-defined types that behave
similarly to dataclasses.

This initial implementation is very limited: we only support
decorator-style use of `typing.dataclass_transform` and do not support
passing additional options to the transform (such as `freeze` or
`init`).

Within MyPy, we add a new `is_dataclass_transform` field to `FuncBase`
which is populated during semantic analysis. When we check for plugin
hooks later, we add new special cases to use the existing dataclasses
plugin if a class decorator is marked with `is_dataclass_transform`.
Ideally we would use a proper plugin API; the hacky special case here
can be replaced in subsequent iterations.

Co-authored-by: Wesley Wright <wesleyw@dropbox.com>
hauntsaninja added a commit that referenced this pull request Jan 29, 2023
…#14532)

This is a very simple first step to implementing [PEP
0681](https://peps.python.org/pep-0681/#decorator-function-example),
which will allow MyPy to recognize user-defined types that behave
similarly to dataclasses.

This initial implementation is very limited: we only support
decorator-style use of `typing.dataclass_transform` and do not support
passing additional options to the transform (such as `freeze` or
`init`).

Within MyPy, we add a new `is_dataclass_transform` field to `FuncBase`
which is populated during semantic analysis. When we check for plugin
hooks later, we add new special cases to use the existing dataclasses
plugin if a class decorator is marked with `is_dataclass_transform`.
Ideally we would use a proper plugin API; the hacky special case here
can be replaced in subsequent iterations.

Co-authored-by: Wesley Collin Wright <deinlebenandern@gmail.com>
Co-authored-by: Wesley Wright <wesleyw@dropbox.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants