-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support pattern matching #282
Comments
Yeah it makes some appearances in the RustPython AST but it doesn't seem to be supported yet by the actual parser. |
This can't be merged until ruff supports pattern matching; see astral-sh/ruff#282.
The parser in question is |
Oh, looks like someone beat me to it 🙂 RustPython/RustPython#4323 |
Yeah that's the one! There's been a little bit of progress in that PR. There's also someone who's been looking to migrate the entire parser to |
I'd love to sponsor someone (financially, that is) to work on this issue (in RustPython). If you're interested, shoot me a DM on Twitter or email me at [email protected] :) |
Actually when there is a |
@JadHADDAD92 - So sorry that you lost time to this. It is the expected behavior, because syntax errors have their own error code ( A few alternatives:
|
I find the first alternative to be the better behavior, because I presume all ruff rules depend on a successful parse of RustPython (right?) therefore if we lint a module which contains a syntax error (failed to parse), and ruff returns 0 as if the file is well linted, it brings no clarity of what happened. |
Not all rules depend on a successful parse. Some can work off the token stream, or even partial stream. Some rules only look at raw lines (like the line-length violations). And some work off the filesystem (e.g., the rule to detect missing We could: always log an |
Warning: match-case isn't support by ruff yet. astral-sh/ruff#282
What direction are we going in order to support this? |
I'm working on it here: RustPython/RustPython#4519. There are a few problems to solve but that implementation already supports many of the variants. |
I'm making good progress on this (I think?). If anyone wants to link to an OSS codebase that makes good use of match statements (for testing), it'd be appreciated. |
@charliermarsh there are good match examples in the 2nd edition Fluent Python book. This snippet looks like it would catch some edge cases. In fact, if you search in that repo for the word “match” you’ll get examples of that word being used as a keyword as well as a normal variable. |
Pyright has some good examples. |
Also test |
Python's own test suite for pattern matching seems like a good litmus test: https://github.com/python/cpython/blob/main/Lib/test/test_patma.py https://github.com/brandtbucher/patmaperformance also has some examples. |
Parser looking good so far. I got it to parse Black's fixtures which cover a lot of tricky cases. |
(It also handles soft keywords as pointed out by @ofek. I've also run it over Hatch, Airflow, and Pandas, and confirmed that the output is unchanged, so it's not incorrectly detecting any |
(Oh, I guess the Black test suite is a subet of |
I expect this to go out some time this week, hopefully in the next few days. The PR is up as #3047 (plus depends on RustPython/RustPython#4519 in RustPython). |
Thank you! Heads up for anyone else using the VS code extension you will need to set your ruff path:
Looks like the extension has it's own ruff executable, which has already been updated to use the latest ruff but hasn't been released yet. |
@danstewart most likely you want this instead:
which will find |
Thanks, that's much better. |
@danstewart - I cut a new pre-release last night that includes v0.0.251, so you can pull it in if you switch to the pre-release channel. I'll probably cut a main-channel release today or tomorrow. |
See also discussion in Ruff issue: astral-sh/ruff#282 (comment)
* pyproject, Makefile: use ruff This can't be merged until ruff supports pattern matching; see astral-sh/ruff#282. * Remove more flake8 refs
Structural pattern matching ([PEP 622](https://peps.python.org/pep-0622/), tracked by astral-sh#282) was implemented in astral-sh#3047, [v0.0.250](https://github.com/charliermarsh/ruff/releases/tag/v0.0.250). Relates-to: astral-sh#3091 where the compatibility was (partially) re-added.
As I am learning the source code, it seems to me that python's pattern matching is not supported at all.
We have a visitor ready: https://github.com/charliermarsh/ruff/blob/c7349b69c12b7511d48fc45390eac8d7282062a5/src/ast/visitor.rs#L527-L571
But, that's about it.
Things to support (feel free to update):
MatchAs
pattern)float
sThe text was updated successfully, but these errors were encountered: