microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.7k stars 768 forks source link

Is there a setting to turn off specific errors displayed by Pylance? #929

Closed Jill-Cheng closed 3 years ago

Jill-Cheng commented 3 years ago

Is there a setting to turn off specific errors displayed by Pylance?

When we use the Pylance language service, sometimes it will display some errors or warnings in the code. Is it possible to turn off the specific errors displayed like pylint?

For example, for "reportUndefinedVariable", is it possible to selectively pick out variables to be excluded from the inspection?

jakebailey commented 3 years ago

There is no way to disable it for a region, only in the user settings, for the workspace, for a specific file, or to # type: ignore a specific line.

is there a specific piece of code you believe should not have this diagnostic?

Jill-Cheng commented 3 years ago

for example:

# Make pylint think that it knows about additional builtins
data = data  # pylint:disable=invalid-name,used-before-assignment,undefined-variable
DEBUG = DEBUG # pylint:disable=invalid-name,used-before-assignment,undefined-variable
VERBOSE = VERBOSE # pylint:disable=invalid-name,used-before-assignment,undefined-variable
jakebailey commented 3 years ago

At the moment, the analogous option is to # type: ignore the line.

The main reason I'm asking is that undefined variables indicate something unique in the code, and that's the bit I'd like to better understand (e.g, why you need it to be ignored).

erictraut commented 3 years ago

How is that working at runtime without generating exceptions? Is there code that's dynamically "injecting" those symbols into your module's scope? If so, that's pretty hacky. I recommend looking for other approaches.

Another option, in addition to what Jake said, is to use the TYPE_CHECKING symbol in the typing module to conditionally define variables to satisfy the static type checker.

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    # Assign values to injected values to satisfy static type checking
    data = {}
    DEBUG = True
    VERBOSE = True
Jill-Cheng commented 3 years ago

Thanks.

For example: The module has been installed in the environment in which it is used, and the code can run successfully, but it shows Import "pandas" could not be resolved from source. How to turn off this message without ignoring other messages displayed because the module is not installed?

image

image

jakebailey commented 3 years ago

Do you have that interpreter selected at the bottom left? If so, then I'd be interested in what's going on there since we should be finding it. It's saying "can't find source" since we ship stubs for pandas with Pylance (which means we can offer completion even if it's not installed).

Jill-Cheng commented 3 years ago

yes,

image

When I reloaded VS Code, no information is displayed here. Thank you very much for your response, but I want to know if there is a setting to turn off specific Pylance information.

jakebailey commented 3 years ago

You can:

But I am still curious as to why these are not resolving; if you could provide some trace logs that would be most helpful. https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue

cpwood commented 3 years ago

Here's a scenario where I'd like to be able to turn off all linting for a method (as opposed to line or file):

import rp2

@rp2.asm_pio(sideset_init=rp2.PIO.OUT_LOW, out_shiftdir=rp2.PIO.SHIFT_LEFT, autopull=True, pull_thresh=24)
def ws2812():
    T1 = 2
    T2 = 5
    T3 = 3
    wrap_target()
    label("bitloop")
    out(x, 1)               .side(0)    [T3 - 1]
    jmp(not_x, "do_zero")   .side(1)    [T1 - 1]
    jmp("bitloop")          .side(1)    [T2 - 1]
    label("do_zero")
    nop()                   .side(0)    [T2 - 1]
    wrap()

It's obviously not "standard" Python - it's code written in assembly language for the Raspberry Pi Pico and run under MicroPython.

As you can imagine, makes linting very unhappy and it would be easier to just turn it off for that method.

Alternatively, a means of turning it off for a method decorated with a particular attribute would be handy (e.g. @rp2.asm_pio in this case).

jakebailey commented 3 years ago

There's no way to turn off linting for a method. (Maybe if we implemented that no_type_check decorator thing.)

What about the above code makes linting unhappy? Do you have an example of the messages you're seeing? I wouldn't expect that you'd want type checking in this file if it's not supposed to be type checked, in which case you could stick a # type: ignore on the first line to hide messages for the entire file, or use a pyrightconfig.json to exclude the files you intend to not type check.

cpwood commented 3 years ago

Mostly reportUndefinedVariable because it can't resolve pull, mov, etc. Like I say, it's assembly language rather than Python. Somebody thought it would be awesome to be able to write assembly within a Python method (and it wasn't me).

image

Putting the # type: ignore comment back in properly works, though I may look at pyrightconfig.json to make it exclude *_asm.py files as a convention.

That said, most code samples for MicoPython include the "normal" Python and the assembly methods in the one .py file, so I'd still prefer a means to exclude a method.

Appreciate your input, @jakebailey . 👍

jakebailey commented 3 years ago

Note that you can do something like:

# pyright: reportUndefinedVariable=false

At the top of the files too. But likely ignore will do what you expect at the cost of having no diagnostics at all. https://github.com/microsoft/pyright/blob/master/docs/configuration.md

Jill-Cheng commented 3 years ago

Thanks a lot!

noah-built commented 3 years ago

I also have a custom addition to __builtins__ and it'd be quite convenient to be able to disable reportUndefinedVariable for only a particular token/variable name!

(Note: Landed here from https://stackoverflow.com/questions/64649795/is-it-possible-to-set-lint-custom-settings-and-ignores-for-pylance)

erictraut commented 3 years ago

You are dynamically injecting new symbols into builtins? What is the problem you were trying to solve with this approach? It sounds very fragile. Importing symbols by name is much better because dependencies are statically understood.

One option is to use typing.TYPE_CHECKING to conditionally define the variable within the global scope of the module where it's used:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    builtins_interloper: str = ""
noah-built commented 3 years ago

Yep agreed, I'm not so sure it's a good idea haha... But here is the inspiration: https://github.com/samuelcolvin/python-devtools

In particular we need to edit sitecustomize.py to log uncaught exceptions.

jakebailey commented 3 years ago

The original issue here reported by @Jill-Cheng was that 1) some variables were marked as undefined (declared magically by whatever runs the file) and 2) that some imports weren't resolving.

