Skip to content
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

Format function argument separator comments #5211

Merged
merged 3 commits into from
Jun 21, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Jun 20, 2023

Summary

This is a complete rewrite of the handling of / and * comment handling in function signatures. The key problem is that slash and star don't have a note. We now parse out the positions of slash and star and their respective preceding and following note. I've left code comments for each possible case of function signature structure and comment placement

Test Plan

I extended the function statement fixtures with cases that i found. If you have more weird edge cases your input would be appreciated.

@konstin
Copy link
Member Author

konstin commented Jun 20, 2023

@github-actions
Copy link
Contributor

github-actions bot commented Jun 20, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      6.9±0.02ms     5.9 MB/sec    1.02      7.0±0.03ms     5.8 MB/sec
formatter/numpy/ctypeslib.py               1.00   1372.4±2.71µs    12.1 MB/sec    1.01   1388.3±2.43µs    12.0 MB/sec
formatter/numpy/globals.py                 1.00    132.4±0.26µs    22.3 MB/sec    1.01    133.2±0.62µs    22.2 MB/sec
formatter/pydantic/types.py                1.00      2.8±0.02ms     9.0 MB/sec    1.02      2.9±0.03ms     8.8 MB/sec
linter/all-rules/large/dataset.py          1.01     13.7±0.11ms     3.0 MB/sec    1.00     13.6±0.03ms     3.0 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.4±0.00ms     4.8 MB/sec    1.00      3.4±0.01ms     4.8 MB/sec
linter/all-rules/numpy/globals.py          1.00    358.8±1.71µs     8.2 MB/sec    1.00    358.9±1.82µs     8.2 MB/sec
linter/all-rules/pydantic/types.py         1.00      6.0±0.01ms     4.2 MB/sec    1.00      6.0±0.01ms     4.2 MB/sec
linter/default-rules/large/dataset.py      1.00      7.0±0.01ms     5.8 MB/sec    1.00      7.0±0.02ms     5.8 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1463.7±4.83µs    11.4 MB/sec    1.00   1461.8±3.24µs    11.4 MB/sec
linter/default-rules/numpy/globals.py      1.00    154.5±0.30µs    19.1 MB/sec    1.00    154.7±0.19µs    19.1 MB/sec
linter/default-rules/pydantic/types.py     1.00      3.2±0.01ms     8.1 MB/sec    1.00      3.2±0.01ms     8.1 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
formatter/large/dataset.py                 1.00      7.9±0.16ms     5.2 MB/sec    1.08      8.5±0.11ms     4.8 MB/sec
formatter/numpy/ctypeslib.py               1.00  1598.8±47.53µs    10.4 MB/sec    1.06  1692.9±30.68µs     9.8 MB/sec
formatter/numpy/globals.py                 1.00    157.4±4.71µs    18.7 MB/sec    1.06    166.5±5.38µs    17.7 MB/sec
formatter/pydantic/types.py                1.00      3.3±0.06ms     7.8 MB/sec    1.08      3.5±0.06ms     7.2 MB/sec
linter/all-rules/large/dataset.py          1.00     15.8±0.24ms     2.6 MB/sec    1.01     15.9±0.29ms     2.6 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.01      4.2±0.07ms     4.0 MB/sec    1.00      4.2±0.07ms     4.0 MB/sec
linter/all-rules/numpy/globals.py          1.00   512.6±12.19µs     5.8 MB/sec    1.00   511.7±15.62µs     5.8 MB/sec
linter/all-rules/pydantic/types.py         1.01      7.2±0.14ms     3.6 MB/sec    1.00      7.1±0.13ms     3.6 MB/sec
linter/default-rules/large/dataset.py      1.00      8.2±0.11ms     5.0 MB/sec    1.00      8.1±0.12ms     5.0 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1735.6±27.66µs     9.6 MB/sec    1.00  1739.7±27.39µs     9.6 MB/sec
linter/default-rules/numpy/globals.py      1.00    202.9±7.80µs    14.5 MB/sec    1.00    203.1±7.58µs    14.5 MB/sec
linter/default-rules/pydantic/types.py     1.01      3.7±0.07ms     6.9 MB/sec    1.00      3.7±0.05ms     6.9 MB/sec

@konstin konstin added the formatter Related to the formatter label Jun 21, 2023
@konstin konstin force-pushed the format_function_argument_separator_comments branch from f3f2a6e to 93b9a3e Compare June 21, 2023 10:12
@konstin konstin force-pushed the format_function_argument_separator_comments branch from 93b9a3e to 6cc0256 Compare June 21, 2023 10:23
@konstin konstin marked this pull request as ready for review June 21, 2023 10:23
crates/ruff_python_formatter/src/comments/placement.rs Outdated Show resolved Hide resolved
Comment on lines 267 to 270
let slash = if arguments.posonlyargs.is_empty() {
// If there are no positional only arguments the default handling below is wrong
None
} else if let Some(preceding_end) = arguments.posonlyargs.last().map(Ranged::end) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this branch? The else branch returns None and the else if can never be true if posonlyargs is empty.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, this was remnant of the defaults list time

};

// If we have a vararg we have a node that the comments attach to
let star = if arguments.kwonlyargs.is_empty() || arguments.vararg.is_some() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: arguments.kewonlyargs.is_empty is already handled by the else branch (the only case where .first() returns None)

/// ^^^^^^ keyword only arguments (kwargs)
/// ```
pub(crate) fn assign_argument_separator_comment_placement(
slash: Option<(TextSize, TextRange, TextSize)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Define a struct with named fields. It's hard to guess the meaning of the text positions and range.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored the entire code

crates/ruff_python_formatter/src/other/arguments.rs Outdated Show resolved Hide resolved
Base automatically changed from fix_cypthon_errors_set_2 to main June 21, 2023 17:45
 ## Summary

This is a complete rewrite of the handling of `/` and `*` comment handling in function signatures. The key problem is that slash and star don't have a note. We now parse out the positions of slash and star and their respective preceding and following note. I've left code comments for each possible case of function signature structure and comment placement

## Test Plan

I extended the function statement fixtures with cases that i found. If you have more weird edge cases your input would be appreciated.
@konstin konstin force-pushed the format_function_argument_separator_comments branch from fa2db93 to 3862051 Compare June 21, 2023 17:50
@konstin konstin enabled auto-merge (squash) June 21, 2023 17:50
@konstin konstin merged commit d7c7484 into main Jun 21, 2023
@konstin konstin deleted the format_function_argument_separator_comments branch June 21, 2023 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants