Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Pylance to ignore the possibility of None?

I love Pylance type checking.

However, If I have a variable var: Union[None, T], where T implements foo, pylance will throw an error at:

var.foo() since type None doesn't implement foo.

Is there any way to resolve this? A way to tell Pylance "This variable is None sometimes but in this case I'm 100% sure it will be assigned

like image 760
George Avatar asked Jul 19 '21 20:07

George


People also ask

Can I disable Pylance?

VS Code users can uninstall or disable the Pylance extension at will and, once it's uninstalled, it won't re-install when the Python extension updates.

What is Pylance in Vscode?

Pylance is an extension for Visual Studio Code . More specifically, Pylance is a Python language server — this means it offers enhancements to IntelliSense, syntax highlighting, package import resolution, and a myriad of other features for an improved development experience in the Python language.

Does pylance support ignore comment for suppressing diagnostics on a particular line?

Yes, Pylance follows PEP 484 and supports the # type: ignore comment for suppressing diagnostics on a particular line. There's almost always a better way to handle the error. Here are some common solutions: Add a missing type annotation. Add an assert (e.g. assert foo is not None if you know that foo cannot be None at that location).

Should I use Pylint or pylance for type checking?

If you want to enforce those types of issues, then pylint is a good choice. Pylance is built on pyright, which is a static type checker, so it follows the type checking standards laid out in PEP 484 and related specifications. PEP 484 specifies that # type: ignore at the end of a line is the way to silence type-related errors.

What does pylance mean when it says something went wrong?

Sorry, something went wrong. @stangier, in this case, Pylance is warning you about a legitimate error in your code that you should fix. If you don't, your code could fail to run in future versions of Python. The Python specification says the following:

What is the use of pylance?

Pylance is an extension that works alongside Python in Visual Studio Code to provide performant language support. Under the hood, Pylance is powered by Pyright, Microsoft's static type checking tool. Using Pyright, Pylance has the ability to supercharge your Python IntelliSense experience with rich type information, ...


1 Answers

There are many ways of forcing a type-checker to accept this.



  1. Use assert:

    from typing import Union
    
    def do_something(var: Union[T, None]):
        assert var is not None
        var.foo()
    

  2. Raise some other exception:

    from typing import Union
    
    def do_something(var: Union[T, None]):
        if var is None:
            raise RuntimeError("NO")
        var.foo()
    

  3. Use an if statement:

    from typing import Union
    
    def do_something(var: Union[T, None]):
        if var is not None:
            var.foo()
    

  4. Use typing.cast, a function that does nothing at runtime but forces a type-checker to accept that a variable is of a certain type:

    from typing import Union, cast
    
    def do_something(var: Union[T, None]):
        var = cast(T, var)
        var.foo()
    

  5. Switch off the type-checker for that line:

    from typing import Union
    
    def do_something(var: Union[T, None]):
        var.foo()  # type: ignore
    


Note also that, while it makes no difference to how your type annotation is interpreted by a type-checker (the two are semantically identical), you can also write typing.Union[T, None] as typing.Optional[T], which is arguably slightly nicer syntax. In Python >=3.10 (or earlier if you have from __future__ import annotations at the top of your code), you can even write Union types with the | operator, i.e. T | None.

like image 103
Alex Waygood Avatar answered Oct 26 '22 23:10

Alex Waygood