Skip to content

False positive Case pattern can never match when matching union against protocol #5059

Open

Description

@randolf-scholz

Describe the Bug

When matching a union type against a Protocol, pyrefly incorrectly thinks matching is impossible.
The warning doesn't get triggered when a non-union type is used.

from typing import Protocol, runtime_checkable

@runtime_checkable
class SupportsMethod(Protocol):
    def method(self) -> None: ...


class Foo: ...

def check(arg: Foo) -> None:
    match arg:
        case SupportsMethod():  # OK
            pass

def check_union(arg: Foo | None) -> None:
    match arg:
        case SupportsMethod():  # WARNING: case pattern can never match
            pass
 WARN sandbox.py:17:9-18:17: Case pattern can never match subject of type `Foo | None` [unreachable-match-case]

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZUxD4IwEIX_yiUM6EIHNzYXjTGi0cHRECRAjC2hxd_va0utICEaR0rv3YX73uOHyPSR1rRcVVhVVubZTaeIcefIqTXcqa21gNxZYzgdx9rAM54ZiqJIKzudlRCvM11kGs2Mw_BqyNo9VVlp_OeJRXTmw3HmMVFA-62_5eF463PB4uBJ162z439Nz8tjsknWsb2C1EMUczxwMrlv1cbm-mQvoIM9oG6NSF1AW7TIdsyNbxYg1nMqlaplzJgrF03Bcs6uIpOsV8FI2yx9pJX5Y_TlSBivSMiOQ7_4CvrQMh9OIR9q4nFjkuIn

(Only applicable for extension issues) IDE Information

No response

Activity

  1. added
    narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or both
    on Oct 23, 2026
  2. github-actions commented on Nov 7, 2026

    @github-actions

    This issue has someone assigned, but has not had recent activity for more than 2 weeks.

    If you are still working on this issue, please add a comment so everyone knows. Otherwise, please comment #unclaim to unassign yourself.

    Thank you for your contributions!

  3. shahhamdihassan commented on Nov 9, 2026

    @shahhamdihassan
    Contributor

    Still on this one. The fix is written and waiting for review on our side. I'll post here once it lands.

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

Metadata

Metadata

Labels

narrowingIssues with narrowing - root cause is usually narrowing, flow handling, or bothscoping-control-flowissues related to scoping and control flowtypechecking

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions