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

Support hiding "private" variables/attributes for auto complete #5281

Closed
marsfan opened this issue Sep 11, 2020 · 14 comments
Closed

Support hiding "private" variables/attributes for auto complete #5281

marsfan opened this issue Sep 11, 2020 · 14 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@marsfan
Copy link

marsfan commented Sep 11, 2020

While Python does not technically have private variables, it is a standardized in PEP8 that single and double underscores at the start of a variable name means that the variable is for internal use only. Some Python IDEs support this, and do not display variables with such prefixes outside of their scope. It would be nice if there was a setting in VS code to enable this behavior.

Source: https://www.python.org/dev/peps/pep-0008/#id36

@karthiknadig
Copy link
Member

Can you clarify if this is in completions or debugger or both?

Thank you for the suggestion! We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision.

@luabud luabud self-assigned this Oct 21, 2020
@Mirandatz
Copy link

@karthiknadig

Hi, I am not the original poster, but I believe I could (try to) clarify.
My understanding is that this is related only to completions.

@rost-git
Copy link

rost-git commented Nov 27, 2021

Whould be nice to have something like:
"python.analysis.internalVariables": "visible" | "hidden" | "greyed-out" // visible as default

@marsfan
Copy link
Author

marsfan commented Nov 29, 2021

@karthiknadig Somehow I never saw your message until now (over a year later). @Mirandatz is correct, this would only apply to auto-completion.

@DIGITALCRIMINAL
Copy link

"Protected" key is what you're looking for?
You can add this to your launch.json configuration

"variablePresentation": {
    "class": "group",
    "function": "group",
    "protected": "group",
}

You can also do the following below to group all variable types.

"variablePresentation": {
    "all": "group",
}

image
image

@q-wertz
Copy link

q-wertz commented Dec 29, 2021

Thanks for the hint, works like a charm. Only problem is, that one cannot set this globally…

@luabud luabud removed their assignment Dec 29, 2021
@luabud

This comment has been minimized.

@Mirandatz
Copy link

@DIGITALCRIMINALS

Sadly this (apparently) does not affect Intellisense

image

@luabud luabud changed the title Support hiding "private" variables/attributes Support hiding "private" variables/attributes for auto complete Jan 11, 2022
@luabud
Copy link
Member

luabud commented Jan 11, 2022

Apologies for the confusion, the issues I had liked above was for debugging and not auto completion. I tweaked the title of this issue to be a bit more clear.

@PolarBean
Copy link

Any way to achieve either hiding private methods in intellisense or at least showing them last rather than first?

@debonte
Copy link
Contributor

debonte commented Jan 9, 2024

Any way to achieve either hiding private methods in intellisense or at least showing them last rather than first?

They are currently sorted last. I'm not sure when this changed.

If you'd prefer that they be omitted from the suggestion list, please up-vote this enhancement request: #4258

@debonte debonte closed this as completed Jan 9, 2024
@idan22moral
Copy link

I'm facing a similar problem with private attributes in pydantic:

image
As you can see, I define a model (class, dataclass to be more specific) consisting of public and private fields.
When Pylance hints me with the function's signature, I see the private attributes in the signature too.

But when I got to get the fields auto-completion, I now see public fields only:

image

image

There is one place where this verbose fields list is acceptable, and it is the class-preview block on hover:

image
That's because it shows a preview of what the model actually consists of, and it is not a function parameter list.

@debonte would you say this example is related to the discussed enhancement request? or should I open a new separate issue?

@debonte
Copy link
Contributor

debonte commented Apr 19, 2024

@debonte would you say this example is related to the discussed enhancement request? or should I open a new separate issue?

@idan22moral, please open a separate issue. It's not clear to me that we should ever hide parameters in signature help. You're welcome to argue for your desired behavior in a separate issue though.

There is one place where this verbose fields list is acceptable, and it is the class-preview block on hover:
image

That hover info isn't for the class, but rather for the generated __init__ method. And there again, I don't believe we should be hiding any parameters. The hover info for the Magic class just looks like this:

image

Btw, Pylance's support for pydantic's data classes comes via their use of dataclass_transform where PrivateAttr is not listed as a field specifier. The only field specifier listed is Field.

If you switch from PrivateAttr to Field as shown below, you will get the signature help behavior that you're looking for even if the private field doesn't have a leading underscore. With this change, Pylance isn't hiding the _private_field parameter in signature help. It actually doesn't believe that there is a _private_field parameter on __init__ at all -- that's what init=False is specifying.

from pydantic import BaseModel, Field

class Magic(BaseModel):
    _private_field: str = Field(init=False, default="blabla")
    regular_field: str

Signature help:
image

Hover on __init__ usage:
image

@idan22moral
Copy link

@debonte I've just finished writing my proposal to Pydantic regarding the discussed behavior, and now I see your comment. Funny timing 🤭

After some digging (since Pydantic internals, dataclasses and Pylance+Pyright are new areas for me),
I came to the understanding of exactly what you described. So thank you for the validation.

As you said, the problem is not with Pylance/Pyright, but the field specifiers listed by Pydantic.

Thanks again for your informative comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

10 participants