Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Add minimal support for pattern matching #1

Merged
merged 7 commits into from
Oct 26, 2021

Conversation

kennipj
Copy link
Collaborator

@kennipj kennipj commented Oct 26, 2021

Pattern matching syntax is currently unsupported in the current release of black, and will result in black crashing.

This PR attempts to provide minimal support for pattern matching, supporting most syntax, but more advanced use cases may not be covered.

Also supports soft-keyword use of match and case, as specified in PEP634.

I can't guarantee how well black will format code of long match/case statements, but it shouldn't break in most use cases.

No tests have been added here, as this is out of scope for this PR. Purpose is purely to make a quick fix to make black support pattern matching.

Some examples of syntax that is supported with this PR:

match point:
    case Point(x, y) if x == y:
        print(f"Y=X at {x}")
    case Point(x, y):
        print(f"Not on the diagonal")
match command.split():
    case ["go", direction] if direction in current_room.exits:
        current_room = current_room.neighbor(direction)
    case ["go", _]:
        print("Sorry, you can't go that way")
match command.split():
    case ["drop", *objects]:
        for obj in objects:
            character.drop(obj, current_room)
    match command.split():
        case ["north"] | ["go", "north"]:
            current_room = current_room.neighbor("north")
        case ["get", obj] | ["pick", "up", obj] | ["pick", obj, "up"]:
            ...  # Code for picking up the given object

Example of syntax not supported:

    match command.split():
        case ["go", ("north" | "south" | "east" | "west") as direction]:
            current_room = current_room.neighbor(direction)

This will fail on case ["go", ("north" | "south" | "east" | "west") as direction]:.

Faced some issues installing without running `pipenv update` on 3.10.
Since the purpose of this fork is purely to provide support for pattern matching, fixing potential failing python2 tests is not part of the scope of this fork.
This adds support for many pattern matching use cases, but not all syntax is supported.
This should support most cases where `match` and `case` are not used as keywords. There may be some edge cases that trigger an error in black.
@kennipj kennipj requested a review from povilasb October 26, 2021 09:44
@kennipj kennipj force-pushed the add-minimal-support-for-pattern-matching branch from 46b1c74 to dbfa366 Compare October 26, 2021 12:54
@kennipj kennipj force-pushed the add-minimal-support-for-pattern-matching branch from dbfa366 to e364e22 Compare October 26, 2021 13:06
Copy link

@povilasb povilasb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make a new branch though (argyle?), so that we could always pull latest black main branch and rebase off it if needed ;)

@kennipj kennipj changed the base branch from main to argyle October 26, 2021 16:29
@kennipj kennipj merged commit 15684c6 into argyle Oct 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants