This wasn't possible with TypeGuard due to undesired type narrowing (iterable type would be lost, Sequence[int | None] would become Iterable[int]), but with TypeIs intersection semantics that problem should not arise (Sequence[int| None] & Iterable[int] should be reducible to Sequence[int], if I'm not mistaken, which is the desirable outcome).
Activity
sterliakov commented on Apr 27, 2024
Following #11823, should all also become a TypeIs guard against None elements?
This wasn't possible with TypeGuard due to undesired type narrowing (iterable type would be lost, Sequence[int | None] would become Iterable[int]), but with TypeIs intersection semantics that problem should not arise (Sequence[int| None] & Iterable[int] should be reducible to Sequence[int], if I'm not mistaken, which is the desirable outcome).
JelleZijlstra commented on Apr 27, 2024
Intuitively that seems fine but both mypy and pyright have some issues with this (e.g., they seem to reduce list[str | None] & Iterable[str] to Never): https://pyright-play.net/?pyrightVersion=1.1.359&pythonVersion=3.13&code=GYJw9gtgBALgngBwJYDsDmUkQWEMogCmAboQIYA2A%2BvAoQDRQCSMhIZARhYQLABQoSLESo0VQgA9WKAM5IwszNlz4AKokJMZ-fgBNCwKJQoBtKqoC6ACiSt2XQgC5mdztzOqoAHygA5BYQWjAD0AJRQALQAfFDqdFomLGxuhB4WFs4AdNk6fPqGMmTY3FYSzkn27jIwIN5%2BAUFQcM4USNUm1bU%2B-iiBoY78UEOYhsal-YPDU0SklDQa45NDhBQyTktTBCTk1LSEi3xTSKMUFFZwE4ebQzM783TnoRsrawNX17dze49AA, https://mypy-play.net/?mypy=latest&python=3.12&gist=b4a3be40d6e245ac7e65ff9ffac03355
Worth trying with an exploratory PR, and maybe with tweaks to the intersection logic in mypy and/or pyright.
Anyway, I'm closing this PR as we can use TypeIs in typeshed now. Feel free to open specific issues about places where TypeIs could be used.