Implement FormatPatternMatchValue#6799
Conversation
851c962 to
62857fc
Compare
cbdbb72 to
a304c80
Compare
62857fc to
c9e664d
Compare
PR Check ResultsBenchmarkLinuxWindows |
| match foo: | ||
| case 1: | ||
| y = 0 | ||
| case ((1)): |
There was a problem hiding this comment.
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.
Ah nevermind, it's answered in your other PR.
c9e664d to
382e30f
Compare
MichaReiser
left a comment
There was a problem hiding this comment.
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.
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
konstin
left a comment
There was a problem hiding this comment.
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