-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Add __dataclass_fields__ and __attrs_attrs__ to dataclasses #8578
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
07f8894
Add __attrs_attrs__ attribute to attr.s decorated classes
tkukushkin abfe951
Add __dataclass_fields__ attribute to dataclasses
tkukushkin 4a8688e
Add explicit Any as type argument to dataclasses.Field and attr.Attri…
tkukushkin 17a9403
Fix some tests
tkukushkin 229c568
Fix almost all tests
tkukushkin 426d644
Remove commented code from stub
tkukushkin 0b97db1
Merge branch 'master' into fix-6568
tkukushkin 8dadb8a
Fix narrowing tests
tkukushkin 62b8497
Fix test
tkukushkin 7b26c67
Fix mypy errors
tkukushkin a5bcda1
Fix flake8 errors
tkukushkin e75b83a
Merge branch 'master' into fix-6568
tkukushkin 0932037
Fix tests
tkukushkin 3a66912
Merge branch 'master' of github.com:python/mypy into fix-6568
tkukushkin 2d3a6ca
Generate precise type for `__attrs_attrs__` in attrs plugin
tkukushkin e932c50
Fix self mypy check
tkukushkin be97c26
Fix tests
tkukushkin 528d587
Delete stray file
tkukushkin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,6 +183,8 @@ def transform(self) -> None: | |
|
||
self.reset_init_only_vars(info, attributes) | ||
|
||
self._add_dataclass_fields_magic_attribute() | ||
|
||
info.metadata['dataclass'] = { | ||
'attributes': [attr.serialize() for attr in attributes], | ||
'frozen': decorator_arguments['frozen'], | ||
|
@@ -361,6 +363,21 @@ def _freeze(self, attributes: List[DataclassAttribute]) -> None: | |
var._fullname = info.fullname + '.' + var.name | ||
info.names[var.name] = SymbolTableNode(MDEF, var) | ||
|
||
def _add_dataclass_fields_magic_attribute(self) -> None: | ||
attr_name = '__dataclass_fields__' | ||
dataclass_field_type = self._ctx.api.named_type('dataclasses.Field') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment here. |
||
attr_type = self._ctx.api.named_type('__builtins__.dict', [ | ||
self._ctx.api.named_type('__builtins__.str'), | ||
dataclass_field_type, | ||
]) | ||
var = Var(name=attr_name, type=attr_type) | ||
var.info = self._ctx.cls.info | ||
self._ctx.cls.info.names[attr_name] = SymbolTableNode( | ||
kind=MDEF, | ||
node=var, | ||
plugin_generated=True, | ||
) | ||
|
||
|
||
def dataclass_class_maker_callback(ctx: ClassDefContext) -> None: | ||
"""Hooks into the class typechecking process to add support for dataclasses. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ilevkivskyi
ctx.api.named_type
is for builtin types, but what i should use instead of it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not for builtin types, it is for types that are guaranteed to be present (i.e. loaded).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ilevkivskyi, and how to ensure that
attr.Attribute
is loaded? Or maybe need to useAny
if it is not loaded?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They should be loaded, because the
attr
module must be analyzed if this plugin is called. The tests fail because they run with partial stubs, not the real ones, see for example https://github.com/python/mypy/blob/master/test-data/unit/lib-stub/attr.pyi,Attribute
is not present there.Also the latter is generic, so I would explicitly add a type argument here (even if it is
Any
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a problem with different styles of import of
attr
.This example works fine:
But if I change import to:
I get exception:
Some tests fail because of this.
Yes, I already added
Attribute
to stub, but not pushed yet.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I just looked at the code of
named_type()
and it is horrible, it useslookup_qualified()
(which is just a normal symbol lookup following Python name resolution rules) so I wonder how it ever worked. You can try usingnamed_type_or_none()
instead, that one looks reasonable.Another option is to actually fix
named_type()
, but it may be not the best task as a first contribution.