Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[dataclass_transform] minimal implementation of dataclass_transform#14523

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

@wesleywright
Copy link
Collaborator

This is a very simple first step to implementingPEP 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 oftyping.dataclass_transform and do not support passing additional options to the transform (such asfreeze orinit).

Within MyPy, we add a newis_dataclass_transform field toFuncBase 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 withis_dataclass_transform. Ideally we would use a proper plugin API; the hacky special case here can be replaced in subsequent iterations.

… dataclasses pluginThis is a very simple first step to implementing [PEP0681](https://peps.python.org/pep-0681/#decorator-function-example),which will allow MyPy to recognize user-defined types that behavesimilarly to dataclasses.This initial implementation is very limited: we only supportdecorator-style use of `typing.dataclass_transform` and do not supportpassing 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 checkfor plugin hooks later, we add new special cases to use the dataclassesplugin if a class decorator is marked with `is_dataclass_transform`.Ideally we would use a proper plugin API; the hacky special case herecan be replaced in subsequent iterations.
)
frommypy.pluginimportCheckerPluginInterface,ClassDefContext,SemanticAnalyzerPluginInterface
frommypy.semanalimportALLOW_INCOMPATIBLE_OVERRIDE,set_callable_name
frommypy.semanal_sharedimportALLOW_INCOMPATIBLE_OVERRIDE,set_callable_name
Copy link
CollaboratorAuthor

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 betweensemanal.py andplugins/common.py

@tmke8
Copy link
Contributor

I see onlytyping.dataclass_transform in the code; does this also work withtyping_extensions.dataclass_transform?

@github-actions

This comment has been minimized.

@wesleywright
Copy link
CollaboratorAuthor

I see onlytyping.dataclass_transform in the code; does this also work withtyping_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
classlist(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
CollaboratorAuthor

Choose a reason for hiding this comment

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

needed to usedataclasses.pyi andtyping-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

@JukkaLJukkaL 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 tomypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninjahauntsaninja mentioned this pull requestJan 26, 2023
17 tasks
Copy link
Collaborator

@hauntsaninjahauntsaninja 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.

NeilGirdhar reacted with hooray emoji
@hauntsaninjahauntsaninja merged commitbac9e77 intopython:masterJan 26, 2023
hauntsaninja pushed a commit to hauntsaninja/mypy that referenced this pull requestJan 26, 2023
…ython#14523)This is a very simple first step to implementing [PEP0681](https://peps.python.org/pep-0681/#decorator-function-example),which will allow MyPy to recognize user-defined types that behavesimilarly to dataclasses.This initial implementation is very limited: we only supportdecorator-style use of `typing.dataclass_transform` and do not supportpassing 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 pluginhooks later, we add new special cases to use the existing dataclassesplugin if a class decorator is marked with `is_dataclass_transform`.Ideally we would use a proper plugin API; the hacky special case herecan be replaced in subsequent iterations.Co-authored-by: Wesley Wright <wesleyw@dropbox.com>
hauntsaninja added a commit that referenced this pull requestJan 29, 2023
…#14532)This is a very simple first step to implementing [PEP0681](https://peps.python.org/pep-0681/#decorator-function-example),which will allow MyPy to recognize user-defined types that behavesimilarly to dataclasses.This initial implementation is very limited: we only supportdecorator-style use of `typing.dataclass_transform` and do not supportpassing 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 pluginhooks later, we add new special cases to use the existing dataclassesplugin if a class decorator is marked with `is_dataclass_transform`.Ideally we would use a proper plugin API; the hacky special case herecan 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@JukkaLJukkaLJukkaL approved these changes

@hauntsaninjahauntsaninjahauntsaninja approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@wesleywright@tmke8@JukkaL@hauntsaninja@JelleZijlstra

[8]ページ先頭

©2009-2025 Movatter.jp