Skip to content

match narrowing fails when the operand is an await expression #17230

Open
coderabbit-test/mypy
#4
@tamird

Description

@tamird

This is related to #12998.

https://mypy-play.net/?mypy=latest&python=3.12&gist=42bbd2ec432dbfd003429c64b8dc7435

from typing import assert_never

async def fn() -> int | None:
    return None

async def foo() -> None:
    match await fn(): 
        case int():
            pass
        case None:
            pass
        case never:
            assert_never(never) # error: Argument 1 to "assert_never" has incompatible type "int | None"; expected "NoReturn"  [arg-type]
            
async def bar() -> None:
    a = fn()
    match await a: 
        case int():
            pass
        case None:
            pass
        case never:
            assert_never(never) # error: Argument 1 to "assert_never" has incompatible type "int | None"; expected "NoReturn"  [arg-type]
            
async def baz() -> None:
    match _a := await fn(): 
        case int():
            pass
        case None:
            pass
        case never:
            assert_never(never) # works!

I also sent #17199 with a test case that demonstrates the bug.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-match-statementPython 3.10's match statement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions