From 9d0184385ba828b01fecaa74ee62a9d49a12037d Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Mon, 16 Sep 2024 00:44:30 -0400 Subject: [PATCH] chore: Update ruff lint configuration --- manage.py | 11 ++----- process/cli.py | 16 +++-------- process/models.py | 56 +++++++++--------------------------- process/processors/loader.py | 4 +-- process/scrapyd.py | 8 ++---- process/util.py | 8 ++---- process/views.py | 20 ++++--------- pyproject.toml | 37 ++++++++++++------------ 8 files changed, 49 insertions(+), 111 deletions(-) diff --git a/manage.py b/manage.py index ccea1635..bd9928d2 100755 --- a/manage.py +++ b/manage.py @@ -8,14 +8,9 @@ def main(): """Run administrative tasks.""" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings") - try: - from django.core.management import execute_from_command_line - except ImportError as exc: - raise ImportError( - "Couldn't import Django. Are you sure it's installed and " - "available on your PYTHONPATH environment variable? Did you " - "forget to activate a virtual environment?" - ) from exc + + from django.core.management import execute_from_command_line + execute_from_command_line(sys.argv) diff --git a/process/cli.py b/process/cli.py index b7c142cd..c103f877 100644 --- a/process/cli.py +++ b/process/cli.py @@ -8,21 +8,15 @@ class CollectionCommand(BaseCommand): def add_arguments(self, parser): - """ - Add default arguments to the command. - """ + """Add default arguments to the command.""" parser.add_argument("collection_id", help=_("the ID of the collection")) self.add_collection_arguments(parser) def add_collection_arguments(self, parser): - """ - Add arguments specific to this command. - """ + """Add arguments specific to this command.""" def handle(self, *args, **options): - """ - Get the collection. - """ + """Get the collection.""" self.stderr.style_func = None logging.getLogger("process").setLevel(logging.DEBUG) @@ -37,7 +31,5 @@ def handle(self, *args, **options): self.handle_collection(collection, *args, **options) def handle_collection(self, collection, *args, **options): - """ - Run the command. - """ + """Run the command.""" raise NotImplementedError("collection commands must implement handle_collection()") diff --git a/process/models.py b/process/models.py index c299fc6c..db84bcdb 100644 --- a/process/models.py +++ b/process/models.py @@ -153,9 +153,7 @@ def clean_fields(self, exclude=None): ) def get_upgraded_collection(self) -> Self | None: - """ - Return the upgraded collection or None. - """ + """Return the upgraded collection or None.""" # This is a shortcut to avoid a query. It is based on the logic in clean_fields(). if self.transform_type: return None @@ -165,27 +163,21 @@ def get_upgraded_collection(self) -> Self | None: return None def get_compiled_collection(self) -> Self | None: - """ - Return the compiled collection or None. - """ + """Return the compiled collection or None.""" try: return Collection.objects.get(transform_type=Collection.Transform.COMPILE_RELEASES, parent=self) except Collection.DoesNotExist: return None def get_root_parent(self) -> Self: - """ - Return the "root" ancestor of the collection. - """ + """Return the "root" ancestor of the collection.""" if self.parent is None: return self return self.parent.get_root_parent() class CollectionNote(models.Model): - """ - A note an analyst made about the collection. - """ + """A note an analyst made about the collection.""" class Level(models.TextChoices): INFO = "INFO" @@ -210,9 +202,7 @@ def __str__(self): class CollectionFile(models.Model): - """ - A file within the collection. - """ + """A file within the collection.""" collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False) @@ -234,9 +224,7 @@ def __str__(self): class ProcessingStep(models.Model): - """ - A step in the lifecycle of collection file. - """ + """A step in the lifecycle of collection file.""" class Name(models.TextChoices): LOAD = "LOAD" @@ -265,9 +253,7 @@ def __str__(self): class CollectionFileItem(models.Model): - """ - An item within a file in the collection. - """ + """An item within a file in the collection.""" collection_file = models.ForeignKey(CollectionFile, on_delete=models.CASCADE, db_index=False) @@ -292,9 +278,7 @@ def __str__(self): class Data(models.Model): - """ - The contents of a release, record or compiled release. - """ + """The contents of a release, record or compiled release.""" hash_md5 = models.TextField() data = models.JSONField(encoder=JSONEncoder) @@ -311,9 +295,7 @@ def __str__(self): class PackageData(models.Model): - """ - The contents of a package, excluding the releases or records. - """ + """The contents of a package, excluding the releases or records.""" hash_md5 = models.TextField() data = models.JSONField(encoder=JSONEncoder) @@ -330,9 +312,7 @@ def __str__(self): class Release(models.Model): - """ - A release. - """ + """A release.""" collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False) collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False) @@ -364,9 +344,7 @@ def __str__(self): class Record(models.Model): - """ - A record. - """ + """A record.""" collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False) collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False) @@ -395,9 +373,7 @@ def __str__(self): class CompiledRelease(models.Model): - """ - A compiled release. - """ + """A compiled release.""" collection = models.ForeignKey(Collection, on_delete=models.CASCADE, db_index=False) collection_file_item = models.ForeignKey(CollectionFileItem, on_delete=models.CASCADE, db_index=False) @@ -422,9 +398,7 @@ def __str__(self): class ReleaseCheck(models.Model): - """ - The result of checking a release. - """ + """The result of checking a release.""" release = models.OneToOneField(Release, on_delete=models.CASCADE) cove_output = models.JSONField() @@ -437,9 +411,7 @@ def __str__(self): class RecordCheck(models.Model): - """ - The result of checking a record. - """ + """The result of checking a record.""" record = models.OneToOneField(Record, on_delete=models.CASCADE) cove_output = models.JSONField() diff --git a/process/processors/loader.py b/process/processors/loader.py index 90c77009..f18370a5 100644 --- a/process/processors/loader.py +++ b/process/processors/loader.py @@ -15,9 +15,7 @@ def file_or_directory(path): - """ - Check whether the path exists. Raise an exception if not. - """ + """Check whether the path exists. Raise an exception if not.""" if not os.path.exists(path): raise argparse.ArgumentTypeError(t("No such file or directory %(path)r") % {"path": path}) return path diff --git a/process/scrapyd.py b/process/scrapyd.py index 47fe8290..2a23bd5c 100644 --- a/process/scrapyd.py +++ b/process/scrapyd.py @@ -5,16 +5,12 @@ def configured() -> bool: - """ - Return whether the connection to Scrapyd is configured. - """ + """Return whether the connection to Scrapyd is configured.""" return bool(settings.SCRAPYD["url"]) def spiders() -> list[str]: - """ - Return the names of the spiders in the Scrapyd project. - """ + """Return the names of the spiders in the Scrapyd project.""" # https://scrapyd.readthedocs.io/en/stable/api.html#listspiders-json url = urljoin(settings.SCRAPYD["url"], "/listspiders.json") response = requests.get(url, params={"project": settings.SCRAPYD["project"]}, timeout=10) diff --git a/process/util.py b/process/util.py index cc1ee81b..a3ad9a96 100644 --- a/process/util.py +++ b/process/util.py @@ -23,9 +23,7 @@ def wrap(string): - """ - Format a long string as a help message, and return it. - """ + """Format a long string as a help message, and return it.""" return "\n".join(fill(paragraph, width=78, replace_whitespace=False) for paragraph in string.split("\n")) @@ -123,9 +121,7 @@ def create_step(name, collection_id, **kwargs): @contextmanager def delete_step(*args, **kwargs): - """ - Delete the named step and run any finish callback only if successful or if the error is expected. - """ + """Delete the named step and run any finish callback only if successful or if the error is expected.""" try: yield # Delete the step so that the collection is completable, only if the error was expected. diff --git a/process/views.py b/process/views.py index f4fa8692..a60ab218 100644 --- a/process/views.py +++ b/process/views.py @@ -78,9 +78,7 @@ class CollectionViewSet(viewsets.ViewSet): }, ) def create(self, request): - """ - Create an original collection and any derived collections. - """ + """Create an original collection and any derived collections.""" serializer = CreateCollectionSerializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -112,9 +110,7 @@ def create(self, request): @extend_schema(request=CloseCollectionSerializer, responses={202: None}) @action(detail=True, methods=["post"]) def close(self, request, pk=None): - """ - Publish a message to RabbitMQ to close a root collection and its derived collections, if any. - """ + """Publish a message to RabbitMQ to close a root collection and its derived collections, if any.""" serializer = CloseCollectionSerializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -151,9 +147,7 @@ def close(self, request, pk=None): @extend_schema(responses={202: None}) def destroy(self, request, pk=None): - """ - Publish a message to RabbitMQ to wipe the dataset. - """ + """Publish a message to RabbitMQ to wipe the dataset.""" with get_publisher() as client: client.publish({"collection_id": pk}, routing_key="wiper") @@ -175,9 +169,7 @@ def destroy(self, request, pk=None): ) @action(detail=True) def metadata(self, request, pk=None): - """ - Return the compiled collection's metadata. - """ + """Return the compiled collection's metadata.""" compiled_collection = get_object_or_404(Collection, pk=pk) collection = compiled_collection.get_root_parent() @@ -243,9 +235,7 @@ def metadata(self, request, pk=None): @extend_schema(responses=TreeSerializer(many=True)) @action(detail=True) def tree(self, request, pk=None): - """ - Return the original collection and its derived collections, if any. - """ + """Return the original collection and its derived collections, if any.""" result = Collection.objects.raw( """\ WITH RECURSIVE tree ( diff --git a/pyproject.toml b/pyproject.toml index d7d9c77d..0701c208 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -9,33 +9,32 @@ target-version = "py311" [tool.ruff.lint] select = ["ALL"] ignore = [ - "ANN", "COM", "EM", - # https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules - "W191", "D206", "Q000", "Q001", "Q002", "Q003", "ISC001", - "D203", "D212", # ignore incompatible rules - "D200", # documentation preferences - "C901", "PLR091", # complexity preferences - "D1", # docstrings - "PTH", # pathlib - "ARG001", "ARG002", "RUF012", # Django - "PLW2901", # release_or_record overridden - "TRY003", # Django command errors + "ANN", "C901", "COM812", "D203", "D212", "D415", "EM", "PERF203", "PLR091", "Q000", + "D1", + "PTH", ] [tool.ruff.lint.flake8-builtins] builtins-ignorelist = ["copyright"] +[tool.ruff.lint.flake8-unused-arguments] +ignore-variadic-names = true + [tool.ruff.lint.per-file-ignores] -"docs/conf.py" = ["INP001"] # no __init__.py file +"docs/*" = ["D100", "INP001"] +"{*/signals,*/views,*/migrations/*}.py" = ["ARG001"] +"{*/admin,*/routers,*/views,*/commands/*}.py" = ["ARG002"] +"{*/admin,*/forms,*/models,*/routers,*/serializers,*/translation,*/migrations/*,tests/*}.py" = ["RUF012"] "*/migrations/*" = ["E501"] "tests/*" = [ - "D", # docstring - "PT", # unittest - "ARG002", # mocks - "BLE001", # Exception - "PLR2004", # Magic value used - "S101", # assert + "D", "FBT003", "INP001", "PLR2004", "PT", "S", "TRY003", + "ARG002", # mock +] +"*/commands/*" = [ + "ARG001", # pika + "PLW2901", + "TRY003", # errors ] [tool.coverage.run] -omit = ['*/migrations/*'] +omit = ["*/migrations/*"]