biqqles / dataclassy

A fast and flexible reimplementation of data classes
https://pypi.org/project/dataclassy
Mozilla Public License 2.0
82 stars 9 forks source link

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

Open hugoake opened 1 month ago

hugoake commented 1 month ago

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 commented 4 weeks ago

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

mara004 commented 3 weeks ago

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 commented 3 weeks ago

like a generic plugin API to tell analyzers where to look

See also https://github.com/microsoft/pyright/issues/607 and https://github.com/davidhalter/jedi/issues/1787 (both rejected, unfortunately :/)

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