ruff icon indicating copy to clipboard operation
ruff copied to clipboard

Support `flake8-class-attributes-order`

Open edgarrmondragon opened this issue 3 years ago • 7 comments

Hello, Is there a plan to support this flake8 extension? https://pypi.org/project/flake8-class-attributes-order/

Originally posted by @WorkHardes in https://github.com/charliermarsh/ruff/issues/106#issuecomment-1347928865

  • [ ] CCE001: Wrong class attributes order (XXX should be after YYY)
  • [ ] CCE002: Class level expression detected

edgarrmondragon avatar Jan 31 '23 23:01 edgarrmondragon

Has there been a decision to include this yet? Seems quite a few people would use it

sigma67 avatar Nov 06 '23 14:11 sigma67

Any decisions on this? It would be an awesome addition

lexygon avatar Jan 30 '24 14:01 lexygon

This one's hard to implement and can generally be unsafe. We don't have plans to implement it soon. Now that we unsafe fix support it might be more reasonable.

cc @AlexWaygood this kind of thing may be worth considering in categorization.

Related https://github.com/astral-sh/ruff/issues/3946

zanieb avatar Mar 11 '24 17:03 zanieb

I think auto-fixing isn't even the feature request here - although certainly a nice bonus.

Just having the detection capability would be great.

Does your argument only apply to the fixing part or do you also consider the detection hard to implement?

sigma67 avatar Mar 11 '24 17:03 sigma67

+1 to this, would be awesome to have it! I think almost any Python project defines many classes, hence having a consistent order of attributes is critical.

yury-fedotov avatar May 31 '24 19:05 yury-fedotov

+1, even having the capability to detect inconsistencies without having an auto-fix as such would be great!

mukul-mpac avatar Oct 07 '24 21:10 mukul-mpac

I'd like to take this on, but since this is still labeled needs-decision, I wanted to check whether the following scope would be acceptable before writing any code.

My proposed MVP would be a single preview, lint-only rule that detects out-of-order class members. It would not include an autofix. That matches the behavior of flake8-class-attributes-order itself, and avoids the unsafe-fix concerns around moving class members.

Rather than porting the CCE plugin 1:1, I'd scope this as one configurable rule with an ordered list of member categories, defaulting roughly to the upstream order. For example: docstring, fields, init, properties, methods, and so on. The separate class-level-expression check (CCE002) seems separable and may overlap with existing rules, so I'd leave it out of v1 unless you think it belongs here.

One possible overlap is with ssort (#3946 / #22235). My understanding is that ssort orders statements by dependency, while this rule would enforce category/section order within classes. Those are orthogonal, but they could conflict in strict configurations, so I'm happy to sequence this after ssort or adjust the scope accordingly.

Would this be a reasonable direction for a preview, detection-only rule? If so, I can follow up with a short design sketch before opening a PR.

vchilikov avatar Jun 09 '26 20:06 vchilikov