mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 02:12:22 +00:00
Use parenthesized_with_dangling_comments
in arguments formatter (#6376)
## Summary Fixes an instability whereby this: ```python def get_recent_deployments(threshold_days: int) -> Set[str]: # Returns a list of deployments not older than threshold days # including `/root/zulip` directory if it exists. recent = set() threshold_date = datetime.datetime.now() - datetime.timedelta( # noqa: DTZ005 days=threshold_days ) ``` Was being formatted as: ```python def get_recent_deployments(threshold_days: int) -> Set[str]: # Returns a list of deployments not older than threshold days # including `/root/zulip` directory if it exists. recent = set() threshold_date = ( datetime.datetime.now() - datetime.timedelta(days=threshold_days) # noqa: DTZ005 ) ``` Which was in turn being formatted as: ```python def get_recent_deployments(threshold_days: int) -> Set[str]: # Returns a list of deployments not older than threshold days # including `/root/zulip` directory if it exists. recent = set() threshold_date = ( datetime.datetime.now() - datetime.timedelta(days=threshold_days) # noqa: DTZ005 ) ``` The second-to-third formattings still differs from Black because we aren't taking the line suffix into account when splitting (https://github.com/astral-sh/ruff/issues/6377), but the first formatting is correct and should be unchanged (i.e., the first-to-second formattings is incorrect, and fixed here). ## Test Plan `cargo run --bin ruff_dev -- format-dev --stability-check ../zulip`
This commit is contained in:
parent
89e4e038b0
commit
63692b3798
3 changed files with 30 additions and 15 deletions
|
@ -111,3 +111,7 @@ f (
|
||||||
1
|
1
|
||||||
# abc
|
# abc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
|
||||||
|
days=threshold_days_threshold_days
|
||||||
|
)
|
||||||
|
|
|
@ -5,9 +5,8 @@ use ruff_python_trivia::{SimpleTokenKind, SimpleTokenizer};
|
||||||
use ruff_text_size::{TextRange, TextSize};
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
|
||||||
use crate::builders::empty_parenthesized_with_dangling_comments;
|
use crate::builders::empty_parenthesized_with_dangling_comments;
|
||||||
use crate::comments::trailing_comments;
|
|
||||||
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
|
use crate::expression::expr_generator_exp::GeneratorExpParentheses;
|
||||||
use crate::expression::parentheses::{parenthesized, Parentheses};
|
use crate::expression::parentheses::{parenthesized_with_dangling_comments, Parentheses};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::FormatNodeRule;
|
use crate::FormatNodeRule;
|
||||||
|
|
||||||
|
@ -35,18 +34,6 @@ impl FormatNodeRule<Arguments> for FormatArguments {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the arguments are non-empty, then a dangling comment indicates a comment on the
|
|
||||||
// same line as the opening parenthesis, e.g.:
|
|
||||||
// ```python
|
|
||||||
// f( # This call has a dangling comment.
|
|
||||||
// a,
|
|
||||||
// b,
|
|
||||||
// c,
|
|
||||||
// )
|
|
||||||
let comments = f.context().comments().clone();
|
|
||||||
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
|
|
||||||
write!(f, [trailing_comments(dangling_comments)])?;
|
|
||||||
|
|
||||||
let all_arguments = format_with(|f: &mut PyFormatter| {
|
let all_arguments = format_with(|f: &mut PyFormatter| {
|
||||||
let source = f.context().source();
|
let source = f.context().source();
|
||||||
let mut joiner = f.join_comma_separated(item.end());
|
let mut joiner = f.join_comma_separated(item.end());
|
||||||
|
@ -84,6 +71,17 @@ impl FormatNodeRule<Arguments> for FormatArguments {
|
||||||
joiner.finish()
|
joiner.finish()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If the arguments are non-empty, then a dangling comment indicates a comment on the
|
||||||
|
// same line as the opening parenthesis, e.g.:
|
||||||
|
// ```python
|
||||||
|
// f( # This call has a dangling comment.
|
||||||
|
// a,
|
||||||
|
// b,
|
||||||
|
// c,
|
||||||
|
// )
|
||||||
|
let comments = f.context().comments().clone();
|
||||||
|
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
|
||||||
|
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
[
|
[
|
||||||
|
@ -103,7 +101,12 @@ impl FormatNodeRule<Arguments> for FormatArguments {
|
||||||
// )
|
// )
|
||||||
// ```
|
// ```
|
||||||
// TODO(konstin): Doesn't work see wrongly formatted test
|
// TODO(konstin): Doesn't work see wrongly formatted test
|
||||||
parenthesized("(", &group(&all_arguments), ")")
|
parenthesized_with_dangling_comments(
|
||||||
|
"(",
|
||||||
|
dangling_comments,
|
||||||
|
&group(&all_arguments),
|
||||||
|
")"
|
||||||
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -117,6 +117,10 @@ f (
|
||||||
1
|
1
|
||||||
# abc
|
# abc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
|
||||||
|
days=threshold_days_threshold_days
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
## Output
|
## Output
|
||||||
|
@ -230,6 +234,10 @@ f(
|
||||||
1
|
1
|
||||||
# abc
|
# abc
|
||||||
)
|
)
|
||||||
|
|
||||||
|
threshold_date = datetime.datetime.now() - datetime.timedelta( # comment
|
||||||
|
days=threshold_days_threshold_days
|
||||||
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue