Skip to content

Is if list not considered a type guard? #9244

Answered by erictraut
awgymer asked this question in Q&A
Discussion options

You must be logged in to vote

The static type system currently has no way to specify that an iterable is "empty" — i.e. whether it is guaranteed to provide at least one iteration. Pyright assumes conservatively that any iterable may provide zero iterations. This can produce false positives, but it is easy to fix your code to avoid such errors.

If you don't want pyright to report this diagnostic in your code, you can disable the reportPossiblyUnboundVariable check. It is intentionally separated from the reportUnboundVariable check, which is reserved only for cases where a variable is provably unbound.

Mypy only recently added support for unbound variable detection, and it still contains a number of holes. For example, …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@awgymer
Comment options

@erictraut
Comment options

@awgymer
Comment options

@rayansostenes
Comment options

Answer selected by awgymer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants