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

RUF022, RUF023: never add two trailing commas to the end of a sequence #9698

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF022.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,38 @@

__all__ = ("don't" "care" "about", "__all__" "with", "concatenated" "strings")

############################################################
# Trailing-comma edge cases that should be flagged and fixed
############################################################

__all__ = (
"loads",
"dumps",)

__all__ = [
"loads",
"dumps" , ]

__all__ = ['xp', 'yp',
'canvas'

# very strangely placed comment

,

# another strangely placed comment
]

__all__ = (
"foo"
# strange comment 1
,
# comment about bar
"bar"
# strange comment 2
,
)

###################################
# These should all not get flagged:
###################################
Expand Down
40 changes: 37 additions & 3 deletions crates/ruff_linter/resources/test/fixtures/ruff/RUF023.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ class Klass3:
"a"
]

##########################################
# Messier multiline __all__ definitions...
##########################################
##################################
# Messier multiline definitions...
##################################

class Klass4:
# comment0
Expand Down Expand Up @@ -154,6 +154,40 @@ class Klass5:
)
__slots__ = ("don't" "care" "about", "__slots__" "with", "concatenated" "strings")

############################################################
# Trailing-comma edge cases that should be flagged and fixed
############################################################

class BezierBuilder:
__slots__ = ('xp', 'yp',
'canvas',)

class BezierBuilder2:
__slots__ = {'xp', 'yp',
'canvas' , }

class BezierBuilder3:
__slots__ = ['xp', 'yp',
'canvas'

# very strangely placed comment

,

# another strangely placed comment
]

class BezierBuilder4:
__slots__ = (
"foo"
# strange comment 1
,
# comment about bar
"bar"
# strange comment 2
,
)

###################################
# These should all not get flagged:
###################################
Expand Down
11 changes: 9 additions & 2 deletions crates/ruff_linter/src/rules/ruff/rules/sequence_sorting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ruff_python_ast as ast;
use ruff_python_codegen::Stylist;
use ruff_python_parser::{lexer, Mode, Tok, TokenKind};
use ruff_python_stdlib::str::is_cased_uppercase;
use ruff_python_trivia::leading_indentation;
use ruff_python_trivia::{leading_indentation, SimpleTokenKind, SimpleTokenizer};
use ruff_source_file::Locator;
use ruff_text_size::{Ranged, TextRange, TextSize};

Expand Down Expand Up @@ -434,14 +434,21 @@ impl MultilineStringSequenceValue {
locator,
);

let mut postlude_tokens =
SimpleTokenizer::starts_at(TextSize::new(0), &postlude).skip_trivia();
let needs_trailing_comma = self.ends_with_trailing_comma
&& postlude_tokens
.next()
.map_or(true, |tok| tok.kind() != SimpleTokenKind::Comma);
AlexWaygood marked this conversation as resolved.
Show resolved Hide resolved

self.items
.sort_by(|a, b| sorting_style.compare(&a.value, &b.value));
let joined_items = join_multiline_string_sequence_items(
&self.items,
locator,
&item_indent,
newline,
self.ends_with_trailing_comma,
needs_trailing_comma,
);

format!("{prelude}{joined_items}{postlude}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -896,8 +896,129 @@ RUF022.py:219:11: RUF022 `__all__` is not sorted
219 | __all__ = ("don't" "care" "about", "__all__" "with", "concatenated" "strings")
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF022
220 |
221 | ###################################
221 | ############################################################
|
= help: Apply an isort-style sorting to `__all__`

RUF022.py:225:11: RUF022 [*] `__all__` is not sorted
|
223 | ############################################################
224 |
225 | __all__ = (
| ___________^
226 | | "loads",
227 | | "dumps",)
| |_____________^ RUF022
228 |
229 | __all__ = [
|
= help: Apply an isort-style sorting to `__all__`

ℹ Safe fix
223 223 | ############################################################
224 224 |
225 225 | __all__ = (
226 |- "loads",
227 |- "dumps",)
226 |+ "dumps",
227 |+ "loads",)
228 228 |
229 229 | __all__ = [
230 230 | "loads",

RUF022.py:229:11: RUF022 [*] `__all__` is not sorted
|
227 | "dumps",)
228 |
229 | __all__ = [
| ___________^
230 | | "loads",
231 | | "dumps" , ]
| |_________________________^ RUF022
232 |
233 | __all__ = ['xp', 'yp',
|
= help: Apply an isort-style sorting to `__all__`

ℹ Safe fix
227 227 | "dumps",)
228 228 |
229 229 | __all__ = [
230 |- "loads",
231 |- "dumps" , ]
230 |+ "dumps",
231 |+ "loads" , ]
232 232 |
233 233 | __all__ = ['xp', 'yp',
234 234 | 'canvas'

RUF022.py:233:11: RUF022 [*] `__all__` is not sorted
|
231 | "dumps" , ]
232 |
233 | __all__ = ['xp', 'yp',
| ___________^
234 | | 'canvas'
235 | |
236 | | # very strangely placed comment
237 | |
238 | | ,
239 | |
240 | | # another strangely placed comment
241 | | ]
| |_________________^ RUF022
242 |
243 | __all__ = (
|
= help: Apply an isort-style sorting to `__all__`

ℹ Safe fix
230 230 | "loads",
231 231 | "dumps" , ]
232 232 |
233 |-__all__ = ['xp', 'yp',
234 |- 'canvas'
233 |+__all__ = [
234 |+ 'canvas',
235 |+ 'xp',
236 |+ 'yp'
235 237 |
236 238 | # very strangely placed comment
237 239 |

RUF022.py:243:11: RUF022 [*] `__all__` is not sorted
|
241 | ]
242 |
243 | __all__ = (
| ___________^
244 | | "foo"
245 | | # strange comment 1
246 | | ,
247 | | # comment about bar
248 | | "bar"
249 | | # strange comment 2
250 | | ,
251 | | )
| |_^ RUF022
252 |
253 | ###################################
|
= help: Apply an isort-style sorting to `__all__`

ℹ Safe fix
241 241 | ]
242 242 |
243 243 | __all__ = (
244 |- "foo"
245 244 | # strange comment 1
246 |- ,
247 245 | # comment about bar
248 |- "bar"
246 |+ "bar",
247 |+ "foo"
249 248 | # strange comment 2
250 249 | ,
251 250 | )
Comment on lines +1009 to +1022
Copy link
Member Author

@AlexWaygood AlexWaygood Jan 30, 2024

Choose a reason for hiding this comment

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

It's hard to say where a comment like # strange comment 1 should go really, if it lies between the string item and the associated comma. Possibly it should move with "foo", even though it's directly above "bar". But this is a very unlikely edge case, and the important thing is that the comment isn't deleted, in my opinion.



Loading
Loading