Skip to content

Commit

Permalink
Use Ruff for Python linting and formatting
Browse files Browse the repository at this point in the history
This replaces the black, pyupgrade, reorder-python-imports and flake8
hooks in pre-commit with ruff.
  • Loading branch information
replaceafill authored Jun 20, 2024
1 parent 2dfa6f3 commit 010de6f
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 60 deletions.
16 changes: 0 additions & 16 deletions .flake8

This file was deleted.

26 changes: 5 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.16.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/asottile/reorder_python_imports
rev: v3.13.0
hooks:
- id: reorder-python-imports
args: [--py38-plus]
- repo: https://github.com/psf/black
rev: "23.12.1"
hooks:
- id: black
args: [--safe, --quiet]
- repo: https://github.com/pycqa/flake8
rev: "7.1.0"
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear==24.4.26
- flake8-comprehensions==3.14.0
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.41.0
hooks:
Expand Down
2 changes: 1 addition & 1 deletion agentarchives/archivesspace/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .client import * # noqa
from .client import *
8 changes: 2 additions & 6 deletions agentarchives/archivesspace/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,7 @@ def _login(self):
output = response.json()
except Exception:
raise ArchivesSpaceError(
"ArchivesSpace server responded with status {}, but returned a non-JSON document".format(
response.status_code
)
f"ArchivesSpace server responded with status {response.status_code}, but returned a non-JSON document"
)

if "error" in output:
Expand Down Expand Up @@ -174,9 +172,7 @@ def _request(self, method, url, params, expected_response, data=None):
output = response.json()
except Exception:
raise ArchivesSpaceError(
"ArchivesSpace server responded with status {}, but returned a non-JSON document".format(
response.status_code
)
f"ArchivesSpace server responded with status {response.status_code}, but returned a non-JSON document"
)

if "error" in output:
Expand Down
2 changes: 1 addition & 1 deletion agentarchives/archivists_toolkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .client import * # noqa
from .client import *
8 changes: 2 additions & 6 deletions agentarchives/archivists_toolkit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ def edit_record(self, new_record):
record_type = self.resource_type(record_id)
if record_type is None:
raise ArchivistsToolkitError(
"Could not determine type for record with ID {}; not in database?".format(
record_id
)
f"Could not determine type for record with ID {record_id}; not in database?"
)

clause = []
Expand Down Expand Up @@ -413,9 +411,7 @@ def find_collection_ids(

params = tuple(params)

sql = "SELECT resourceId FROM Resources WHERE ({}) AND resourceLevel in ('recordgrp', 'collection') ORDER BY title".format(
clause
)
sql = f"SELECT resourceId FROM Resources WHERE ({clause}) AND resourceLevel in ('recordgrp', 'collection') ORDER BY title"

if page is not None:
start = (page - 1) * page_size
Expand Down
2 changes: 1 addition & 1 deletion agentarchives/atom/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .client import * # noqa
from .client import *
10 changes: 3 additions & 7 deletions agentarchives/atom/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ class AuthenticationError(AtomError):

class CommunicationError(AtomError):
def __init__(self, status_code, response):
message = "AtoM server responded with status code {} (URL: {})".format(
status_code, response.url
)
message = f"AtoM server responded with status code {status_code} (URL: {response.url})"
self.response = response
super().__init__(message)

Expand Down Expand Up @@ -70,9 +68,7 @@ def _request(self, method, url, params, expected_response, data=None):
output = response.json()
except Exception:
raise AtomError(
"Atom server responded with status {}, but returned a non-JSON document".format(
response.status_code
)
f"Atom server responded with status {response.status_code}, but returned a non-JSON document"
)

if "error" in output:
Expand Down Expand Up @@ -239,7 +235,7 @@ def edit_record(self, new_record):
updated_date = {}

# Only single dates are currently supported
if "dates" in new_record and type(new_record["dates"]) is list:
if "dates" in new_record and isinstance(new_record["dates"], list):
new_record["dates"] = new_record["dates"][0]

# Map agentarchives date specification to AtoM specification
Expand Down
23 changes: 23 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,36 @@ dev = [
"pytest-cov",
"pytest-mock",
"pytest",
"ruff",
"vcrpy",
]

[tool.setuptools.dynamic]
version = {attr = "agentarchives.__version__"}
readme = {file = ["README.md"], content-type = "text/markdown"}

[tool.ruff.lint]
# Rule reference: https://docs.astral.sh/ruff/rules/
select = [
"B",
"C4",
"E",
"F",
"I",
"UP",
"W",
]
ignore = [
"B904",
"E501",
]

[tool.ruff.lint.per-file-ignores]
"agentarchives/{archivesspace,archivists_toolkit,atom}/__init__.py" = ["F403"]

[tool.ruff.lint.isort]
force-single-line = true

[tool.pytest.ini_options]
python_files = [
"test_*.py",
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pyyaml==6.0.1
# via vcrpy
requests==2.32.3
# via agentarchives (pyproject.toml)
ruff==0.4.9
# via agentarchives (pyproject.toml)
tomli==2.0.1
# via
# build
Expand Down
1 change: 0 additions & 1 deletion tests/test_archivesspace_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from agentarchives.archivesspace.client import ArchivesSpaceError
from agentarchives.archivesspace.client import CommunicationError


THIS_DIR = os.path.dirname(os.path.abspath(__file__))
AUTH = {"host": "http://localhost:8089", "user": "admin", "passwd": "admin"}

Expand Down

0 comments on commit 010de6f

Please # to comment.