Skip to content

chore: pattern matching #2434

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

henryiii
Copy link
Contributor

Adding some pattern matching where I think it makes sense to do so.

henryiii added 2 commits May 29, 2025 13:25
Signed-off-by: Henry Schreiner <[email protected]>
@henryiii henryiii requested a review from Copilot May 29, 2025 22:05
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates multiple modules to use Python’s structural pattern matching, replacing traditional if–elif chains for increased clarity and exhaustiveness. Key changes include refactoring conditionals in functions like is_main, setup_python, build, and others to use match/case and assert_never for default cases.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
cibuildwheel/projectfiles.py Replaced nested if conditions in is_main with a match/case statement supporting pattern matching.
cibuildwheel/platforms/windows.py Refactored build setup functions to employ match/case on build_frontend and its name.
cibuildwheel/platforms/macos.py Updated if–elif chains for build setup to use match/case patterns consistently.
cibuildwheel/platforms/linux.py Applied pattern matching to build_in_container logic instead of if–elif branches.
cibuildwheel/platforms/ios.py Converted if–elif conditions into match/case expressions for both setup_python and build functions.
cibuildwheel/options.py Implemented match/case for inheritance rules and setting type stringification.
cibuildwheel/oci_container.py Replaced if–elif conditions with match/case for engine.name in the version check functions.
cibuildwheel/architecture.py Used a match/case expression to selectively handle bitness values with assert_never as default.

@joerick
Copy link
Contributor

joerick commented May 29, 2025

Do you mind if we punt this to after v3.0? Big diffs like this seem like unecessary risk when tapering to a release.

@henryiii henryiii added the Hold for future release This PR might be complete, but is scheduled to be merged in a future release. Don't merge yet. label May 29, 2025
@henryiii
Copy link
Contributor Author

Sure, I even almost added the tag when I opened it.


return len(consts) == 1
match parent:
case ast.If(test=ast.Compare(left=left, ops=[ast.Eq()], comparators=[comp])):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW, I really like this one too. :) I used AI to help me rewrite it - I'd generally think models do better on old code, but it knew exactly how to do this. :)

@joerick joerick removed the Hold for future release This PR might be complete, but is scheduled to be merged in a future release. Don't merge yet. label Jun 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants