pyright doesn't understand collection is not None (hence iterable) after condition check
jabbera opened this issue · comments
Describe the bug
pyright doesn't understand collection is not None (hence iterable) after condition check
Code or Screenshots
def demo(x: list[int] | list[str] | None):
val = x[0] if x else None
if val is None:
return
return [item for item in x]
Yields the error:
Object of type "None" cannot be used as iterable value on: return [item for item in x]
VS Code extension or command-line
Command line: pyright 1.1.362
Pyright is working as designed here, so I don't consider this a bug.
Your code isn't using a supported type guard check for expression x
. For a complete list of type guard patterns supported by pyright, refer to this documentation.
You can fix this in your code by switching to the check if x is None
.
Incidentally, mypy generates the same type error as pyright in this code sample.