Skip to content

Python: case _: adds a decision point #9

Open

Description

@dvokhmintsev

Each case_clause adds 1, including the wildcard case _:, which is the fall-through and not a decision. A two-way match scores higher than the equivalent if.

def classify_match(x):
    match x:
        case 0:
            return "zero"
        case _:
            return "other"


def classify_if(x):
    if x == 0:
        return "zero"
    return "other"

crapper --no-coverage src/demo/cc.py:

classify_match                 demo.cc                                3   N/A       N/A
classify_if                    demo.cc                                2   N/A       N/A

Expected: both 2. Counting an irrefutable case block (case _: or a bare capture such as case other:, see PEP 634) as the default branch would match how switch default is usually treated.

Environment: Linux x86_64, Python 3.11.13, tree-sitter 0.26.0, tree-sitter-language-pack 1.22.1, crapper 6fd92c4.

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions