Skip to content

Docs: walrus TypeGuard #11075

Closed
Closed
@mcmasn-amzn

Description

@mcmasn-amzn

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.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions