-
For the following method, I am not clear on the circumstances under which the list can be
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
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 Mypy only recently added support for unbound variable detection, and it still contains a number of holes. For example, mypy doesn't report an error in this case, which results in a runtime exception: def func(l: list[int]) -> int:
for i in l:
break
return i
func([]) |
Beta Was this translation helpful? Give feedback.
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 thereportUnboundVariable
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, …