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 Ruff for Python static analysis and formatting #1784

Merged
merged 25 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d1f9fff
Use Ruff for Python static analysis and formatting
brianhelba Nov 3, 2023
7b6d475
Fix I002 Missing required import
brianhelba Jan 3, 2024
ca96bf1
Fix I001 Import block is un-sorted or un-formatted
brianhelba Nov 3, 2023
56cb741
Fix TCH001 Move application import into a type-checking block
brianhelba Jan 3, 2024
f7302f5
Fix TCH002 Move third-party import into a type-checking block
brianhelba Nov 3, 2023
8385608
Fix TCH003 Move standard library import into a type-checking block
brianhelba Nov 6, 2023
08414c1
Fix UP035 Import from `collections.abc` instead
brianhelba Nov 29, 2023
a077734
Fix UP037 Remove quotes from type annotation
brianhelba Jan 3, 2024
665c364
Ignore BLE001 Do not catch blind exception
brianhelba Nov 7, 2023
1e86e6b
Ignore SLF001 Private member accessed
brianhelba Nov 7, 2023
fc7af42
Ignore DJ001 Disallow null CharField
brianhelba Dec 15, 2023
5d3cd61
Ignore N806 Variable in function should be lowercase
brianhelba Nov 30, 2023
876b587
Ignore PLW0127 Self-assignment of variable
brianhelba Nov 30, 2023
ee3e6c1
Ignore ARG004 Unused static method argument
brianhelba Dec 1, 2023
5b5d637
Ignore C901 function is too complex
brianhelba Dec 1, 2023
9ea1dca
Ignore PLR0913 Too many arguments in function definition
brianhelba Dec 1, 2023
78dd350
Ignore PLR2004 Magic value used in comparison
brianhelba Dec 8, 2023
ddda096
Ignore S608 Possible SQL injection vector through string-based query …
brianhelba Dec 8, 2023
3a4eac4
Apply "ruff format"
brianhelba Dec 14, 2023
08a5ded
Fix RUF100 Unused `noqa` directive
brianhelba Nov 29, 2023
c9392ec
Manually fix line lengths
brianhelba Dec 14, 2023
7122589
Ignore PTH119 use `pathlib` instead of `os.path`
mvandenburgh Jan 22, 2024
eeeddfe
Fix N818 Exception name should be named with an Error suffix
mvandenburgh Jan 22, 2024
098a054
Fix RSE102 Unnecessary parentheses on raised exception
mvandenburgh Jan 22, 2024
4da4109
Fix DJ008 Model does not define `__str__` method
mvandenburgh Jan 22, 2024
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
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ignore overall.

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