astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
31.42k stars 1.05k forks source link

[Feature Request] Sort dict keys #10085

Open Tatsh opened 7 months ago

Tatsh commented 7 months ago

Would be nice and I did not find this feature already being requested.

ESLint has this feature: sort-keys. Should have options like case-sensitivity, minimum keys, natural, asc/desc. # noqa: ... should disable the requirement and stop the auto-fixer.

AlexWaygood commented 7 months ago

Thanks for the feature request! For reference, this is similar to a few issues that have been opened before:

However, I think we should keep this open. The first two of those issues were closed in favour of #1198, which was closed out without implementing this specific feature. The third is different enough to this that it could be considered separate.

I'm not sure I'd want to turn on this rule in my Python projects, since Python dictionaries maintain insertion order when you iterate through them (they have been guaranteed to do so by the language spec since Python 3.7), and I've written code in the past that's relied on that useful property. That means that the autofix proposed here has the potential to break working code. The only Python builtin collections where it would be safe to autosort the collection are sets and frozensets, since those are the only two that do not maintain insertion order.

Nonetheless, it does seem like there's a fair amount of user demand for this feature, and I gather the ESLint rule also has the potential to change the iteration order of a JavaScript object. So I think we should consider having a rule like this. If we're sorting dictionary keys, though, we may as well also sort list, tuples and sets, since we'd already be doing something pretty unsafe by sorting dictionary keys.

One open question is whether enabling this rule should sort all collections? isort has a feature where collections that are marked with an # isort: unique-list comment are deduplicated and sorted -- we could possibly consider doing something similar.

MichaReiser commented 7 months ago

we may as well also sort list, tuples and sets, since we'd already be doing something pretty unsafe by sorting dictionary keys.

It would be worth having different rules, at least for lists. I've used object sorting in very large code bases in JS with very little need to suppress the rules because it's uncommon that the ordering matters (at least in the projects I worked). However, that's different for lists where ordering is more likely to be semantically meaningful (or I would have used a set).

JelleZijlstra commented 7 months ago

I would like this specifically for set (I realize that's not what the issue is about). Sets don't have insertion ordering, so unlike with dicts or lists, there isn't as much of a concern about changing behavior. For example, it would be nice if Ruff sorted this set so it's easier to keep track of which letters are in it.

AlexWaygood commented 7 months ago

I would like this specifically for set (I realize that's not what the issue is about). Sets don't have insertion ordering, so unlike with dicts or lists, there isn't as much of a concern about changing behavior. For example, it would be nice if Ruff sorted this set so it's easier to keep track of which letters are in it.

Yeah, I agree that it makes much more sense for sets than any other data structure. I wonder if this should be multiple rules — or one, configurable rule.