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
1
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
.
7
Please don’t use blanket # type: ignore
. Instead be specific on what linting error you want to ignore:
myOptionalVar.foo() # pyright: ignore[reportOptionalMemberAccess]
Above works in VSCode default linter Pylance which uses Pyright.
1
from /a/71523301/54745
-
In a class, use a private member and a property:
class something: def __init__(self): self._var = None @property def var(self) -> T: assert self._var is not None return self._var def do_something(self) self.var.foo()
0