Skip to content

match: Incorrect "Incompatible types in capture pattern" when type has already been narrowed #16736

Open
@JelleZijlstra

Description

x: int | None
y: int
match x:
    case None:
        pass
    case y:
        pass
main.py:6: error: Incompatible types in capture pattern (pattern captures type "int | None", variable has type "int")  [misc]

https://mypy-play.net/?mypy=latest&python=3.10&gist=b3423e1eab3afadc8e4d172761d256a2

This should be fine, because the previous case ensured that y is not None.

I ran into this in a real use case.

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions