Type narrowing after use of all() built-in #8665
Replies: 1 comment 1 reply
-
This isn't currently a supported type guard pattern. For a full list of supported type guard patterns, refer to this documentation. I've seen the I recommend using a user-defined type guard (with the from typing import Sequence, TypeIs
def no_nones_or_zeros[T](val: Sequence[T | None]) -> TypeIs[Sequence[T]]:
return all(val)
def func(nums: Sequence[int | None]):
if not no_nones_or_zeros(nums):
raise ValueError
total = 0
for num in nums:
total += num
return total |
Beta Was this translation helpful? Give feedback.
-
I think it would be great if Pyright could detect that a sequence can't contain
None
after a check withall()
:Beta Was this translation helpful? Give feedback.
All reactions