Thanks to visit codestin.com
Credit goes to github.com

Skip to content

PEP 742 (TypeIs) tracker #11453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 of 6 tasks
JelleZijlstra opened this issue Feb 19, 2024 · 2 comments
Closed
4 of 6 tasks

PEP 742 (TypeIs) tracker #11453

JelleZijlstra opened this issue Feb 19, 2024 · 2 comments
Labels
project: feature tracker Tracks whether a typing feature can be used in typeshed stubs

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Feb 19, 2024

This issue is supposed to track when we can start to use the TypeIs special form (PEP 742) in typeshed. Support is needed in released versions of:

Please reference this issue as needed from other issues/PRs to collect places that we might want to update.

Once this is supported, many (most?) TypeGuard functions in typeshed should be changed to use TypeIs.

@JelleZijlstra JelleZijlstra added the project: feature tracker Tracks whether a typing feature can be used in typeshed stubs label Feb 19, 2024
@sterliakov
Copy link

Following #11823, should all also become a TypeIs guard against None elements?

def all(iterable: Iterable[_T | None], /) -> TypeIs[Iterable[_T]]: ...

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
Copy link
Member Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
project: feature tracker Tracks whether a typing feature can be used in typeshed stubs
Projects
None yet
Development

No branches or pull requests

2 participants