Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use isort in MegaLinter #872

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ a3b18cfbf32a2f0e6a9c55ead043fea12a71883e
17c099c3b99172f4f623f22ddc374abcf692ee5d
# Re-run black with new version 24.8.0 @johannaengland 29/08/2024
10dc8b7b612dddb042462718eea5b62f9d6455ea
# Re-format files using isort @johannaengland 30/08/2024
975ab295654ef926379c9cfaef2017e1367e4877
45 changes: 45 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
# MegaLinter GitHub Action configuration file
# More info at https://megalinter.io
name: MegaLinter
on:
push:
branches: "master"
pull_request:

jobs:
megalinter:
name: MegaLinter
runs-on: ubuntu-latest
permissions:
# Give the linter write permission to comment on PRs (if PR is not from fork)
issues: write
pull-requests: write
steps:
# Git Checkout
- name: Checkout Code
uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
# MegaLinter
- name: MegaLinter
id: ml
# You can override MegaLinter flavor used to have faster performances
# More info at https://megalinter.io/flavors/
uses: oxsecurity/megalinter/flavors/python@v7
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Validate whole codebase on pushes and only changes on pull requests
# VALIDATE_ALL_CODEBASE: ${{ github.event_name == 'push'}}
VALIDATE_ALL_CODEBASE: true

# Upload MegaLinter artifacts
- name: Archive production artifacts
if: success() || failure()
uses: actions/upload-artifact@v4
with:
name: MegaLinter reports
path: |
megalinter-reports
mega-linter.log
29 changes: 0 additions & 29 deletions .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,7 @@ on:
pull_request:

jobs:
lint:
runs-on: ubuntu-latest
name: Lint
steps:
- uses: actions/checkout@v4

- uses: actions/cache@v4
id: cache
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install dependencies
run: |
pip install -U pip
pip install tox flake8

- name: flake8 critical lint
run: tox -e flake8-critical

test:
needs: lint

name: "Python ${{ matrix.python-version }}"
runs-on: ubuntu-latest

Expand Down
18 changes: 18 additions & 0 deletions .mega-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# All available variables are described in documentation
# https://megalinter.io/configuration/

FILTER_REGEX_EXCLUDE: "migrations"

LINTER_RULES_PATH: .

ENABLE_LINTERS:
- PYTHON_BLACK
- PYTHON_ISORT
- PYTHON_RUFF

# Make workflow fail even on non blocking errors
FORMATTERS_DISABLE_ERRORS: false

