You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi there - thanks for the great repo. I've been continually amazed by how many checks have been included and how fast ruff is. The work y'all are doing is amazing.
I'm interesting in adding a new set of linter checks around alphabetized enums. In the variety of corporate repos I work in, there are some use cases with very long lists of values in an enum, like log types, slack channel names, etc. We've been enforcing that these stay in alphabetical order via PR, but obviously enforcing via a lint check would be easier, especially if it can be auto fixed. I've tried to find flake8 extensions that have a similar behavior but I have yet to find anything that does this exactly. I'm a python developer without any Rust experience (yet) but would be interested in implementing in ruff if this is deemed appropriate as a new kind of check.
Some examples:
1️⃣ Alphabetize non-auto string values based on the enum attribute names.
2️⃣ Order non-auto integer values based on the enum values
It's fairly common to use enums to handle priority order, so I think the clearest thing to do is to sort these on the values, not on the attribute names. This could also be sorted on the attribute names.
4️⃣ Do Nothing with regular enums that use auto()
Because regular enum auto() assigns integer values, changing the order will change the integer values that get assigned. Assuming all of the uses are internal, this might not cause issues, but if the values are written to a database, used outside the application, or any comparisons happen directly to the integer values, this could cause problems. As a result, I think it's better to leave enums that look like this alone
I think this is overall a reasonable suggestion. It'll take some care to preserve comments, if that's something that we care about.
The main blockers here, IMO, are: (1) we're a little behind on defining the criteria for inclusion new checks (#2257), and (2) we need better mechanisms for categorizing these novel rules (we can put them in RUF, but I'd rather they go in some more experimental section -- see #1774).
I'm hoping to write something up to kickstart the discussion on (1) by the end of the week.
Or whatever... Sometimes there is a special value that we don't want to sort, but I guess I'd expect the violation to normally be raised against the class Animals(Enum): line so flagging this could be awkward.
This is great suggestion! Something related would be the ability to sort collections that use enums as keys similar to the undocumented isort options to sort collections with string keys. I do this manually in many places which is always a pain.
Being able to opt out an entire enum rather than each member with a comment would be useful also.
Hi there - thanks for the great repo. I've been continually amazed by how many checks have been included and how fast
ruff
is. The work y'all are doing is amazing.I'm interesting in adding a new set of linter checks around alphabetized enums. In the variety of corporate repos I work in, there are some use cases with very long lists of values in an enum, like log types, slack channel names, etc. We've been enforcing that these stay in alphabetical order via PR, but obviously enforcing via a lint check would be easier, especially if it can be auto fixed. I've tried to find flake8 extensions that have a similar behavior but I have yet to find anything that does this exactly. I'm a python developer without any Rust experience (yet) but would be interested in implementing in
ruff
if this is deemed appropriate as a new kind of check.Some examples:
1️⃣ Alphabetize non-auto string values based on the enum attribute names.
Bad:
Good:
2️⃣ Order non-auto integer values based on the enum values
It's fairly common to use enums to handle priority order, so I think the clearest thing to do is to sort these on the values, not on the attribute names. This could also be sorted on the attribute names.
Bad:
Good:
3️⃣ Alphabetize auto values based on the
StrEnum
attribute names.When using a
StrEnum
,auto
will set the values toname.lower()
so changing the order will have no impact.Bad:
Good:
4️⃣ Do Nothing with regular enums that use
auto()
Because regular enum
auto()
assigns integer values, changing the order will change the integer values that get assigned. Assuming all of the uses are internal, this might not cause issues, but if the values are written to a database, used outside the application, or any comparisons happen directly to the integer values, this could cause problems. As a result, I think it's better to leave enums that look like this aloneThe text was updated successfully, but these errors were encountered: