Flake8 Class Attributes Order

0.3.0 · active · verified Wed Apr 15

An extension for Flake8 that enforces a consistent order for attributes within Python classes. It analyzes various attribute types, including docstrings, properties, methods, nested classes, and constants, and can also detect Django model fields. The current version is 0.3.0, and it maintains an active release cadence with regular updates.

Warnings

Install

Imports

Quickstart

To use `flake8-class-attributes-order`, first install it. Then, run `flake8` as you normally would. The plugin automatically integrates. You can configure the order or enable strict mode via a configuration file such as `setup.cfg` or `.flake8`.

import os

# Create a dummy python file for flake8 to check
with open('my_module.py', 'w') as f:
    f.write("""class MyClass:
    FOO = 1
    def __init__(self):
        self.bar = 2
    def method_a(self):
        pass
""")

# Create a flake8 configuration file to enable strict mode (optional, but good for demo)
with open('setup.cfg', 'w') as f:
    f.write("[flake8]\nuse_class_attributes_order_strict_mode = True\n")

# Run flake8. Note: flake8's exit code is non-zero if errors are found.
# We use os.system for demonstration; in a real CI, you'd just run 'flake8'.
print("Running flake8 without strict mode (default order):")
os.system("flake8 my_module.py")

print("\nRunning flake8 with strict mode (via setup.cfg):")
os.system("flake8 my_module.py")

# Clean up
os.remove('my_module.py')
os.remove('setup.cfg')

view raw JSON →