PYTHON_BLACK_CONFIG_FILE: pyproject.toml
PYTHON_ISORT_CONFIG_FILE: pyproject.toml
PYTHON_RUFF_CONFIG_FILE: pyproject.toml
14 changes: 8 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ repos:
- id: end-of-file-fixer
exclude: &exclude_pattern '^changelog.d/'
- id: debug-statements
- repo: https://github.com/pycqa/flake8
rev: 7.1.1
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: flake8
name: "Flake8: critical"
args: ['--count', '--select=E9,F63,F7,F82', '--show-source', '--statistics']
types: [file, python]
- id: isort
name: isort (python)
- repo: https://github.com/psf/black
rev: 24.8.0
hooks:
- id: black
exclude: migrations/
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.1
hooks:
- id: ruff
- repo: https://github.com/twisted/towncrier
rev: 24.8.0
hooks:
Expand Down
24 changes: 22 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ dev = [
"black",
"coverage",
"django-extensions",
"flake8",
"ipython",
"isort",
"pre-commit",
"python-dotenv",
"ruff",
"towncrier",
"werkzeug",
"tox<4", # does not work on tox 4 for some reason
Expand Down Expand Up @@ -89,7 +90,8 @@ write_to = "src/argus/version.py"

[tool.black]
line-length = 120
exclude = '''
# Exclude files even when passed directly as argument (for MegaLinter)
force-exclude = '''
(
/(
\.eggs # exclude a few common directories in the
Expand All @@ -108,6 +110,24 @@ exclude = '''
)
'''

[tool.ruff]
line-length = 120
target-version = "py39"
exclude = [
"src/**/migrations"
]
# Exclude files even when passed directly as argument (for MegaLinter)
force-exclude = true

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]
ignore = ["E731", "F403", "F405"]

[tool.isort]
profile = "black"
src_paths = ["src", "tests"]
extend_skip_glob = ["src/**/migrations"]

[tool.towncrier]
directory = "changelog.d"
filename = "CHANGELOG.md"
Expand Down
4 changes: 3 additions & 1 deletion src/argus/auth/V1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from rest_framework import serializers
from typing import List

from rest_framework import serializers

from argus.notificationprofile.models import DestinationConfig

from ..models import User


Expand Down
3 changes: 1 addition & 2 deletions src/argus/auth/V1/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from django.urls import path
from rest_framework import routers

from . import views as views_V1
from .. import views

from . import views as views_V1

router = routers.SimpleRouter()
router.register(r"phone-number", views_V1.PhoneNumberViewV1, basename="phone_number")
Expand Down
7 changes: 6 additions & 1 deletion src/argus/auth/V1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
from argus.drf.permissions import IsOwner
from argus.notificationprofile.models import DestinationConfig
from argus.notificationprofile.serializers import RequestDestinationConfigSerializer
from .serializers import RequestPhoneNumberSerializerV1, ResponsePhoneNumberSerializerV1, UserSerializerV1

from .serializers import (
RequestPhoneNumberSerializerV1,
ResponsePhoneNumberSerializerV1,
UserSerializerV1,
)


class CurrentUserViewV1(APIView):
Expand Down
3 changes: 2 additions & 1 deletion src/argus/auth/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

from .models import User
from argus.notificationprofile.models import DestinationConfig

from .models import User


class DestinationConfigInline(admin.TabularInline):
model = DestinationConfig
Expand Down
1 change: 0 additions & 1 deletion src/argus/auth/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from rest_framework import serializers
from rest_framework.reverse import reverse


from .models import User


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

from . import views


router = routers.SimpleRouter()


Expand Down
1 change: 0 additions & 1 deletion src/argus/auth/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.utils.module_loading import import_string

from rest_framework.reverse import reverse
from social_core.backends.base import BaseAuth
from social_core.backends.oauth import BaseOAuth2
Expand Down
12 changes: 8 additions & 4 deletions src/argus/auth/views.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from django.contrib.auth import logout
from django.conf import settings
from django.contrib.auth import logout
from django.db import transaction

from drf_spectacular.utils import extend_schema, extend_schema_view
from rest_framework import generics
from rest_framework.authtoken.models import Token
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.exceptions import ValidationError
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import User
from .serializers import BasicUserSerializer, EmptySerializer, RefreshTokenSerializer, UserSerializer
from .serializers import (
BasicUserSerializer,
EmptySerializer,
RefreshTokenSerializer,
UserSerializer,
)
from .utils import get_authentication_backend_name_and_type


Expand Down
1 change: 0 additions & 1 deletion src/argus/compat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from enum import Enum


__all__ = ["StrEnum"]


Expand Down
1 change: 0 additions & 1 deletion src/argus/dataporten/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.conf import settings
from django.shortcuts import redirect

from rest_framework.authtoken.models import Token
from social_django import views as social_views

Expand Down
4 changes: 2 additions & 2 deletions src/argus/dev/management/commands/check_token_expiry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from rest_framework.authtoken.models import Token

from argus.incident.models import (
create_token_expiry_incident,
get_or_create_default_instances,
Incident,
Tag,
create_token_expiry_incident,
get_or_create_default_instances,
)


Expand Down
3 changes: 1 addition & 2 deletions src/argus/dev/management/commands/create_fake_incident.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import argparse
import json
from pathlib import Path
from random import randint

from django.core.management.base import BaseCommand

from argus.incident.constants import MIN_INCIDENT_LEVEL, MAX_INCIDENT_LEVEL
from argus.incident.constants import MAX_INCIDENT_LEVEL, MIN_INCIDENT_LEVEL
from argus.incident.models import create_fake_incident


Expand Down
2 changes: 1 addition & 1 deletion src/argus/dev/management/commands/create_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ def handle(self, *args, **options):
source = options["source"]
source_type = options.get("source_type") or "argus"
sst = SourceSystemTypeFactory(name=source_type)
ss = SourceSystemFactory(name=source, type=sst)
SourceSystemFactory(name=source, type=sst)
1 change: 0 additions & 1 deletion src/argus/dev/management/commands/list_filters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from django.contrib.auth.hashers import check_password
from django.core.management.base import BaseCommand

from argus.filter import get_filter_backend
Expand Down
5 changes: 2 additions & 3 deletions src/argus/dev/management/commands/stresstest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from httpx import TimeoutException, HTTPStatusError, HTTPError

from django.core.management.base import BaseCommand
from httpx import HTTPError, HTTPStatusError, TimeoutException

from argus.dev.utils import StressTester, DatabaseMismatchError
from argus.dev.utils import DatabaseMismatchError, StressTester


class Command(BaseCommand):
Expand Down
8 changes: 4 additions & 4 deletions src/argus/dev/utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime, timedelta
from urllib.parse import urljoin
import asyncio
import itertools
from typing import Any, Dict, AnyStr, List, Tuple
from datetime import datetime, timedelta
from typing import Any, AnyStr, Dict, List, Tuple
from urllib.parse import urljoin

from httpx import AsyncClient, TimeoutException, HTTPStatusError, post
from httpx import AsyncClient, HTTPStatusError, TimeoutException, post


class DatabaseMismatchError(Exception):
Expand Down
3 changes: 0 additions & 3 deletions src/argus/filter/V1/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import json
from typing import List

from rest_framework import fields, serializers
from rest_framework import serializers

from argus.incident.constants import INCIDENT_LEVELS
from argus.notificationprofile.models import Filter

from ..primitive_serializers import CustomMultipleChoiceField
from .validators import validate_filter_string


Expand Down
1 change: 0 additions & 1 deletion src/argus/filter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.conf import settings


DEFAULT_BACKEND = "argus.filter.default"


Expand Down
2 changes: 0 additions & 2 deletions src/argus/filter/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.apps import AppConfig
from django.contrib.auth.signals import user_logged_in
from django.core.checks import register
from django.db.models.signals import post_save, pre_save, post_migrate


class FilterConfig(AppConfig):
Expand Down
Loading
Loading