Skip to main content

flake8-function-order

An extension for flake8 to report on wrong class attributes order and class level logic, adapted from the flake8-class-attributes-order package by @best-doctor.

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:

  • __new__
  • __init__
  • __post_init__
  • other magic methods
  • @property
  • @staticmethod
  • @classmethod
  • other methods
  • private methods

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-function-order

Configurable options:

Option Description Fallbacks to*
meta_class class Meta: (e.g. in Django projects) nested_class
nested_class Other nested classes None*
constant SOME_CONSTANTS field
outer_field some = models.ForeignKey etc. field
field Other fields None
__new__ __new__ magic_method
__init__ __init__ magic_method
__post_init__ __post_init__ magic_method
__str__ __str__ magic_method
magic_method Other magic methods method
save def save(...) method
delete def delete(...) method
property_method @property/@cached_property etc. method
private_property_method @property/@cached_property with _ property_method
static_method @staticmethod method
private_static_method @staticmethod beginning with _ static_method
class_method @classmethod method
private_class_method @classmethod beginning with _ class_method
private_method other methods beginning with _ method
method other methods None

* 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

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

Metadata

Release files for flake8-function-order 0.0.5

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-function-order 0.0.5
File Size Uploaded
flake8_function_order-0.0.5.tar.gz 6.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flake8-function-order 0.0.5
File Interpreter ABI Platform
flake8_function_order-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 14.7 kB

Release files / flake8_function_order-0.0.5.tar.gz

Download URL flake8_function_order-0.0.5.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
16185e1a126ddc7767add8e60616859e3364208fac3650b1c2c624f05c5e39dd
BLAKE2b-256 checksum
How to use checksums
a651b01a5657280ffcb183f1bb0d1ce2a6ec70a8e05d18d761e68eba13c521fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

Release files / flake8_function_order-0.0.5-py3-none-any.whl

Download URL flake8_function_order-0.0.5-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58fd9cdf5584894318dd3949ce51b4000f28810adc053cb092f10911ffe9ec8e
BLAKE2b-256 checksum
How to use checksums
bd87f5da17988142237190d9e22437aee5adeddc7e97cdf6da9096b878ffece5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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