SortedAttributesRule

Ever wanted to sort a bunch of class attributes alphabetically? Well now it’s easy! Just add “@sorted-attributes” in the doc string of a class definition and lint will automatically sort all attributes alphabetically.

Feel free to add other methods and such – it should only affect class attributes.

Message

It appears you are using the @sorted-attributes directive and the class variables are unsorted. See the lint autofix suggestion.

Has Autofix: Yes

VALID Code Examples

# 1:

class MyConstants:
    """
    @sorted-attributes
    """
    A = 'zzz123'
    B = 'aaa234'

class MyUnsortedConstants:
    B = 'aaa234'
    A = 'zzz123'

INVALID Code Examples

# 1:

class MyUnsortedConstants:
    """
    @sorted-attributes
    """
    z = "hehehe"
    B = 'aaa234'
    A = 'zzz123'
    cab = "foo bar"
    Daaa = "banana"

    @classmethod
    def get_foo(cls) -> str:
        return "some random thing"

Autofix:

---
+++
@@ -3,11 +3,11 @@
     """
     @sorted-attributes
     """
+    A = 'zzz123'
+    B = 'aaa234'
+    Daaa = "banana"
+    cab = "foo bar"
     z = "hehehe"
-    B = 'aaa234'
-    A = 'zzz123'
-    cab = "foo bar"
-    Daaa = "banana"

     @classmethod
     def get_foo(cls) -> str: