mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-02 05:46:25 -06:00
Replace pycodestyle with ruff
This commit is contained in:
parent
89d490cc69
commit
3758391ecd
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -73,7 +73,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirements.txt
|
pip install -r requirements.txt
|
||||||
pip install pycodestyle coverage tblib
|
pip install ruff coverage tblib
|
||||||
|
|
||||||
- name: Build documentation
|
- name: Build documentation
|
||||||
run: mkdocs build
|
run: mkdocs build
|
||||||
@ -85,7 +85,7 @@ jobs:
|
|||||||
run: python netbox/manage.py makemigrations --check
|
run: python netbox/manage.py makemigrations --check
|
||||||
|
|
||||||
- name: Check PEP8 compliance
|
- name: Check PEP8 compliance
|
||||||
run: pycodestyle --ignore=W504,E501 --exclude=node_modules netbox/
|
run: ruff check netbox/ --ignore E501,F403,F405
|
||||||
|
|
||||||
- name: Check UI ESLint, TypeScript, and Prettier Compliance
|
- name: Check UI ESLint, TypeScript, and Prettier Compliance
|
||||||
run: yarn --cwd netbox/project-static validate
|
run: yarn --cwd netbox/project-static validate
|
||||||
|
@ -70,10 +70,10 @@ NetBox ships with a [git pre-commit hook](https://githooks.com/) script that aut
|
|||||||
cd .git/hooks/
|
cd .git/hooks/
|
||||||
ln -s ../../scripts/git-hooks/pre-commit
|
ln -s ../../scripts/git-hooks/pre-commit
|
||||||
```
|
```
|
||||||
For the pre-commit hooks to work, you will also need to install the pycodestyle package:
|
For the pre-commit hooks to work, you will also need to install the [ruff](https://docs.astral.sh/ruff/) linter:
|
||||||
|
|
||||||
```no-highlight
|
```no-highlight
|
||||||
python -m pip install pycodestyle
|
python -m pip install ruff
|
||||||
```
|
```
|
||||||
...and set up the yarn packages as shown in the [Web UI Development Guide](web-ui.md)
|
...and set up the yarn packages as shown in the [Web UI Development Guide](web-ui.md)
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
# Style Guide
|
# Style Guide
|
||||||
|
|
||||||
NetBox generally follows the [Django style guide](https://docs.djangoproject.com/en/stable/internals/contributing/writing-code/coding-style/), which is itself based on [PEP 8](https://www.python.org/dev/peps/pep-0008/). [Pycodestyle](https://github.com/pycqa/pycodestyle) is used to validate code formatting, ignoring certain violations.
|
NetBox generally follows the [Django style guide](https://docs.djangoproject.com/en/stable/internals/contributing/writing-code/coding-style/), which is itself based on [PEP 8](https://www.python.org/dev/peps/pep-0008/). [ruff](https://docs.astral.sh/ruff/) is used for linting (with certain [exceptions](#linter-exceptions)).
|
||||||
|
|
||||||
## Code
|
## Code
|
||||||
|
|
||||||
@ -20,32 +20,32 @@ NetBox generally follows the [Django style guide](https://docs.djangoproject.com
|
|||||||
|
|
||||||
* Nested API serializers generate minimal representations of an object. These are stored separately from the primary serializers to avoid circular dependencies. Always import nested serializers from other apps directly. For example, from within the DCIM app you would write `from ipam.api.nested_serializers import NestedIPAddressSerializer`.
|
* Nested API serializers generate minimal representations of an object. These are stored separately from the primary serializers to avoid circular dependencies. Always import nested serializers from other apps directly. For example, from within the DCIM app you would write `from ipam.api.nested_serializers import NestedIPAddressSerializer`.
|
||||||
|
|
||||||
### PEP 8 Exceptions
|
### Linting
|
||||||
|
|
||||||
NetBox ignores certain PEP8 assertions. These are listed below.
|
The [ruff](https://docs.astral.sh/ruff/) linter is used to enforce code style. A [pre-commit hook](./getting-started.md#3-enable-pre-commit-hooks) which runs this automatically is included with NetBox. To invoke `ruff` manually, run:
|
||||||
|
|
||||||
#### Wildcard Imports
|
```
|
||||||
|
ruff check netbox/ --ignore E501,F403,F405
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Linter Exceptions
|
||||||
|
|
||||||
|
The following rules are ignored when linting.
|
||||||
|
|
||||||
|
##### [E501](https://docs.astral.sh/ruff/rules/line-too-long/): Line too long
|
||||||
|
|
||||||
|
NetBox does not enforce a hard restriction on line length, although a maximum length of 120 characters is strongly encouraged for Python code where possible. The maximum length does not apply to HTML templates or to automatically generated code (e.g. database migrations).
|
||||||
|
|
||||||
|
##### [F403](https://docs.astral.sh/ruff/rules/undefined-local-with-import-star/): Undefined local with import star
|
||||||
|
|
||||||
Wildcard imports (for example, `from .constants import *`) are acceptable under any of the following conditions:
|
Wildcard imports (for example, `from .constants import *`) are acceptable under any of the following conditions:
|
||||||
|
|
||||||
* The library being import contains only constant declarations (e.g. `constants.py`)
|
* The library being import contains only constant declarations (e.g. `constants.py`)
|
||||||
* The library being imported explicitly defines `__all__`
|
* The library being imported explicitly defines `__all__`
|
||||||
|
|
||||||
#### Maximum Line Length (E501)
|
##### [F405](https://docs.astral.sh/ruff/rules/undefined-local-with-import-star-usage/): Undefined local with import star usage
|
||||||
|
|
||||||
NetBox does not restrict lines to a maximum length of 79 characters. We use a maximum line length of 120 characters, however this is not enforced by CI. The maximum length does not apply to HTML templates or to automatically generated code (e.g. database migrations).
|
The justification for ignoring this rule is the same as F403 above.
|
||||||
|
|
||||||
#### Line Breaks Following Binary Operators (W504)
|
|
||||||
|
|
||||||
Line breaks are permitted following binary operators.
|
|
||||||
|
|
||||||
### Enforcing Code Style
|
|
||||||
|
|
||||||
The [`pycodestyle`](https://pypi.org/project/pycodestyle/) utility (formerly `pep8`) is used by the CI process to enforce code style. A [pre-commit hook](./getting-started.md#3-enable-pre-commit-hooks) which runs this automatically is included with NetBox. To invoke `pycodestyle` manually, run:
|
|
||||||
|
|
||||||
```
|
|
||||||
pycodestyle --ignore=W504,E501 netbox/
|
|
||||||
```
|
|
||||||
|
|
||||||
### Introducing New Dependencies
|
### Introducing New Dependencies
|
||||||
|
|
||||||
|
@ -28,8 +28,8 @@ if [ ${NOVALIDATE} ]; then
|
|||||||
exit $EXIT
|
exit $EXIT
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "Validating PEP8 compliance..."
|
echo "Linting with ruff..."
|
||||||
pycodestyle --ignore=W504,E501 --exclude=node_modules netbox/
|
ruff check netbox/ --ignore E501,F403,F405
|
||||||
if [ $? != 0 ]; then
|
if [ $? != 0 ]; then
|
||||||
EXIT=1
|
EXIT=1
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user