Skip to content

Commit

Permalink
fully integrate ruff for formatting and linting (#14)
Browse files Browse the repository at this point in the history
We had already introduced ruff as a replacement for pydocstyle. This
this commit, we also replace flake8 and black with it.

It was decided not to remove bandit and pylint because they still have
rules that are unsupported in ruff. Furthermore, all bandit and pylint
rules will remain active because the performance improvement was tiny.
The following script can theoretically extract all pylint messages that
are unsupported in ruff:

```python
from bs4 import BeautifulSoup
import requests

response = requests.get("astral-sh/ruff#970 (comment)")

soup = BeautifulSoup(response.text, "html.parser")

for li in soup.find_all("li", class_="task-list-item"):
    if "checked" in li.find("input").attrs:
        continue
    print(li.find_all("code")[0].get_text(), end="")
    print(",")
```
  • Loading branch information
Kircheneer committed Apr 11, 2024
1 parent f8695cc commit 57862d4
Show file tree
Hide file tree
Showing 17 changed files with 37 additions and 193 deletions.
10 changes: 0 additions & 10 deletions .flake8

This file was deleted.

23 changes: 5 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ env:
APP_NAME: "nautobot-app-dev-example"

jobs:
black:
ruff-format:
runs-on: "ubuntu-22.04"
env:
INVOKE_NAUTOBOT_DEV_EXAMPLE_LOCAL: "True"
Expand All @@ -25,8 +25,8 @@ jobs:
uses: "actions/checkout@v4"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Linting: black"
run: "poetry run invoke black"
- name: "Linting: ruff format"
run: "poetry run invoke ruff --action format"
bandit:
runs-on: "ubuntu-22.04"
env:
Expand All @@ -38,7 +38,7 @@ jobs:
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Linting: bandit"
run: "poetry run invoke bandit"
ruff:
ruff-lint:
runs-on: "ubuntu-22.04"
env:
INVOKE_NAUTOBOT_DEV_EXAMPLE_LOCAL: "True"
Expand All @@ -48,7 +48,7 @@ jobs:
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Linting: ruff"
run: "poetry run invoke ruff"
run: "poetry run invoke ruff --action lint"
check-docs-build:
runs-on: "ubuntu-22.04"
env:
Expand All @@ -60,17 +60,6 @@ jobs:
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Check Docs Build"
run: "poetry run invoke build-and-check-docs"
flake8:
runs-on: "ubuntu-22.04"
env:
INVOKE_NAUTOBOT_DEV_EXAMPLE_LOCAL: "True"
steps:
- name: "Check out repository code"
uses: "actions/checkout@v4"
- name: "Setup environment"
uses: "networktocode/gh-action-setup-poetry-environment@v6"
- name: "Linting: flake8"
run: "poetry run invoke flake8"
poetry:
runs-on: "ubuntu-22.04"
env:
Expand All @@ -97,10 +86,8 @@ jobs:
needs:
- "bandit"
- "ruff"
- "flake8"
- "poetry"
- "yamllint"
- "black"
runs-on: "ubuntu-22.04"
strategy:
fail-fast: true
Expand Down
1 change: 1 addition & 0 deletions changes/14.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fully integrated ruff as a (partial) replacement for the other linters.
4 changes: 2 additions & 2 deletions development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Debug
#

DEBUG = is_truthy(os.getenv("NAUTOBOT_DEBUG", False))
DEBUG = is_truthy(os.getenv("NAUTOBOT_DEBUG", "false"))
_TESTING = len(sys.argv) > 1 and sys.argv[1] == "test"

if DEBUG and not _TESTING:
Expand Down Expand Up @@ -50,7 +50,7 @@
"PORT": os.getenv(
"NAUTOBOT_DB_PORT", default_db_settings[nautobot_db_engine]["NAUTOBOT_DB_PORT"]
), # Database port, default to postgres
"CONN_MAX_AGE": int(os.getenv("NAUTOBOT_DB_TIMEOUT", 300)), # Database timeout
"CONN_MAX_AGE": int(os.getenv("NAUTOBOT_DB_TIMEOUT", "300")), # Database timeout
"ENGINE": nautobot_db_engine,
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/dev/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The project is packaged with a light [development environment](dev_environment.m

The project is following Network to Code software development guidelines and is leveraging the following:

- Python linting and formatting: `black`, `pylint`, `bandit`, `flake8`, and `ruff`.
- Python linting and formatting: `pylint`, `bandit`, and `ruff`.
- YAML linting is done with `yamllint`.
- Django unit test to ensure the app is working properly.

Expand Down
8 changes: 2 additions & 6 deletions docs/dev/dev_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ Each command can be executed with `invoke <command>`. All commands support the a

```
bandit Run bandit to validate basic static code security analysis.
black Run black to check that Python files adhere to its style standards.
flake8 Run flake8 to check that Python files adhere to its style standards.
ruff Run ruff to validate docstring formatting adheres to NTC defined standards.
ruff Run ruff to perform code formatting and/or linting.
pylint Run pylint code analysis.
tests Run all tests for this app.
unittest Run Django unit tests for the app.
Expand Down Expand Up @@ -454,7 +452,7 @@ This is the same as running:

### Tests

To run tests against your code, you can run all of the tests that TravisCI runs against any new PR with:
To run tests against your code, you can run all of the tests that the CI runs against any new PR with:

```bash
➜ invoke tests
Expand All @@ -465,8 +463,6 @@ To run an individual test, you can run any or all of the following:
```bash
➜ invoke unittest
➜ invoke bandit
➜ invoke black
➜ invoke flake8
➜ invoke ruff
➜ invoke pylint
```
Expand Down
2 changes: 1 addition & 1 deletion nautobot_dev_example/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Filtering for nautobot_dev_example."""

from nautobot.apps.filters import NautobotFilterSet, NameSearchFilterSet
from nautobot.apps.filters import NameSearchFilterSet, NautobotFilterSet

from nautobot_dev_example import models

Expand Down
5 changes: 3 additions & 2 deletions nautobot_dev_example/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Generated by Django 3.2.21 on 2024-02-16 08:32

import uuid

import django.core.serializers.json
from django.db import migrations, models
import nautobot.core.models.fields
import nautobot.extras.models.mixins
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
1 change: 0 additions & 1 deletion nautobot_dev_example/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# Nautobot imports
from nautobot.apps.models import PrimaryModel


# from nautobot.extras.utils import extras_features
# If you want to use the extras_features decorator please reference the following documentation
# https://docs.nautobot.com/projects/core/en/latest/plugins/development/#using-the-extras_features-decorator-for-graphql
Expand Down
1 change: 0 additions & 1 deletion nautobot_dev_example/navigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from nautobot.apps.ui import NavMenuAddButton, NavMenuGroup, NavMenuItem, NavMenuTab


items = (
NavMenuItem(
link="plugins:nautobot_dev_example:devexample_list",
Expand Down
3 changes: 1 addition & 2 deletions nautobot_dev_example/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse
from nautobot.users.models import Token
from rest_framework import status
from rest_framework.test import APIClient

from nautobot.users.models import Token

User = get_user_model()


Expand Down
8 changes: 5 additions & 3 deletions nautobot_dev_example/tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""Basic tests that do not require Django."""

import unittest
import os
import unittest

import toml


Expand All @@ -16,8 +17,9 @@ def test_version(self):
with open(f"{parent_path}/docs/requirements.txt", "r", encoding="utf-8") as file:
requirements = [line for line in file.read().splitlines() if (len(line) > 0 and not line.startswith("#"))]
for pkg in requirements:
if len(pkg.split("==")) == 2:
pkg, version = pkg.split("==")
package_name = pkg
if len(pkg.split("==")) == 2: # noqa: PLR2004
package_name, version = pkg.split("==")
else:
version = "*"
self.assertEqual(poetry_details[pkg], version)
4 changes: 2 additions & 2 deletions nautobot_dev_example/tests/test_filter_devexample.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test DevExample Filter."""

from django.test import TestCase
from nautobot_dev_example import filters
from nautobot_dev_example import models

from nautobot_dev_example import filters, models
from nautobot_dev_example.tests import fixtures


Expand Down
1 change: 0 additions & 1 deletion nautobot_dev_example/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from nautobot_dev_example import views


router = NautobotUIViewSetRouter()
router.register("devexample", views.DevExampleUIViewSet)

Expand Down
86 changes: 1 addition & 85 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 57862d4

Please sign in to comment.