Skip to content

[Python][Bug]: Matching against multiple union cases in OR pattern returns none #4649

Closed

Description

@HLWeil

I have the following issue where I have a discriminate union and a function containing a match expression where matching against two of the three union cases should call the same expressionr. I expect the atLeastMid function to return True both for BestCase and MidCase. But for BestCase it returns none in python transpiled code.

type Case =
    | BestCase
    | MidCase
    | WorstCase

let atLeastMid (c: Case) =
    match c with
    | BestCase | MidCase -> true
    | WorstCase -> false

printf "%b" (atLeastMid BestCase)
printf "%b" (atLeastMid MidCase)
printf "%b" (atLeastMid WorstCase)

transpiles to (case 0 is missing):

def at_least_mid(c: Case) -> bool:
    match c.tag:
        case 1:
            return True

        case 2:
            return False

And therefore returns (none should also be true):

none
true
false

using Fable5.1

Distinction

This seems to be specific to matching against discriminate union cases, as the following works:

let oneOrTwo (x: int) =
    match x with
    | 1 | 2 -> true
    | _ -> false

--> (notice both 1 and 2 covered in one case)

def one_or_two(x: int32) -> bool:
    match x:
        case 1 | 2:
            return True

        case _:
            return False

Activity

  1. dbrattli commented on Jun 12, 2026

    @dbrattli
    Collaborator

    Thanks for the clear repro! Fixed in #4653.

    The Python match generator was dropping the wildcard case _: when the default branch shared a target with an explicit case, which left the union tag only reachable via the default (BestCase here) unmatched — so it returned None. The PR always emits the wildcard default and instead drops the redundant explicit cases that route to it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions