Home

Awesome

flake8-class-attributes-order

Build Status Maintainability Test Coverage PyPI - Python Version

An extension for flake8 to report on wrong class attributes order and class level logic.

The validator can extract class attribute type: docstring, property, nested class, GLOBAL_VAR, etc. If django model fields are detected, the validator can detect, if the field is link to another table (foreign key, generic key, etc) or not.

After resolving each attribute type, validator checks attributes order.

Default configuration checks for the following order of attributes:

If the order is broken, validator will report on it.

Besides methods, the validator checks other attributes methods: docstrings, nested classes, constants, attributes, and so on.

Also validator checks, if class has no class level logic and report if any. Here is an example:

class PhoneForm(forms.Form):
    phone = forms.CharField(17, label='Телефон'.upper())

    # this should happen in __init__!
    phone.widget.attrs.update({'class': 'form-control phone'})

Installation

pip install flake8-class-attributes-order

Configuration

Strict mode

There is another preconfigured order that is more strict on private subtypes:

To enable strict validation, please set the flag in your config file:

[flake8]
use_class_attributes_order_strict_mode = True

Manual order configuration

Order can be manually configured via class_attributes_order config setting.

For example, if you prefer to put class Meta after constants and fields:

[flake8]
class_attributes_order =
    field,
    meta_class,
    nested_class,
    magic_method,
    property_method,
    static_method,
    class_method,
    method,
    protected_method,
    private_method

Configurable options:

OptionDescriptionFallbacks to*
meta_classclass Meta: (e.g. in Django projects)nested_class
nested_classOther nested classesNone*
constantSOME_CONSTANTSfield
outer_fieldsome = models.ForeignKey etc.field
fieldOther fieldsNone
protected fieldOther field starting with _field
private fieldOther field starting with __field
__new____new__magic_method
__init____init__magic_method
__post_init____post_init__magic_method
__str____str__magic_method
magic_methodOther magic methodsmethod
savedef save(...)method
deletedef delete(...)method
property_method@property/@cached_property etc.method
protected_property_method@property/@cached_property etc. with _property_method
private_property_method@property/@cached_property with __property_method
static_method@staticmethodmethod
protected_static_method@staticmethod beginning with _static_method
private_static_method@staticmethod beginning with __static_method
class_method@classmethodmethod
protected_class_method@classmethod beginning with _class_method
private_class_method@classmethod beginning with __class_method
private_methodother methods beginning with __method
protected_methodother methods beginning with _method
methodother methodsNone

* if not provided, will use its supertype order

** if not defined, such base types and all their subtypes (unless defined) will be ignored during validation. It's recommended to set at least nested_class, field and method

You choose how detailed your configuration is. For example, you can define order of each supported magic method (__new__, __str__, etc.), or set magic_method to allow any order among them or even just use method

Example

DEBUG = True


class User:
    def fetch_info_from_crm(self):
        pass

    LOGIN_FIELD = 'email'  # wtf? this should be on top of class definition!


class UserNode:
    class Meta:
        model = User

    if DEBUG:  # not great idea at all
        def is_synced_with_crm(self):
            pass

Usage:

$ flake8 test.py
test.py:5:5: CCE001 User.fetch_info_from_crm should be after User.LOGIN_FIELD
test.py:15:5: CCE002 Class level expression detected model UserNode, line 15

Tested on Python 3.7.x and flake8 3.7.5.

Error codes

Error codeDescription
CCE001Wrong class attributes order (XXX should be after YYY)
CCE002Class level expression detected

Contributing

We would love you to contribute to our project. It's simple:

Here are useful tips: