diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b4be03742..1b1793139 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,7 +73,7 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt - pip install pycodestyle coverage tblib + pip install ruff coverage tblib - name: Build documentation run: mkdocs build @@ -85,7 +85,7 @@ jobs: run: python netbox/manage.py makemigrations --check - 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 run: yarn --cwd netbox/project-static validate diff --git a/docs/development/getting-started.md b/docs/development/getting-started.md index 4dbdb63b2..374ed34b0 100644 --- a/docs/development/getting-started.md +++ b/docs/development/getting-started.md @@ -70,10 +70,10 @@ NetBox ships with a [git pre-commit hook](https://githooks.com/) script that aut cd .git/hooks/ 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 -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) diff --git a/docs/development/style-guide.md b/docs/development/style-guide.md index 9e5606749..a41fee075 100644 --- a/docs/development/style-guide.md +++ b/docs/development/style-guide.md @@ -1,6 +1,6 @@ # 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 @@ -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`. -### 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: * The library being import contains only constant declarations (e.g. `constants.py`) * 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). - -#### 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/ -``` +The justification for ignoring this rule is the same as F403 above. ### Introducing New Dependencies diff --git a/scripts/git-hooks/pre-commit b/scripts/git-hooks/pre-commit index 2ccf8df89..c7d968f0a 100755 --- a/scripts/git-hooks/pre-commit +++ b/scripts/git-hooks/pre-commit @@ -28,8 +28,8 @@ if [ ${NOVALIDATE} ]; then exit $EXIT fi -echo "Validating PEP8 compliance..." -pycodestyle --ignore=W504,E501 --exclude=node_modules netbox/ +echo "Linting with ruff..." +ruff check netbox/ --ignore E501,F403,F405 if [ $? != 0 ]; then EXIT=1 fi