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
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.
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.
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).
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.
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:
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, ...
There are many ways of forcing a type-checker to accept this.
Use assert
:
from typing import Union
def do_something(var: Union[T, None]):
assert var is not None
var.foo()
Raise some other exception:
from typing import Union
def do_something(var: Union[T, None]):
if var is None:
raise RuntimeError("NO")
var.foo()
Use an if
statement:
from typing import Union
def do_something(var: Union[T, None]):
if var is not None:
var.foo()
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()
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With