python-attrs / attrs

Python Classes Without Boilerplate
https://www.attrs.org/
MIT License
5.3k stars 374 forks source link

Private field is not understood by VScode. #1239

Closed pinkfrog9 closed 9 months ago

pinkfrog9 commented 9 months ago

Given the following code,

@define
class X:
  _foo:int

The following initialization code will run without error,


x = X(foo=3)
```.

However, Vscode does not understood `foo` is valid in `X(foo=3)`. It will auto complete with `X(_foo)` instead.
hynek commented 9 months ago

Unfortunately, we have no influence on what VS Code / Pylance / Microsoft do. You might have some luck by aliasing the field name explicitly using the alias argument: https://www.attrs.org/en/stable/init.html#private-attributes-and-aliases

pinkfrog9 commented 9 months ago

yes, using "alias" worked. thanks.

pinkfrog9 commented 9 months ago

I am curious since instructing with an "alias" field works.

Can we do some control on the "@define" alone, so that the pylance can understand the transformation of private fields (removing the prefixing _)?

hynek commented 9 months ago

Sadly, no. This is something that has to implemented on the Pyright/Pylance side and generally speaking, they only want to implement whatever is officially defined for dataclass transforms. Which is true for alias: https://peps.python.org/pep-0681/#field-specifier-parameters