Skip to content
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

RFC: refactor geometry matching clauses with Python 3.10's pattern matching #5051

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

neutrinoceros
Copy link
Member

PR Summary

Now that we don't support 3.9 anymore we have access to match/case statements (pattern matching). Arguably this improves readability in these instances, albeit at the cost of an additional indentation level.

@neutrinoceros neutrinoceros added the refactor improve readability, maintainability, modularity label Nov 11, 2024
matthewturk
matthewturk previously approved these changes Nov 11, 2024
Copy link
Member

@matthewturk matthewturk left a comment

Choose a reason for hiding this comment

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

Oh, I like this a lot more. Thank you.

Quick question -- is there something about the way the | is evaluated in the cases that means it has to be put in the case? Or, could we create top-level special cases like HAS_Z_AXIS for CARTESIAN | POLAR | SPECTRAL_CUBE ?

@neutrinoceros
Copy link
Member Author

is there something about the way the | is evaluated in the cases that means it has to be put in the case?

If you're asking if | has a syntactically different meaning in the context of case clauses, the answer is yes.
See https://peps.python.org/pep-0636/#or-patterns

could we create top-level special cases like HAS_Z_AXIS for CARTESIAN | POLAR | SPECTRAL_CUBE ?

I think this would be achievable using IntFlag enums, however we need geometry to be an StrEnum for backward compatibility, and I don't fancy trying to implement a common subclass.

@matthewturk
Copy link
Member

I think this would be achievable using IntFlag enums, however we need geometry to be an StrEnum for backward compatibility, and I don't fancy trying to implement a common subclass.

Me neither! I think the first answer, that | is syntactically different, is enough to say this is perfect as-is. Thank you!

@neutrinoceros
Copy link
Member Author

I want to push couple times to this branch before I can undraft this.

@neutrinoceros neutrinoceros marked this pull request as ready for review November 11, 2024 12:59
Copy link
Contributor

@chrishavlin chrishavlin left a comment

Choose a reason for hiding this comment

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

ok, i'm now convinced of match/case's usefulness :) I was kinda ambivalent about it before.

@neutrinoceros
Copy link
Member Author

I'll merge now to save myself some conflict solving later.

@neutrinoceros neutrinoceros merged commit 9e483ce into yt-project:main Nov 14, 2024
13 checks passed
@neutrinoceros neutrinoceros added this to the 4.5.0 milestone Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor improve readability, maintainability, modularity
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants