Skip to content

Docs: walrus TypeGuard #11075

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
mcmasn-amzn opened this issue Sep 8, 2021 · 2 comments · Fixed by #11161
Closed

Docs: walrus TypeGuard #11075

mcmasn-amzn opened this issue Sep 8, 2021 · 2 comments · Fixed by #11161
Assignees

Comments

@mcmasn-amzn
Copy link

Documentation

In #9865, mypy added support for this pattern.

from typing_extensions import TypeGuard
def is_float(a: object) -> TypeGuard[float]: pass
def main(a: object) -> None:
    if is_float(x := a):
        reveal_type(x)  # N: Revealed type is 'builtins.float'
        reveal_type(a)  # N: Revealed type is 'builtins.object

I couldn't find examples of this in https://www.python.org/dev/peps/pep-0647/ or in the docs in #10758.

Question

Is this a unique behavior to mypy? Is it something that is expected of all type checkers that implement type guards?

Assuming this wasn't an experimental behavior that will be dropped, it would be helpful to document this usage pattern.

@tmke8
Copy link
Contributor

tmke8 commented Sep 14, 2021

I can confirm it works in pyright as well.

@sobolevn
Copy link
Member

sobolevn commented Sep 17, 2021

I will add this after #11088! Thanks a lot!

(I cannot not assign myself in the UI, but count that as an assignment) 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants