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

Support @dataclass classes #399

Description

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.

Activity

  1. munael commented on Feb 7, 2019

    @munael

    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.

  2. brettcannon commented on Feb 15, 2019

    @brettcannon
    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?

  3. jakebailey commented on Feb 15, 2019

    @jakebailey
    MemberAuthor

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

  4. erezsh commented on Dec 28, 2019

    @erezsh

    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?

  5. alphacentauri12 commented on May 19, 2020

    @alphacentauri12

    Jake Bailey (@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__.

  6. mahmoudajawad commented on May 26, 2020

    @mahmoudajawad

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions