-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Implement FormatPatternMatchValue
#6799
Conversation
851c962
to
62857fc
Compare
cbdbb72
to
a304c80
Compare
62857fc
to
c9e664d
Compare
PR Check ResultsBenchmarkLinux
Windows
|
match foo: | ||
case 1: | ||
y = 0 | ||
case ((1)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would the formatting be like after the fix for double parentheses? Would it still keep the parentheses from the original code i.e., format is as (1)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah nevermind, it's answered in your other PR.
c9e664d
to
382e30f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, this will make testing of other match patterns much easier.
I'm not sure if you saw this, but @evanrittenhouse was working on this #6608
use ruff_formatter::{write, Buffer, FormatResult}; | ||
use ruff_python_ast::PatternMatchValue; | ||
|
||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter}; | ||
use crate::{AsFormat, FormatNodeRule, PyFormatter}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: use crate::prelude::*
. It should allow removing Buffer
, FormatResult
, PyFormatter
, and AsFormat
(and it makes it easier to change the code in the future without having to add a ton of imports)
crates/ruff_python_formatter/src/pattern/pattern_match_value.rs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added Closes #6555
to the description
crates/ruff_python_formatter/resources/test/fixtures/ruff/statement/match.py
Show resolved
Hide resolved
382e30f
to
fb314e6
Compare
Thanks! I did see the other PR but I wanted to push these along so that I could start working on some of the other problems around comments and parentheses without so many |
## Summary This PR fixes the duplicate-parenthesis problem that's visible in the tests from #6799. The issue is that we might have parentheses around the entire match-case pattern, like in `(1)` here: ```python match foo: case (1): y = 0 ``` In this case, the inner expression (`1`) will _think_ it's parenthesized, but we'll _also_ detect the parentheses at the case level -- so they get rendered by the case, then again by the expression. Instead, if we detect parentheses at the case level, we can force-off the parentheses for the pattern using a design similar to the way we handle parentheses on expressions. Closes #6753. ## Test Plan `cargo test`
Summary
This is effectively #6608, but with additional tests.
We aren't properly handling parenthesized patterns, but that needs to be dealt with separately as it's somewhat involved.
Closes #6555