Summary -- This PR fixes #17796 by taking the approach mentioned in https://github.com/astral-sh/ruff/issues/17796#issuecomment-2847943862 of simply recursing into the `MatchAs` patterns when checking if we need parentheses. This allows us to reuse the parentheses in the inner pattern before also breaking the `MatchAs` pattern itself: ```diff match class_pattern: case Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture: pass - case ( - Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture - ): + case Class( + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ) as capture: pass - case ( - Class( - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - ) as capture - ): + case Class( + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ) as capture: pass case ( Class( @@ -685,13 +683,11 @@ match sequence_pattern_brackets: case [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture: pass - case ( - [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture - ): + case [ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ] as capture: pass - case ( - [ - xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - ] as capture - ): + case [ + xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx + ] as capture: pass ``` I haven't really resolved the question of whether or not it's okay always to recurse, but I'm hoping the ecosystem check on this PR might shed some light on that. Test Plan -- New tests based on the issue and then reviewing the ecosystem check here
55 lines
2.2 KiB
Rust
55 lines
2.2 KiB
Rust
//! Helpers to test if a specific preview style is enabled or not.
|
|
//!
|
|
//! The motivation for these functions isn't to avoid code duplication but to ease promoting preview styles
|
|
//! to stable. The challenge with directly using [`is_preview`](PyFormatContext::is_preview) is that it is unclear
|
|
//! for which specific feature this preview check is for. Having named functions simplifies the promotion:
|
|
//! Simply delete the function and let Rust tell you which checks you have to remove.
|
|
|
|
use crate::PyFormatContext;
|
|
|
|
/// Returns `true` if the [`hug_parens_with_braces_and_square_brackets`](https://github.com/astral-sh/ruff/issues/8279) preview style is enabled.
|
|
pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the [`no_chaperone_for_escaped_quote_in_triple_quoted_docstring`](https://github.com/astral-sh/ruff/pull/17216) preview style is enabled.
|
|
pub(crate) const fn is_no_chaperone_for_escaped_quote_in_triple_quoted_docstring_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the [`blank_line_before_decorated_class_in_stub`](https://github.com/astral-sh/ruff/issues/18865) preview style is enabled.
|
|
pub(crate) const fn is_blank_line_before_decorated_class_in_stub_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the
|
|
/// [`remove_parens_around_except_types`](https://github.com/astral-sh/ruff/pull/20768) preview
|
|
/// style is enabled.
|
|
pub(crate) const fn is_remove_parens_around_except_types_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the
|
|
/// [`allow_newline_after_block_open`](https://github.com/astral-sh/ruff/pull/21110) preview style
|
|
/// is enabled.
|
|
pub(crate) const fn is_allow_newline_after_block_open_enabled(context: &PyFormatContext) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the
|
|
/// [`avoid_parens_for_long_as_captures`](https://github.com/astral-sh/ruff/pull/21176) preview
|
|
/// style is enabled.
|
|
pub(crate) const fn is_avoid_parens_for_long_as_captures_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|