Skip to main content

flake8-class-attributes-order

Build Status Maintainability Test Coverage

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.

Here are methods order for non strict validation:

  • new
  • init
  • post_init
  • other magic methods
  • @property
  • @staticmethod
  • @classmethod
  • other methods
  • private methods

And here are methods order if you have strict validation enabled:

  • new
  • init
  • post_init
  • other magic method
  • @property
  • @staticmethod
  • @classmethod
  • other methods
  • private @property
  • private @staticmethod
  • private @classmethod
  • other private methods

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

use_class_attributes_order_strict_mode = True

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())
    phone.widget.attrs.update({'class': 'form-control phone'})  # this should happens in __init__!

Installation

pip install flake8-class-attributes-order

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 code Description
CCE001 Wrong class attributes order (XXX should be after YYY)
CCE002 Class level expression detected

Contributing

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

  1. Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
  2. Create a pull request. Make sure all checks are green.
  3. Fix review comments if any.
  4. Be awesome.

Here are useful tips:

  • You can run all checks and tests with make check. Please do it before TravisCI does.
  • We use BestDoctor python styleguide. Sorry, styleguide is available only in Russian for now.
  • We respect Django CoC. Make soft, not bullshit.

Metadata

Release files for flake8-class-attributes-order 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-class-attributes-order 0.0.6
File Size Uploaded
flake8_class_attributes_order-0.0.6.tar.gz 5.4 kB Details

Release files / flake8_class_attributes_order-0.0.6.tar.gz

Download URL flake8_class_attributes_order-0.0.6.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e79215345e3b3feeee39318c740da7f4512d4456c03241470428f8bfb8a50acb
BLAKE2b-256 checksum
How to use checksums
5346e3cbec716bb5877aaf71c2bcb1d0ee40a078cbd2739934a75fa013fee15d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

Release history Release notifications | RSS feed

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.7

1 release file

This release

0.0.6 This release

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page