python-language-server icon indicating copy to clipboard operation
python-language-server copied to clipboard

Support @dataclass classes

Open jakebailey opened this issue 6 years ago • 6 comments

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.

jakebailey avatar Nov 15 '18 23:11 jakebailey

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.

munael avatar Feb 07 '19 09:02 munael

https://github.com/Microsoft/vscode-python/issues/3598 discovered that dataclasses currently have their attributes flagged as use-before-def. Do you want that reported as a separate issue?

brettcannon avatar Feb 15 '19 00:02 brettcannon

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).

jakebailey avatar Feb 15 '19 06:02 jakebailey

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?

erezsh avatar Dec 28 '19 01:12 erezsh

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

alphacentauri12 avatar May 19 '20 20:05 alphacentauri12

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

mahmoudajawad avatar May 26 '20 16:05 mahmoudajawad