Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Support @dataclass classes #399

Open
jakebailey opened this issue Nov 15, 2018 · 6 comments
Open

Support @dataclass classes #399

jakebailey opened this issue Nov 15, 2018 · 6 comments
Labels
enhancement New feature or request

Comments

@jakebailey
Copy link
Member

jakebailey commented Nov 15, 2018

Classes decorated as @dataclass get a slew of methods added to them, which we currently cannot detect: https://docs.python.org/3/library/dataclasses.html

For example, if I declared a Point class as a dataclass, I'd expect to see some extra info when trying to instantiate one:

image

It should look the same as:

image

(Where I've explicitly defined __init__ as the Python docs do.)

Unfortunately, it's not as simple as "see @dataclass, add these methods", since the decorator allows for customizing which methods get added. I expect this will need to operate a lot like namedtuple and need to look at the call to determine what to add. Also note the rules like "if __init__ exists, it's not overridden" which will need to be considered.

@MikhailArkhipov MikhailArkhipov added the enhancement New feature or request label Nov 23, 2018
@munael
Copy link

munael commented Feb 7, 2019

One point for completely 'vanilla' dataclasses:

Initialized variables of a dataclass type offer correct completions (usually even better sorted than completions for a manually-created class with the same members, which can show several __blah members before normal members), but type hints don't always show when hovering over a member name (but a member is consistent; it either shows the type or doesn't).

Like this:
image

Here^ I was hovering over dirs in app.dirs. app's type is correctly inferred and shows on hover, its members show in autocomplete, but members' types don't show on hover.

@brettcannon
Copy link
Member

microsoft/vscode-python#3598 discovered that dataclasses currently have their attributes flagged as use-before-def. Do you want that reported as a separate issue?

@jakebailey
Copy link
Member Author

No, that's probably #395. The new LS hasn't reimplemented that diagnostic yet, either way, so I'd be hoping to implement it without these bugs (in a less hacky way).

@erezsh
Copy link

erezsh commented Dec 28, 2019

Hi, any idea if there's any progress on this?

jedi (in vscode) has pretty good support for it (dataclass autocomplete). Perhaps you can look at their solution for inspiration?

@alphacentauri12
Copy link

alphacentauri12 commented May 19, 2020

@jakebailey I also had this problem and I think I might have solved it by adding a docstring. If it is still not working delete the folder __pycache__.

@mahmoudajawad
Copy link

@alphacentauri12, what do you add to the docstring? full definition of the args used?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants