Skip to content

Commit

Permalink
Merge pull request #1784 from dandi/ruff
Browse files Browse the repository at this point in the history
Use Ruff for Python static analysis and formatting
  • Loading branch information
mvandenburgh authored Jan 22, 2024
2 parents 1be249a + 4da4109 commit bee9b58
Show file tree
Hide file tree
Showing 122 changed files with 664 additions and 196 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
staticfiles/
/.ruff_cache/

# Created by https://www.gitignore.io/api/django
# Edit at https://www.gitignore.io/?templates=django
Expand Down Expand Up @@ -123,4 +124,4 @@ dmypy.json
# End of https://www.gitignore.io/api/django

# Editor settings
.vscode
.vscode
2 changes: 2 additions & 0 deletions dandiapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from importlib.metadata import version

# This project module is imported for us when Django starts. To ensure that Celery app is always
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/analytics/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.apps import AppConfig


Expand Down
2 changes: 2 additions & 0 deletions dandiapi/analytics/migrations/0001_initial_v2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import django.core.validators
from django.db import migrations, models

Expand Down
2 changes: 2 additions & 0 deletions dandiapi/analytics/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.core.validators import RegexValidator
from django.db import models

Expand Down
11 changes: 8 additions & 3 deletions dandiapi/analytics/tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from collections import Counter
from collections.abc import Generator
from pathlib import Path
from typing import TYPE_CHECKING

from celery.app import shared_task
from celery.utils.log import get_task_logger
Expand All @@ -15,6 +17,9 @@
from dandiapi.api.models.asset import AssetBlob, EmbargoedAssetBlob
from dandiapi.api.storage import get_boto_client, get_embargo_storage, get_storage

if TYPE_CHECKING:
from collections.abc import Generator

logger = get_task_logger(__name__)

# should be one of the DANDI_DANDISETS_*_LOG_BUCKET_NAME settings
Expand Down Expand Up @@ -77,14 +82,14 @@ def process_s3_log_file_task(bucket: LogBucket, s3_log_key: str) -> None:
return

s3 = get_boto_client(get_storage() if not embargoed else get_embargo_storage())
BlobModel = AssetBlob if not embargoed else EmbargoedAssetBlob
BlobModel = AssetBlob if not embargoed else EmbargoedAssetBlob # noqa: N806
data = s3.get_object(Bucket=bucket, Key=s3_log_key)
download_counts = Counter()

for log_entry in s3logparse.parse_log_lines(
line.decode('utf8') for line in data['Body'].iter_lines()
):
if log_entry.operation == 'REST.GET.OBJECT' and log_entry.status_code == 200:
if log_entry.operation == 'REST.GET.OBJECT' and log_entry.status_code == 200: # noqa: PLR2004
download_counts.update({log_entry.s3_key: 1})

with transaction.atomic():
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/analytics/tests/test_download_counts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.conf import settings
import pytest

Expand Down
5 changes: 3 additions & 2 deletions dandiapi/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# flake8: noqa
from __future__ import annotations

# TODO: remove this after migration is complete
import dandiapi.api.user_migration
import dandiapi.api.user_migration # noqa: F401
7 changes: 6 additions & 1 deletion dandiapi/api/admin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import csv
from typing import TYPE_CHECKING

from allauth.socialaccount.models import SocialAccount
from django.contrib import admin, messages
Expand All @@ -8,7 +11,6 @@
from django.db.models.aggregates import Count
from django.db.models.query import Prefetch, QuerySet
from django.forms.models import BaseInlineFormSet
from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.urls import reverse
from django.utils.html import format_html
Expand All @@ -26,6 +28,9 @@
from dandiapi.api.views.users import social_account_to_dict
from dandiapi.zarr.tasks import ingest_dandiset_zarrs

if TYPE_CHECKING:
from django.http.request import HttpRequest

admin.site.site_header = 'DANDI Admin'
admin.site.site_title = 'DANDI Admin'

Expand Down
7 changes: 5 additions & 2 deletions dandiapi/api/apps.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import re

Expand All @@ -14,7 +16,7 @@ class PublishConfig(AppConfig):
verbose_name = 'DANDI: Publish'

@staticmethod
def _get_sentry_performance_sample_rate(*args, **kwargs) -> float:
def _get_sentry_performance_sample_rate(*args, **kwargs) -> float: # noqa: ARG004
from dandiapi.api.models.asset import Asset
from dandiapi.api.models.dandiset import Dandiset
from dandiapi.api.models.version import Version
Expand Down Expand Up @@ -44,7 +46,8 @@ def is_noisy():
return 0.001 if is_noisy() else 0.01

def ready(self):
import dandiapi.api.checks # noqa: F401
# RUF100 is caused by https://github.com/astral-sh/ruff/issues/60
import dandiapi.api.checks # noqa: F401, RUF100
import dandiapi.api.signals # noqa: F401

if hasattr(settings, 'SENTRY_DSN'):
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/checks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.conf import settings
from django.core.checks import Error, register

Expand Down
6 changes: 5 additions & 1 deletion dandiapi/api/doi.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from __future__ import annotations

import logging
from typing import TYPE_CHECKING

