Skip to content
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

Pylance gives no tooltips when creating instances of a dataclass #63

Open
hugoake opened this issue Aug 14, 2024 · 3 comments
Open

Pylance gives no tooltips when creating instances of a dataclass #63

hugoake opened this issue Aug 14, 2024 · 3 comments

Comments

@hugoake
Copy link

hugoake commented Aug 14, 2024

In VSCode, dataclass from dataclasses allows me to see the attributes of my dataclass when creating an object. For example, if I have a dataclass called MyClass and I write somewhere MyClass( I would get a small box/widget next to my cursor specifying all the attributes and their types. This is extremely useful when I don't remember exactly the names of the attributes.

@hugoake
Copy link
Author

hugoake commented Aug 16, 2024

I learned that it is the Pylance extension that gives rise to this info box.

There seemed to be two reasons for this not working.

  1. The first was that the decorator in decorator.py in turn needs to have another decorator @dataclass_transform() from the typing package. This somehow tells Python/Pylance(?) that this works
  2. The second reason was that the file dataclass.py needs to be renamed to something else. I renamed it to misc.py. For some reason Pylance gets confused otherwise.

I have implemented these changes in a fork: https://github.com/hugoake/dataclassy. I would open a pull request but I am not certain how the @dataclass_tranform() decorator will affect other things, and which arguments to pass to it (for example field_specifier).

@hugoake hugoake changed the title No hover information Pylance gives no tooltips when creating instances of a dataclass Aug 16, 2024
@mara004
Copy link

mara004 commented Aug 18, 2024

I'm currently trying to build an own dataclass-like framework and took a look at dataclass_transform(), too.
Unfortunately it seems extremely inflexible. I think it won't work well with anything except dataclasses itself, or something that effectively duplicates its semantics.
In particular, this does not provide a way to support custom field ordering. AFAICS, all semantics and parameter names are hardcoded, except defaults and field specifiers.

IDEs should really come up with better mechanisms to deal with such transforms, like a generic plugin API to tell analyzers where to look. On the other hand, I suppose that's harder than it sounds...
Nevertheless, it's sad to see how key features, such as more flexible placement of parent fields, are being rejected by certain class creator projects, merely because there isn't currently a way to teach this to IDEs.

@mara004
Copy link

mara004 commented Aug 19, 2024

like a generic plugin API to tell analyzers where to look

See also microsoft/pyright#607 and davidhalter/jedi#1787 (both rejected, unfortunately :/)

Interestingly, MyPy does have a plugin mechanism, but it is only a type checker, and not a language server.

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

No branches or pull requests

2 participants