For the first case, you can use if TYPE_CHECKING to declare their types or a # type: ignore to handle this, or just disable the diagnostic for the file/project. The config could also just ignore files of certain types (which does at least handle the *_asm.py case).

For the second, I was still looking for logs and other info so we could figure out why the import wasn't resolving (imports should resolve if they are in the activated interpreter).

Since there's been no updates in over a month, I'm going to close this; we can reopen this if more info can be provided for the missing import case, or if these solutions for "adding more builtins" are too much of a problem; perhaps the "real" solution here is some mechanism to extend the builtins, but I feel like the TYPE_CHECKING method, or potentially even a dummy stub that's * imported with the extra builtins is a more reasonable option.

burcaka commented 3 years ago

I am having the same problem:

pylance_vscode_error

The code causing the glitch may be located at:

https://github.com/snoack/python-goto/tree/py38

The author himself states that:

"Note that label .begin and goto .begin is regular Python syntax to retrieve the attribute begin from the objects with the variable names label and goto. However, in the example above these variables aren't defined. So this code would usually cause a NameError. But since it's valid syntax the function can be parsed, and results in following..."

Please note that my program runs just fine and pip list displays goto-statement as a valid, installed package.

I am aware of the suggested solutions and will probably go with the # type: ignore way. Just wanted to offer my two cents for a possible, future fix.

jakebailey commented 3 years ago

@burcaka See https://github.com/microsoft/pylance-release/issues/1269#issuecomment-839930862. I do not see us ever supporting goto.

erictraut commented 3 years ago

The goto-statement library modifies the compiled byte code, effectively changing the language to something other than Python. Pylance works with the standard Python language, and we don't have any plans to support non-standard variants.

burcaka commented 3 years ago

Fair enough... I thought it was a good example for the comment provided by @jakebailey:

"1) some variables were marked as undefined (declared magically by whatever runs the file)..."

dmbaker commented 2 years ago

None of the solutions have worked for me. Here is my code:

image

image

image

jakebailey commented 2 years ago

Those are all pylint errors, not Pylance. You'd need to configure pylint to get rid of those, or disable it.

The only message there from Pylance is the unreachable one, which is only explaining why the code has been grayed out (and can't be removed).

benjaminsweetnam247 commented 2 years ago

have had an issue with the the object being assigned in a try catch block being assigned Unknown by pylance and then throwing an error when trying to call a member of that object. for example using boto3 in a get_client function in a try catch block using that returned client throws an error "add_tags_to_resource" is not a known member of "None" Pylance(reportOptionalMemberAccess) now obviously the code works fine

erictraut commented 2 years ago

@benjaminsweetnam247, it sounds like your code has a bug where there is some code path where a variable might contain None under some conditions. If you would like to open a new issue or start a new discussion thread and post a minimal self-contained code sample, we can help you diagnose further.

anneadb commented 2 years ago

Hi all, I landed here because I am using display in a .py file using py-percent cells to use in the interactive window of VS Code. Is there a way to globally define for all my projects that this function should not raise reportUndefinedVariable?

For flake8 i can do this in the setup.cfg:

[flake8]
builtins = display
debonte commented 2 years ago

@anneadb, please open a new issue.

Mark2Mark commented 1 year ago

I’d also love to be able to ignore certain keywords more class names. I develop plugins for a hosting app, which provides a lot of built-in objects, that Pylance obviously can’t know about. A setting where one could just add the words to be ignored would be chef kiss.

I think this use case has nothing to do with what was proposed solutions as stated here in the closing comment. It is not an issue with failing imports or missing builtins.

Thanks so much for your time!

josephw-ml commented 1 year ago

To disable inline, # pyright: reportUndefinedVariable=false may help?

erictraut commented 1 year ago

@Mark2Mark, rather than ignoring these errors, I recommend using this mechanism to extend builtins. Not only will that eliminate the "undefined symbol" errors, but you'll also get auto-completion and type checking for these symbols.

Mark2Mark commented 1 year ago

thanks @erictraut. Yeah, I implemented a stubs file in the mean time now. That works well.

lxRbckl commented 1 year ago

Using VSCode, I solved this by installing the extension Python: Environments. You're then able to go into the environments you have, hover over the one you're using for the project, and select 'Set as active workspace interpreter'. After I selected this, all "missing dependency" issues went away, visually.

simkimsia commented 11 months ago

@lxRbckl what if i am using docker env?