Skip to content

PEP 742 (TypeIs) tracker #11453

Closed
Closed

Description

@JelleZijlstra

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.

Activity

  1. sterliakov commented on Apr 27, 2024

    @sterliakov

    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).

  2. JelleZijlstra commented on Apr 27, 2024

    @JelleZijlstra
    MemberAuthor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    project: feature trackerTracks whether a typing feature can be used in typeshed stubs

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions