Skip to content

Dataclasses: support mutable properties - #6644

Closed
ckanesan wants to merge 4 commits into
python:masterfrom
ckanesan:dataclasses-mutable-property
Closed

ckanesan wants to merge 4 commits into
python:masterfrom
ckanesan:dataclasses-mutable-property

Conversation

@ckanesan

@ckanesan ckanesan commented Apr 8, 2019

Copy link
Copy Markdown

Skip over attributes that are redefined as properties on dataclass construction.

As far as I understand, not redefining a symbol within the same scope is a fundamental assumption in mypy. However, I have not been able to figure out another way to get a mutable property that is also part of the dataclasses.fields. It seems that we are falling within the cracks in the sense that the property methods actually mask the attribute with the desired functionality and all seems to go well in the end.

Any indications on how to make a cleaner implementation would be most welcome!

Fixes #6493

Skip over attributes that are redefined as properties on dataclass construction

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! I have one suggestion.

The failure in CI seems to be caused by #6432, so that should be fixed first.

assert isinstance(node, (Var, OverloadedFuncDef))

if isinstance(node, OverloadedFuncDef):
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is still dangerous, what if node is a Decorator? I would just replace the old assert with if not isinstance(node, Var): continue.

@ckanesan

Copy link
Copy Markdown
Author

@ilevkivskyi Thanks for the suggestion. I think you are right but I can't seem to wrap my head around the possible edge cases.

@ilevkivskyi

Copy link
Copy Markdown
Member

Thanks for update! Unfortunately, we will need to wait with this PR until #6432 is fixed.

@ilevkivskyi

Copy link
Copy Markdown
Member

This PR may be unblocked now. Could you please rebase on current master?

Also it looks like a fix identical to the one you add may also fix #6715 would you like to add it to this PR? Thank you!

@ckanesan

Copy link
Copy Markdown
Author

@ilevkivskyi Sorry for the delay, I was absent for a while due to personal obligations. Is this still okay to merge?

@ilevkivskyi

Copy link
Copy Markdown
Member

@ckanesan No problem. Please note there is still a (legitimate) failure on the new semantic analyzer. So you should probably update the test to only run on new analyzer, or put a type ignore on that line.

@ilevkivskyi

Copy link
Copy Markdown
Member

Also as I mentioned there is almost identical issue for attrs #6715. It would be great if you can fix it too.

@97littleleaf11

Copy link
Copy Markdown
Collaborator

Jukka closed the original issue, #6493 (comment)

This no longer crashes, so I'm closing this issue. Mypy still doesn't support dataclasses with properties, however -- feel free to create a separate issue about this feature.

I am going to close this PR since it's inactive. Feel free to open a new one at your convenience!

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.

Internal error on dataclasses with properties

4 participants