from django.conf import settings
import requests

from dandiapi.api.models import Version
if TYPE_CHECKING:
from dandiapi.api.models import Version

# All of the required DOI configuration settings
DANDI_DOI_SETTINGS = [
Expand Down
7 changes: 6 additions & 1 deletion dandiapi/api/garbage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
from __future__ import annotations

from datetime import timedelta
from typing import TYPE_CHECKING

from django.db.models import Exists, OuterRef
from django.db.models.query import QuerySet
from django.utils import timezone

from dandiapi.api.models import Asset, Version

if TYPE_CHECKING:
from django.db.models.query import QuerySet

# How long after the last modification things are eligible for deletion
STALE_TIME_INTERVAL = timedelta(days=7)

Expand Down
10 changes: 7 additions & 3 deletions dandiapi/api/mail.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import annotations

from collections.abc import Iterable
import logging
from typing import TYPE_CHECKING

from allauth.socialaccount.models import SocialAccount
from django.conf import settings
from django.contrib.auth.models import User
from django.core import mail
from django.template.loader import render_to_string

if TYPE_CHECKING:
from collections.abc import Iterable

from allauth.socialaccount.models import SocialAccount
from django.contrib.auth.models import User

logger = logging.getLogger(__name__)

BASE_RENDER_CONTEXT = {
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/cleanup_blobs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.conf import settings
import djclick as click
from storages.backends.s3 import S3Storage
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/collect_garbage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import djclick as click

from dandiapi.api.garbage import stale_assets
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/create_dev_dandiset.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from uuid import uuid4

from django.conf import settings
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/depose_placeholder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.contrib.auth.models import User
import djclick as click

Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/depose_placeholders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.contrib.auth.models import User
import djclick as click

Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/ingest_asset_paths.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import djclick as click

from dandiapi.api.asset_paths import add_version_asset_paths
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/list_placeholders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.contrib.auth.models import User
import djclick as click

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from difflib import ndiff
from pprint import pformat

Expand All @@ -14,7 +16,8 @@
@click.argument('to_version')
def migrate_published_version_metadata(*, dandiset: str, published_version: str, to_version: str):
click.echo(
f'Migrating published version {dandiset}/{published_version} metadata to version {to_version}' # noqa: E501
f'Migrating published version {dandiset}/{published_version} '
f'metadata to version {to_version}'
)
version = Version.objects.exclude(version='draft').get(
dandiset=dandiset, version=published_version
Expand All @@ -23,7 +26,7 @@ def migrate_published_version_metadata(*, dandiset: str, published_version: str,

try:
metanew = migrate(metadata, to_version=to_version, skip_validation=False)
except Exception as e:
except Exception as e: # noqa: BLE001
click.echo(f'Failed to migrate {dandiset}/{published_version}')
click.echo(e)
raise click.Abort from e
Expand Down
4 changes: 3 additions & 1 deletion dandiapi/api/management/commands/migrate_version_metadata.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from dandischema import migrate
import djclick as click

Expand All @@ -15,7 +17,7 @@ def migrate_version_metadata(*, to_version: str):

try:
metanew = migrate(metadata, to_version=to_version, skip_validation=True)
except Exception as e:
except Exception as e: # noqa: BLE001
click.echo(f'Failed to migrate {version.dandiset.identifier}/{version.version}')
click.echo(e)
continue
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/management/commands/revalidate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import djclick as click

from dandiapi.api.models import Asset, Version
Expand Down
9 changes: 7 additions & 2 deletions dandiapi/api/manifests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from __future__ import annotations

from contextlib import contextmanager
import tempfile
from typing import IO, Any, Generator, Iterable
from typing import IO, TYPE_CHECKING, Any
from urllib.parse import urlparse, urlunparse

from django.conf import settings
Expand All @@ -11,6 +13,9 @@
from dandiapi.api.models import Asset, AssetBlob, Version
from dandiapi.api.storage import create_s3_storage

if TYPE_CHECKING:
from collections.abc import Generator, Iterable


def _s3_url(path: str) -> str:
"""Turn an object path into a fully qualified S3 URL."""
Expand Down Expand Up @@ -68,7 +73,7 @@ def _streaming_file_upload(path: str) -> Generator[IO[bytes], None, None]:

# Piggyback on the AssetBlob storage since we want to store manifests in the same bucket
storage = AssetBlob.blob.field.storage
storage._save(path, File(outfile))
storage._save(path, File(outfile)) # noqa: SLF001


def _yaml_dump_sequence_from_generator(stream: IO[bytes], generator: Iterable[Any]) -> None:
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/migrations/0001_initial_v2.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import uuid

from django.conf import settings
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/migrations/0001_stagingapplication.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/migrations/0002_asset_zarr.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.db import migrations, models
import django.db.models.deletion

Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/migrations/0003_default_oauth_application.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.db import migrations
from django.db.models import Q
from oauth2_provider.settings import oauth2_settings
Expand Down
2 changes: 2 additions & 0 deletions dandiapi/api/migrations/0004_merge.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from django.db import migrations


Expand Down
Loading

0 comments on commit bee9b58

Please sign in to comment.