-
Notifications
You must be signed in to change notification settings - Fork 767
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
Don't show "not accessed" / graying out for "required" method parameters #194
Comments
Variables that are not referenced are colored in gray as a subtle hint that they haven't been accessed. This can sometimes indicate a bug. If you intended for this symbol to be unaccessed, you can ignore the subtle clue. This behavior is consistent with other language servers. If you find the grayed-out text annoying, you can adjust your color theme settings to display the same color as normal text (in your case, white). |
This would be a behavior change, but I think it may be an improvement to not gray out "required" parameters (e.g. self, cls, things needed to properly overload / implement interfaces), as they aren't helpful in those cases. |
@jakebailey I agree. Came here to suggest exactly that. |
There is also the case where self is accessed indirectly via super. Those are still diagnosed as not accessed. |
It would be nice if we could mark variables, say by naming them with a leading underscore, to indicate that having no accesses to them is OK. |
Pylance never emits an error for an unreferenced parameter. By default, it also doesn't emit an error for an unreferenced variable, although you can enable the |
Thanks for your answer. You are absolutely right that I understand that graying out variables is mostly meant as a subtle cue rather than a real warning. However, unused variables are so often a symptom of real errors that gray variables name really become nagging. |
Prefixing with Apart from that, prefixing In itself, this rule (x is not accessed) is very helpful in all other cases so I'd rather not disable it entirely. EDIT: i read that wrong, you meant replacing the entire variable name with |
Pylance does not emit a diagnostic message for this condition. It simply grays out the text which is a subtle visual queue that the variable is not accessed. Are you seeing an actual diagnostic message (like an error or warning) somewhere? |
I agree with this, whereas the OT was removing the greying out altogether. I find the greying out useful in the general case, but with "required"/conventional arguments I find it an annoying distraction. That is, in this snippet:
I'd expect |
In my case (using coc.nvim) it's a bit more distracting as the color scheme doesn't just gray things out but also puts a marker on every line and underlines it. You could argue that's my problem and nvim should highlight things like this more subtly, but that's not the point. Like @pzelnip says, it's highlighting stuff as "possibly shouldn't be here", whereas it absolutely should be there to conform with how Python does (class)methods. |
@jakebailey Just in case you weren't thinking of ABC when you wrote this (although I'm guessing you were), Pylance should also not gray out any variables in signatures of abstract methods: |
Is there any solution on this? |
@ProgrammingLife Not yet. Feel free to upvote the original issue to let the Pylance team know it's important to you. |
The next release changes the behavior such that the following cases are no longer marked as "not accessed":
I think this should cover all of the cases where either it's "required" (makes Python mad if you omit it) or is a part of defining a contract (where the parameters are a part of that definition). Thanks for the feedback. |
This issue has been fixed in version 2021.4.2, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md#202142-21-april-2021 |
Thanks for fixing this! |
Nice to see this being adressed in the latest release, thanks! |
I'm having similar issues with a lot of my annotations (@). Is there a issue already opened to that? |
If the behavior differs from what I defined above, then a new issue would be appreciated. |
I might be missing on something here, but what about an overriding method in a derived class(that overrides a method in a base class) where not all the parameters are used? Also if I'm passing a lambda/function as an argument to another function where the passed lambda/function is going to be invoked with a certain signature(number and types of arguments) but the lambda/function passed as an argument doesn't make use of all parameters? |
If a parameter is not used in the lambda/function, it will be displayed in gray. This isn't an error condition. It's a subtle visual cue that the symbol is not accessed. Sometimes a symbol that is not accessed can be indicative of a bug, but in other cases it's a legitimate condition. This visual cue gives you the ability to detect cases where you expect a symbol to be accessed but it is not. |
And after I check and determine that not referencing the parameter isn't an inadvertent bug the cue will remain, so someone else viewing the code(or even myself after a while) will see the cue and check again. There should be a way to turn off the cue so that it doesn't always prompt the reader to check the parameter usage. |
|
The parameters in your example are not referenced in the method, so they are displayed accordingly. There are plenty of examples of where it's fine for a parameter to go unreferenced. That's true for dunder methods as well as non-dunder methods. Pylance therefore never emits an error for unused parameters. It simply causes them to be displayed as "grayed out" as a subtle reminder that they are unreferenced. |
Makes sense, but I have the same issue as jaapz (won't @ them since it was 2 years ago) where the LSP puts a warning marker at the end of the line, and the diagnostics list has unnecessary stuff you just gotta skip past and ignore. Is there a way to turn off the diagnostic in configs, specifically for dunder methods? |
@Lamby777, are you using VS Code? I'm guessing that you're using some other editor that is misinterpreting these "unused code" hints as diagnostics rather than indications that the text should be displayed as "grayed out". If that's the case, you should raise the issue with the maintainer of your editor. Or switch to VS Code, which doesn't have this problem. |
Why does Pylance grey-out the "self" parameter when i don't use it in the method and shows this when I hover over it:
The text was updated successfully, but these errors were encountered: