A flake8 extension that checks classes attributes order
Project description
flake8-class-attributes-order
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:
__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-class-attributes-order
Configuration
Strict mode
There is another preconfigured order that is more strict on private subtypes:
__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:
[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,
private_method
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
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:
- Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
- Create a pull request. Make sure all checks are green.
- Fix review comments if any.
- 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.
- We respect Django CoC. Make soft, not bullshit.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_class_attributes_order-0.1.3.tar.gz
.
File metadata
- Download URL: flake8_class_attributes_order-0.1.3.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9dde5581832d278ce247d4c47777f980cd86df2232bc0c50275144857d56e91d |
|
MD5 | 1474bdbfaa3058d7c977d7e91a78aaa8 |
|
BLAKE2b-256 | 48864d6eb75d7108ea84a72d5100da5bdb23165938cc0daae83e291f5fc51dd3 |
File details
Details for the file flake8_class_attributes_order-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: flake8_class_attributes_order-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6398368da4511cdda1a7c0e25934bcaac49d9df89aa2842729353b4a20f7ed13 |
|
MD5 | 95e3d010ca3ded23582c3e69643054a9 |
|
BLAKE2b-256 | 0e057cecfad1708692d618eea8570804b86ec89a9e8570898b5a2637851bdf02 |