34

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

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
George
  • 3,521
  • 4
  • 30
  • 75

3 Answers3

51

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.

Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
  • @George I rolled back your edit as `Optional[T]` is semantically identical to `Union[T, None]`. While I agree that it is nicer syntax, it *should* make no difference to how the annotation is interpreted by a type-checker. – Alex Waygood Oct 06 '21 at 14:31
  • 1
    Do you mind letting it in? I get that it's the same but I think the syntax is nicer, it's what I ended up using in my codebase. I guess the question is not so popular anyway, so up to you in the end. – George Oct 07 '21 at 13:16
  • @George does pylance not complain if you use `var.foo()` after annotating `var` as being of type `Optional[T]`? Mypy sure does, so it's not a solution that's universal for all type-checkers :/ E.g. https://mypy-play.net/?mypy=latest&python=3.10&flags=show-error-codes%2Cstrict&gist=66569c9e53af328165f9259591bedf1f – Alex Waygood Oct 07 '21 at 13:22
  • I've added a paragraph at the bottom regarding syntax options :) – Alex Waygood Oct 07 '21 at 13:22
  • 1
    @George I've just tested the linked snippet here on Pylance, and it fails on Pylance even with type-checking set to "basic" rather than "strict" (as it should). So I don't see how writing it as `Optional[T]` is a solution to the problem you posed in the question :) https://gist.github.com/AlexWaygood/f9ae35eb43418a2e674826db516dc654 – Alex Waygood Oct 08 '21 at 15:48
  • 2
    I was looking for an answer like this. It's neat to see different options on forcing the type checker. Kind of wish this was in the docs for [mypy errors about None/Optional types](https://mypy.readthedocs.io/en/stable/common_issues.html#unexpected-errors-about-none-and-or-optional-types) – Ehtesh Choudhury Mar 18 '22 at 09:05
  • 1
    Hmm. I can maybe submit a docs PR to mypy. No promises about when though. (@EhteshChoudhury feel free to submit your own PR to mypy! Some form of credit in the PR description would be nice, if you do want to do so :) I'm @AlexWaygood on github.) – Alex Waygood Mar 18 '22 at 11:18
6

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.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
-3

from https://stackoverflow.com/a/71523301/54745

  1. 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()
    
Jason Harrison
  • 922
  • 9
  • 27