Skip to content

Commit

Permalink
Improve linting
Browse files Browse the repository at this point in the history
Problem:
Slow linters are used that have complicated configuration and expensive
to maintain.

Solution:
Introduce the new ruff linter which is super fast as it is written in
Rust. Also has options to emit suggested solutions and also to
automatically fix errors. This is just the introduction - eventually
this will be added to a commit hook and the github actions and
other linters will be removed.
Additionally added workaround for fixing the current version of the
github ubuntu-latest runner.

Signed-off-by: Paul Hewlett <[email protected]>
  • Loading branch information
eccles committed Mar 9, 2023
1 parent 896d531 commit d566dd9
Show file tree
Hide file tree
Showing 94 changed files with 494 additions and 492 deletions.
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"charliermarsh.ruff",
"ms-python.python",
"ms-python.vscode-pylance"
]
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
# https://github.com/community/community/discussions/47863
sudo apt-mark hold grub-efi-amd64-signed
sudo apt-get update
sudo apt-get upgrade -y --no-install-recommends
sudo apt-get install -y --fix-missing \
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
- name: Install dependencies
run: |
export DEBIAN_FRONTEND=noninteractive
# https://github.com/community/community/discussions/47863
sudo apt-mark hold grub-efi-amd64-signed
sudo apt-get update
sudo apt-get upgrade -y --no-install-recommends
sudo apt-get install -y --fix-missing \
Expand Down
13 changes: 13 additions & 0 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,23 @@ tasks:
deps: [about]
cmds:
- ./scripts/builder.sh python3 --version
- ./scripts/builder.sh ruff check archivist examples functests unittests
- ./scripts/builder.sh python3 -m pyright archivist
- ./scripts/builder.sh pycodestyle --format=pylint archivist examples functests unittests
- ./scripts/builder.sh python3 -m pylint archivist examples functests unittests

check-fixes:
desc: Show proposed fixes from ruff
deps: [about]
cmds:
- ./scripts/builder.sh ruff check --show-fixes archivist examples functests unittests

check-fixes-apply:
desc: Apply proposed fixes from ruff
deps: [about]
cmds:
- ./scripts/builder.sh ruff check --fix archivist examples functests unittests

clean:
desc: Clean git repo
cmds:
Expand Down
12 changes: 7 additions & 5 deletions archivist/access_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@
"""

from __future__ import annotations
from logging import getLogger

from copy import deepcopy
from typing import Any, Generator, Optional
from logging import getLogger
from typing import TYPE_CHECKING, Any, Generator, Optional

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist

if TYPE_CHECKING:
from . import archivist

from .assets import Asset
from .constants import (
ACCESS_POLICIES_SUBPATH,
ACCESS_POLICIES_LABEL,
ACCESS_POLICIES_SUBPATH,
ASSETS_LABEL,
)
from .dictmerge import _deepmerge


LOGGER = getLogger(__name__)


Expand Down
9 changes: 7 additions & 2 deletions archivist/appidp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,20 @@
"""

from __future__ import annotations

from logging import getLogger
from typing import TYPE_CHECKING

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
# pylint:disable=too-few-public-methods
from . import archivist

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
if TYPE_CHECKING:
from . import archivist

from .constants import (
APPIDP_SUBPATH,
APPIDP_LABEL,
APPIDP_SUBPATH,
APPIDP_TOKEN,
)

Expand Down
9 changes: 5 additions & 4 deletions archivist/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,21 @@
"""

from __future__ import annotations

from logging import getLogger
from typing import Any, Optional
from typing import TYPE_CHECKING, Any, Optional

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist
if TYPE_CHECKING:
from . import archivist

from .constants import (
APPLICATIONS_SUBPATH,
APPLICATIONS_LABEL,
APPLICATIONS_REGENERATE,
APPLICATIONS_SUBPATH,
)
from .dictmerge import _deepmerge


LOGGER = getLogger(__name__)


Expand Down
34 changes: 15 additions & 19 deletions archivist/archivist.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,43 +30,42 @@
"""
from __future__ import annotations
from logging import getLogger

from copy import deepcopy
from logging import getLogger
from time import time
from typing import Any, BinaryIO, Optional, Tuple

from requests_toolbelt.multipart.encoder import MultipartEncoder

from .access_policies import _AccessPoliciesClient
from .appidp import _AppIDPClient
from .applications import _ApplicationsClient
from .archivistpublic import ArchivistPublic
from .assetattachments import _AssetAttachmentsClient
from .assets import _AssetsRestricted
from .attachments import _AttachmentsClient
from .compliance import _ComplianceClient
from .compliance_policies import _CompliancePoliciesClient
from .composite import _CompositeClient
from .confirmer import MAX_TIME
from .constants import (
ROOT,
SEP,
)
from .dictmerge import _dotdict
from .errors import (
_parse_response,
ArchivistError,
_parse_response,
)
from .archivistpublic import ArchivistPublic
from .retry429 import retry_429

from .access_policies import _AccessPoliciesClient
from .appidp import _AppIDPClient
from .applications import _ApplicationsClient
from .assets import _AssetsRestricted
from .assetattachments import _AssetAttachmentsClient
from .attachments import _AttachmentsClient
from .compliance import _ComplianceClient
from .compliance_policies import _CompliancePoliciesClient
from .composite import _CompositeClient
from .events import _EventsRestricted
from .locations import _LocationsClient
from .retry429 import retry_429
from .runner import _Runner
from .sboms import _SBOMSClient
from .subjects import _SubjectsClient
from .tenancies import _TenanciesClient


