# flake8-class-attributes-order
[](https://github.com/best-doctor/flake8-class-attributes-order/actions/workflows/build.yml)
[](https://codeclimate.com/github/best-doctor/flake8-class-attributes-order/maintainability)
[](https://codeclimate.com/github/best-doctor/flake8-class-attributes-order/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.
Default configuration checks for the following order of attributes:
- `__new__`
- `__init__`
- `__post_init__`
- other magic methods
- `@property`
- `@staticmethod`
- `@classmethod`
- other methods
- protected 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:
```python
class PhoneForm(forms.Form):
phone = forms.CharField(17, label='Телефон'.upper())
# this should happen in __init__!
phone.widget.attrs.update({'class': 'form-control phone'})
```
## Installation
```terminal
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
- protected `@property`
- protected `@staticmethod`
- protected `@classmethod`
- other protected methods
- private `@property`
- private `@staticmethod`
- private `@classmethod`
- other private methods
To enable strict validation, please set the flag in your config file:
```ini
[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:
```ini
[flake8]
class_attributes_order =
field,
meta_class,
nested_class,
magic_method,
property_method,
static_method,
class_method,
method,
protected_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 |
| protected field | Other field starting with _ | field |
| private field | Other field starting with __ | field |
| `__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 |
| protected_property_method | @property/@cached_property etc. with _ | property_method |
| private_property_method | @property/@cached_property with __ | property_method |
| static_method | @staticmethod | method |
| protected_static_method | @staticmethod beginning with _ | static_method |
| private_static_method | @staticmethod beginning with __ | static_method |
| class_method | @classmethod | method |
| protected_class_method | @classmethod beginning with _ | class_method |
| private_class_method | @classmethod beginning with __ | class_method |
| private_method | other methods beginning with __ | method |
| protected_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
```python
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:
```terminal
$ 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.9.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](https://github.com/best-doctor/guides/blob/master/guides/en/python_styleguide.md).
- We respect [Django CoC](https://www.djangoproject.com/conduct/).
Make soft, not bullshit.
Raw data
{
"_id": null,
"home_page": "https://github.com/best-doctor/flake8-class-attributes-order",
"name": "flake8-class-attributes-order",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "flake8",
"author": "Ilya Lebedev",
"author_email": "melevir@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/57/e1/b466e241e97c163ea082c549cdf02817380be01014477c56bf3cbd5946d3/flake8_class_attributes_order-0.3.0.tar.gz",
"platform": null,
"description": "# flake8-class-attributes-order\n\n[](https://github.com/best-doctor/flake8-class-attributes-order/actions/workflows/build.yml)\n[](https://codeclimate.com/github/best-doctor/flake8-class-attributes-order/maintainability)\n[](https://codeclimate.com/github/best-doctor/flake8-class-attributes-order/test_coverage)\n\n\nAn extension for flake8 to report on wrong class attributes order and\nclass level logic.\n\nThe validator can extract class attribute type: docstring, property,\nnested class, `GLOBAL_VAR`, etc.\nIf django model fields are detected, the validator can detect,\nif the field is link to another table (foreign key, generic key, etc)\nor not.\n\nAfter resolving each attribute type, validator checks attributes order.\n\nDefault configuration checks for the following order of attributes:\n\n- `__new__`\n- `__init__`\n- `__post_init__`\n- other magic methods\n- `@property`\n- `@staticmethod`\n- `@classmethod`\n- other methods\n- protected methods\n- private methods\n\nIf the order is broken, validator will report on it.\n\nBesides methods, the validator checks other attributes methods:\ndocstrings, nested classes, constants, attributes, and so on.\n\nAlso validator checks, if class has no class level logic and report\nif any. Here is an example:\n\n```python\nclass PhoneForm(forms.Form):\n phone = forms.CharField(17, label='\u0422\u0435\u043b\u0435\u0444\u043e\u043d'.upper())\n\n # this should happen in __init__!\n phone.widget.attrs.update({'class': 'form-control phone'})\n\n```\n\n## Installation\n\n```terminal\npip install flake8-class-attributes-order\n```\n\n## Configuration\n\n### Strict mode\n\nThere is another preconfigured order that is more strict on private subtypes:\n\n- `__new__`\n- `__init__`\n- `__post_init__`\n- other magic method\n- `@property`\n- `@staticmethod`\n- `@classmethod`\n- other methods\n- protected `@property`\n- protected `@staticmethod`\n- protected `@classmethod`\n- other protected methods\n- private `@property`\n- private `@staticmethod`\n- private `@classmethod`\n- other private methods\n\nTo enable strict validation, please set the flag in your config file:\n\n```ini\n[flake8]\nuse_class_attributes_order_strict_mode = True\n```\n\n### Manual order configuration\n\nOrder can be manually configured via `class_attributes_order` config setting.\n\nFor example, if you prefer to put `class Meta` after constants and fields:\n\n```ini\n[flake8]\nclass_attributes_order =\n field,\n meta_class,\n nested_class,\n magic_method,\n property_method,\n static_method,\n class_method,\n method,\n protected_method,\n private_method\n```\n\nConfigurable options:\n\n| Option | Description | Fallbacks to\\* |\n|:-------------------------:|:--------------------------------------:|:---------------:|\n| meta_class | class Meta: (e.g. in Django projects) | nested_class |\n| nested_class | Other nested classes | None\\* |\n| constant | SOME_CONSTANTS | field |\n| outer_field | some = models.ForeignKey etc. | field |\n| field | Other fields | None |\n| protected field | Other field starting with _ | field |\n| private field | Other field starting with __ | field |\n| `__new__` | `__new__` | magic_method |\n| `__init__` | `__init__` | magic_method |\n| `__post_init__` | `__post_init__` | magic_method |\n| `__str__` | `__str__` | magic_method |\n| magic_method | Other magic methods | method |\n| save | def save(...) | method |\n| delete | def delete(...) | method |\n| property_method | @property/@cached_property etc. | method |\n| protected_property_method | @property/@cached_property etc. with _ | property_method |\n| private_property_method | @property/@cached_property with __ | property_method |\n| static_method | @staticmethod | method |\n| protected_static_method | @staticmethod beginning with _ | static_method |\n| private_static_method | @staticmethod beginning with __ | static_method |\n| class_method | @classmethod | method |\n| protected_class_method | @classmethod beginning with _ | class_method |\n| private_class_method | @classmethod beginning with __ | class_method |\n| private_method | other methods beginning with __ | method |\n| protected_method | other methods beginning with _ | method |\n| method | other methods | None |\n\n\\* if not provided, will use its supertype order\n\n\\*\\* if not defined, such base types and all their subtypes (unless defined)\nwill be ignored during validation. It's recommended\nto set at least `nested_class`, `field` and `method`\n\nYou choose how detailed your configuration is.\nFor example, you can define order of each supported magic method\n(`__new__`, `__str__`, etc.), or set `magic_method`\nto allow any order among them or even just use `method`\n\n## Example\n\n```python\nDEBUG = True\n\n\nclass User:\n def fetch_info_from_crm(self):\n pass\n\n LOGIN_FIELD = 'email' # wtf? this should be on top of class definition!\n\n\nclass UserNode:\n class Meta:\n model = User\n\n if DEBUG: # not great idea at all\n def is_synced_with_crm(self):\n pass\n\n```\n\nUsage:\n\n```terminal\n$ flake8 test.py\ntest.py:5:5: CCE001 User.fetch_info_from_crm should be after User.LOGIN_FIELD\ntest.py:15:5: CCE002 Class level expression detected model UserNode, line 15\n```\n\nTested on Python 3.9.x and flake8 3.7.5.\n\n## Error codes\n\n| Error code | Description |\n|:----------:|:--------------------------------------------------------:|\n| CCE001 | Wrong class attributes order (`XXX should be after YYY`) |\n| CCE002 | Class level expression detected |\n\n## Contributing\n\nWe would love you to contribute to our project. It's simple:\n\n- Create an issue with bug you found or proposal you have.\n Wait for approve from maintainer.\n- Create a pull request. Make sure all checks are green.\n- Fix review comments if any.\n- Be awesome.\n\nHere are useful tips:\n\n- You can run all checks and tests with `make check`. Please do it\n before TravisCI does.\n- We use\n [BestDoctor python styleguide](https://github.com/best-doctor/guides/blob/master/guides/en/python_styleguide.md).\n- We respect [Django CoC](https://www.djangoproject.com/conduct/).\n Make soft, not bullshit.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A flake8 extension that checks classes attributes order",
"version": "0.3.0",
"project_urls": {
"Homepage": "https://github.com/best-doctor/flake8-class-attributes-order"
},
"split_keywords": [
"flake8"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "2cc0a3cd8ada22618643fb60cbd73b9f8c61d9fc0688130bdf41e6de51055fd8",
"md5": "4d50bc2a84d833ad14bccf1a8a0ebb32",
"sha256": "8bc901323de30fbf0d0ca62cf98610d79ccb861db89ec388249f95327d3afe67"
},
"downloads": -1,
"filename": "flake8_class_attributes_order-0.3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4d50bc2a84d833ad14bccf1a8a0ebb32",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 9959,
"upload_time": "2025-03-20T15:42:14",
"upload_time_iso_8601": "2025-03-20T15:42:14.723264Z",
"url": "https://files.pythonhosted.org/packages/2c/c0/a3cd8ada22618643fb60cbd73b9f8c61d9fc0688130bdf41e6de51055fd8/flake8_class_attributes_order-0.3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "57e1b466e241e97c163ea082c549cdf02817380be01014477c56bf3cbd5946d3",
"md5": "b29c7f82b0fc74ac4ccc7ec6105fc1c5",
"sha256": "ec5f6c8ffbab8c2fce71e04936418efbb3e36e58649752d646bbbf6902c35c44"
},
"downloads": -1,
"filename": "flake8_class_attributes_order-0.3.0.tar.gz",
"has_sig": false,
"md5_digest": "b29c7f82b0fc74ac4ccc7ec6105fc1c5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 11892,
"upload_time": "2025-03-20T15:42:16",
"upload_time_iso_8601": "2025-03-20T15:42:16.223535Z",
"url": "https://files.pythonhosted.org/packages/57/e1/b466e241e97c163ea082c549cdf02817380be01014477c56bf3cbd5946d3/flake8_class_attributes_order-0.3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-03-20 15:42:16",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "best-doctor",
"github_project": "flake8-class-attributes-order",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "flake8-class-attributes-order"
}