ruff/crates
Alex Waygood f5061dbb8e
Add a rule/autofix to sort __slots__ and __match_args__ (#9564)
## Summary

This PR introduces a new rule to sort `__slots__` and `__match_args__`
according to a [natural sort](https://en.wikipedia.org/wiki/Natural_sort_order), as was
requested in https://github.com/astral-sh/ruff/issues/1198#issuecomment-1881418365.

The implementation here generalises some of the machinery introduced in
3aae16f1bd
so that different kinds of sorts can be applied to lists of string
literals. (We use an "isort-style" sort for `__all__`, but that isn't
really appropriate for `__slots__` and `__match_args__`, where nearly
all items will be snake_case.) Several sections of code have been moved
from `sort_dunder_all.rs` to a new module, `sorting_helpers.rs`, which
`sort_dunder_all.rs` and `sort_dunder_slots.rs` both make use of.

`__match_args__` is very similar to `__all__`, in that it can only be a
tuple or a list. `__slots__` differs from the other two, however, in
that it can be any iterable of strings. If slots is a dictionary, the
values are used by the builtin `help()` function as per-attribute
docstrings that show up in the output of `help()`. (There's no
particular use-case for making `__slots__` a set, but it's perfectly
legal at runtime, so there's no reason for us not to handle it in this
rule.)

Note that we don't do an autofix for multiline `__slots__` if `__slots__` is a dictionary: that's out of scope. Everything else, we can nearly always fix, however.

## Test Plan

`cargo test` / `cargo insta review`.

I also ran this rule on CPython, and the diff looked pretty good

---

Co-authored-by: Micha Reiser <micha@reiser.io>
2024-01-22 12:21:55 +00:00
..
ruff Bump version to v0.1.14 (#9581) 2024-01-19 12:54:39 -05:00
ruff_benchmark Approximate tokens len (#9546) 2024-01-19 17:39:37 +01:00
ruff_cache Make all dependencies workspace dependencies (#9333) 2024-01-02 13:41:59 +00:00
ruff_dev Rename ruff_cli crate to ruff (#9557) 2024-01-16 17:47:01 -05:00
ruff_diagnostics Enable annotation quoting for multi-line expressions (#9142) 2023-12-15 01:03:09 +00:00
ruff_formatter --show-settings displays active settings in a far more readable format (#9464) 2024-01-12 14:30:29 -05:00
ruff_index Make all dependencies workspace dependencies (#9333) 2024-01-02 13:41:59 +00:00
ruff_linter Add a rule/autofix to sort __slots__ and __match_args__ (#9564) 2024-01-22 12:21:55 +00:00
ruff_macros remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
ruff_notebook Generate deterministic ids when formatting notebooks (#9359) 2024-01-04 09:19:00 -06:00
ruff_python_ast Move is_tuple_parenthesized from the formatter to ruff_python_ast (#9533) 2024-01-15 16:10:40 +00:00
ruff_python_codegen Remove source path from parser errors (#9322) 2023-12-30 20:33:05 +00:00
ruff_python_formatter Move is_tuple_parenthesized from the formatter to ruff_python_ast (#9533) 2024-01-15 16:10:40 +00:00
ruff_python_index Approximate tokens len (#9546) 2024-01-19 17:39:37 +01:00
ruff_python_literal Use Rust 1.75 toolchain (#9437) 2024-01-08 18:03:16 +01:00
ruff_python_parser Approximate tokens len (#9546) 2024-01-19 17:39:37 +01:00
ruff_python_resolver Use Rust 1.75 toolchain (#9437) 2024-01-08 18:03:16 +01:00
ruff_python_semantic [tryceratops] Add fix for error-instead-of-exception (TRY400) (#9520) 2024-01-16 03:00:04 +00:00
ruff_python_stdlib Add an explicit fast path for whitespace to is_identifier_continuation (#9532) 2024-01-16 08:23:43 +00:00
ruff_python_trivia Add an explicit fast path for whitespace to is_identifier_continuation (#9532) 2024-01-16 08:23:43 +00:00
ruff_shrinking Bump version to v0.1.14 (#9581) 2024-01-19 12:54:39 -05:00
ruff_source_file Add cell indexes to all diagnostics (#9387) 2024-01-04 14:02:23 +00:00
ruff_text_size Make all dependencies workspace dependencies (#9333) 2024-01-02 13:41:59 +00:00
ruff_wasm Approximate tokens len (#9546) 2024-01-19 17:39:37 +01:00
ruff_workspace Include global --config when determining namespace packages (#9603) 2024-01-21 19:10:43 -05:00