LOGGER = getLogger(__name__)


Expand Down Expand Up @@ -222,10 +221,7 @@ def __copy__(self) -> Archivist:
)

def _add_headers(self, headers: dict[str, str] | None) -> dict[str, Any]:
if isinstance(headers, dict):
newheaders = {**headers}
else:
newheaders = {}
newheaders = {**headers} if isinstance(headers, dict) else {}

auth = self.auth # this may trigger a refetch so only do it once here
# for appidp endpoint there may not be an authtoken
Expand Down
23 changes: 11 additions & 12 deletions archivist/archivistpublic.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,37 @@
"""

from __future__ import annotations
from logging import getLogger

from collections import deque
from copy import deepcopy
from typing import Any, BinaryIO, Optional
from logging import getLogger
from typing import TYPE_CHECKING, Any, BinaryIO, Optional

import requests
from requests.models import Response

if TYPE_CHECKING:
from requests.models import Response


from .assetattachments import _AssetAttachmentsClient
from .assets import _AssetsPublic
from .confirmer import MAX_TIME
from .constants import (
HEADERS_REQUEST_TOTAL_COUNT,
HEADERS_TOTAL_COUNT,
)
from .dictmerge import _deepmerge, _dotdict
from .errors import (
_parse_response,
ArchivistBadFieldError,
ArchivistDuplicateError,
ArchivistHeaderError,
ArchivistNotFoundError,
_parse_response,
)
from .events import _EventsPublic
from .headers import _headers_get
from .retry429 import retry_429

from .assets import _AssetsPublic
from .events import _EventsPublic
from .assetattachments import _AssetAttachmentsClient

LOGGER = getLogger(__name__)


Expand Down Expand Up @@ -168,10 +170,7 @@ def __copy__(self):
)

def _add_headers(self, headers: Optional[dict]) -> dict[str, str]:
if headers is not None:
newheaders = {**headers}
else:
newheaders = {}
newheaders = {**headers} if headers is not None else {}

return newheaders

Expand Down
6 changes: 3 additions & 3 deletions archivist/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
from __future__ import annotations

from contextlib import suppress


class Asset(dict):
"""Asset
Expand Down Expand Up @@ -50,9 +52,7 @@ def primary_image(self) -> str | None:
def name(self) -> str | None:
"""str: name of the asset"""
name = None
try:
with suppress(KeyError, TypeError):
name = self["attributes"]["arc_display_name"]
except (KeyError, TypeError):
pass

return name
14 changes: 8 additions & 6 deletions archivist/assetattachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@
# pylint:disable=too-few-public-methods

from __future__ import annotations

from copy import deepcopy
from logging import getLogger
from typing import Any, BinaryIO, Optional
from typing import TYPE_CHECKING, Any, BinaryIO, Optional
from urllib.parse import urlparse

from requests.models import Response
if TYPE_CHECKING:
from requests.models import Response

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist
# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist

from .constants import (
SEP,
ASSETATTACHMENTS_SUBPATH,
ASSETATTACHMENTS_LABEL,
ASSETATTACHMENTS_SUBPATH,
ATTACHMENTS_LABEL,
SEP,
)
from .dictmerge import _deepmerge

Expand Down
9 changes: 4 additions & 5 deletions archivist/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,20 @@
"""

from __future__ import annotations

from copy import deepcopy
from logging import getLogger
from typing import Any, Optional, Tuple
from copy import deepcopy

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist

from . import archivist, confirmer
from .asset import Asset
from .constants import (
ASSET_BEHAVIOURS,
ASSETS_SUBPATH,
ASSETS_LABEL,
ASSETS_SUBPATH,
CONFIRMATION_STATUS,
)
from . import confirmer
from .dictmerge import _deepmerge
from .errors import ArchivistBadFieldError, ArchivistNotFoundError
from .utils import selector_signature
Expand Down
12 changes: 7 additions & 5 deletions archivist/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@
# pylint:disable=too-few-public-methods

from __future__ import annotations

from copy import deepcopy
from io import BytesIO
from logging import getLogger
from os import path
from typing import BinaryIO, Optional, Any
from typing import TYPE_CHECKING, Any, BinaryIO, Optional

from requests.models import Response
if TYPE_CHECKING:
from requests.models import Response

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist
# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from . import archivist

from .constants import (
ATTACHMENTS_SUBPATH,
ATTACHMENTS_LABEL,
ATTACHMENTS_SUBPATH,
)
from .dictmerge import _deepmerge
from .utils import get_url
Expand Down
1 change: 0 additions & 1 deletion archivist/cmds/runner/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from .main import main


if __name__ == "__main__":
# execute only if run as a script
main()
1 change: 0 additions & 1 deletion archivist/cmds/runner/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from sys import stdout as sys_stdout

from ...parser import common_parser, endpoint

from .run import run

LOGGER = getLogger(__name__)
Expand Down
6 changes: 2 additions & 4 deletions archivist/cmds/runner/run.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# pylint: disable=missing-docstring

from __future__ import annotations

from logging import getLogger
from os import environ
from sys import exit as sys_exit

from pyaml_env import parse_config

from ... import about

# pylint:disable=cyclic-import # but pylint doesn't understand this feature
from ... import archivist

from ... import about, archivist

LOGGER = getLogger(__name__)

Expand Down
Loading

0 comments on commit d566dd9

Please sign in to comment.