Skip to content

Commit

Permalink
ci: Migrate to ruff (#5150)
Browse files Browse the repository at this point in the history
  • Loading branch information
khvn26 authored Feb 21, 2025
1 parent 1987252 commit f7487ac
Show file tree
Hide file tree
Showing 204 changed files with 876 additions and 570 deletions.
29 changes: 10 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
repos:
- repo: https://github.com/PyCQA/isort
rev: 6.0.0
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.9.7
hooks:
- id: isort
name: isort (python)

- repo: https://github.com/psf/black
rev: 25.1.0
hooks:
- id: black
language_version: python3
exclude: migrations

- repo: https://github.com/pycqa/flake8
rev: 7.1.2
hooks:
- id: flake8
name: flake8
args: [--config, api/.flake8]
# Run the linter.
- id: ruff
args: [--config, api/pyproject.toml, --config, "src = ['api']", --fix]
# Run the formatter.
- id: ruff-format
args: [--config, api/pyproject.toml, --config, "src = ['api']"]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
Expand Down Expand Up @@ -52,5 +43,5 @@ repos:
args: ["-C", "api"]

ci:
skip: [python-typecheck, isort]
skip: [python-typecheck]
autoupdate_commit_msg: "ci: pre-commit autoupdate"
10 changes: 0 additions & 10 deletions api/.flake8

This file was deleted.

6 changes: 5 additions & 1 deletion api/api/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from typing import Any

from drf_yasg.inspectors import SwaggerAutoSchema # type: ignore[import-untyped]
from drf_yasg.openapi import SCHEMA_DEFINITIONS, Response, Schema # type: ignore[import-untyped]
from drf_yasg.openapi import ( # type: ignore[import-untyped]
SCHEMA_DEFINITIONS,
Response,
Schema,
)
from pydantic import BaseModel
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaValue
from pydantic_core import core_schema
Expand Down
2 changes: 1 addition & 1 deletion api/api/urls/v1.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from app_analytics.views import SDKAnalyticsFlags, SelfHostedTelemetryAPIView
from django.conf import settings
from django.urls import include, path, re_path
from drf_yasg import openapi # type: ignore[import-untyped]
from drf_yasg.views import get_schema_view # type: ignore[import-untyped]
from rest_framework import authentication, permissions, routers

from app_analytics.views import SDKAnalyticsFlags, SelfHostedTelemetryAPIView
from environments.identities.traits.views import SDKTraits
from environments.identities.views import SDKIdentities
from environments.sdk.views import SDKEnvironmentAPIView
Expand Down
3 changes: 2 additions & 1 deletion api/api/urls/v2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from app_analytics.views import SDKAnalyticsFlagsV2
from django.urls import re_path

from app_analytics.views import SDKAnalyticsFlagsV2

app_name = "v2"

urlpatterns = [
Expand Down
11 changes: 9 additions & 2 deletions api/api_keys/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from django.conf import settings
from django.db import models
from django_lifecycle import BEFORE_UPDATE, LifecycleModelMixin, hook # type: ignore[import-untyped]
from django_lifecycle import ( # type: ignore[import-untyped]
BEFORE_UPDATE,
LifecycleModelMixin,
hook,
)
from rest_framework_api_key.models import AbstractAPIKey, APIKeyManager
from softdelete.models import SoftDeleteManager, SoftDeleteObject # type: ignore[import-untyped]
from softdelete.models import ( # type: ignore[import-untyped]
SoftDeleteManager,
SoftDeleteObject,
)

from organisations.models import Organisation

Expand Down
9 changes: 7 additions & 2 deletions api/api_keys/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ def is_group_admin(self, group_id: int) -> bool:
return False

def has_project_permission(
self, permission: str, project: "Project", tag_ids: typing.List[int] = None # type: ignore[assignment]
self,
permission: str,
project: "Project",
tag_ids: typing.List[int] = None, # type: ignore[assignment]
) -> bool:
return project in self.get_permitted_projects(permission, tag_ids)

Expand All @@ -95,7 +98,9 @@ def has_organisation_permission(
)

def get_permitted_projects(
self, permission_key: str, tag_ids: typing.List[int] = None # type: ignore[assignment]
self,
permission_key: str,
tag_ids: typing.List[int] = None, # type: ignore[assignment]
) -> QuerySet["Project"]:
return get_permitted_projects_for_master_api_key(
self.key, permission_key, tag_ids
Expand Down
10 changes: 5 additions & 5 deletions api/app_analytics/analytics_db_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from datetime import date, datetime, timedelta
from typing import List

from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.db.models import Sum
from django.utils import timezone

from app_analytics.dataclasses import FeatureEvaluationData, UsageData
from app_analytics.influxdb_wrapper import (
get_events_for_organisation,
Expand All @@ -16,11 +21,6 @@
FeatureEvaluationBucket,
Resource,
)
from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.db.models import Sum
from django.utils import timezone

from environments.models import Environment
from features.models import Feature
from organisations.models import Organisation
Expand Down
5 changes: 3 additions & 2 deletions api/app_analytics/cache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from collections import defaultdict
from threading import Lock

from app_analytics.tasks import track_feature_evaluation, track_request
from app_analytics.track import track_feature_evaluation_influxdb
from django.conf import settings
from django.utils import timezone

from app_analytics.tasks import track_feature_evaluation, track_request
from app_analytics.track import track_feature_evaluation_influxdb


class APIUsageCache:
def __init__(self): # type: ignore[no-untyped-def]
Expand Down
3 changes: 2 additions & 1 deletion api/app_analytics/management/commands/migrate_analytics.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import argparse
from typing import Any

from app_analytics.migrate_to_pg import migrate_feature_evaluations
from django.core.management import BaseCommand

from app_analytics.migrate_to_pg import migrate_feature_evaluations


class Command(BaseCommand):
def add_arguments(self, parser: argparse.ArgumentParser) -> None:
Expand Down
5 changes: 3 additions & 2 deletions api/app_analytics/management/commands/populate_buckets.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import argparse
from typing import Any

from django.conf import settings
from django.core.management import BaseCommand

from app_analytics.constants import ANALYTICS_READ_BUCKET_SIZE
from app_analytics.tasks import (
populate_api_usage_bucket,
populate_feature_evaluation_bucket,
)
from django.conf import settings
from django.core.management import BaseCommand

MINUTES_IN_DAY: int = 1440

Expand Down
3 changes: 2 additions & 1 deletion api/app_analytics/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.conf import settings

from app_analytics.cache import APIUsageCache
from app_analytics.tasks import track_request
from django.conf import settings

from .models import Resource
from .track import (
Expand Down
2 changes: 1 addition & 1 deletion api/app_analytics/migrate_to_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def migrate_feature_evaluations(migrate_till: int = 30) -> None:
query_api = influxdb_client.query_api()

for i in range(migrate_till):
range_start = f"-{i+1}d"
range_start = f"-{i + 1}d"
range_stop = f"-{i}d"
query = (
f'from (bucket: "{read_bucket}") '
Expand Down
6 changes: 5 additions & 1 deletion api/app_analytics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from django.core.exceptions import ValidationError
from django.db import models
from django_lifecycle import BEFORE_CREATE, LifecycleModelMixin, hook # type: ignore[import-untyped]
from django_lifecycle import ( # type: ignore[import-untyped]
BEFORE_CREATE,
LifecycleModelMixin,
hook,
)


class Resource(models.IntegerChoices):
Expand Down
22 changes: 16 additions & 6 deletions api/app_analytics/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime, timedelta
from typing import List, Tuple

from app_analytics.constants import ANALYTICS_READ_BUCKET_SIZE
from django.conf import settings
from django.db.models import Q, Sum
from django.utils import timezone
Expand All @@ -10,6 +9,7 @@
register_task_handler,
)

from app_analytics.constants import ANALYTICS_READ_BUCKET_SIZE
from environments.models import Environment

from .models import (
Expand All @@ -29,7 +29,9 @@
},
)
def populate_bucket( # type: ignore[no-untyped-def]
bucket_size: int, run_every: int, source_bucket_size: int = None # type: ignore[assignment]
bucket_size: int,
run_every: int,
source_bucket_size: int = None, # type: ignore[assignment]
):
populate_api_usage_bucket(bucket_size, run_every, source_bucket_size)
populate_feature_evaluation_bucket(bucket_size, run_every, source_bucket_size)
Expand Down Expand Up @@ -139,7 +141,9 @@ def get_time_buckets(


def populate_api_usage_bucket( # type: ignore[no-untyped-def]
bucket_size: int, run_every: int, source_bucket_size: int = None # type: ignore[assignment]
bucket_size: int,
run_every: int,
source_bucket_size: int = None, # type: ignore[assignment]
):
for bucket_start_time, bucket_end_time in get_time_buckets(bucket_size, run_every):
data = _get_api_usage_source_data(
Expand All @@ -156,7 +160,9 @@ def populate_api_usage_bucket( # type: ignore[no-untyped-def]


def populate_feature_evaluation_bucket( # type: ignore[no-untyped-def]
bucket_size: int, run_every: int, source_bucket_size: int = None # type: ignore[assignment]
bucket_size: int,
run_every: int,
source_bucket_size: int = None, # type: ignore[assignment]
):
for bucket_start_time, bucket_end_time in get_time_buckets(bucket_size, run_every):
data = _get_feature_evaluation_source_data(
Expand All @@ -173,7 +179,9 @@ def populate_feature_evaluation_bucket( # type: ignore[no-untyped-def]


def _get_api_usage_source_data(
process_from: datetime, process_till: datetime, source_bucket_size: int = None # type: ignore[assignment]
process_from: datetime,
process_till: datetime,
source_bucket_size: int = None, # type: ignore[assignment]
) -> dict: # type: ignore[type-arg]
filters = Q(
created_at__lte=process_till,
Expand All @@ -193,7 +201,9 @@ def _get_api_usage_source_data(


def _get_feature_evaluation_source_data(
process_from: datetime, process_till: datetime, source_bucket_size: int = None # type: ignore[assignment]
process_from: datetime,
process_till: datetime,
source_bucket_size: int = None, # type: ignore[assignment]
) -> dict: # type: ignore[type-arg]
filters = Q(
created_at__lte=process_till,
Expand Down
10 changes: 7 additions & 3 deletions api/app_analytics/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
import uuid

import requests
from app_analytics.influxdb_wrapper import InfluxDBWrapper
from django.conf import settings
from django.core.cache import caches
from six.moves.urllib.parse import quote # type: ignore[import-untyped] # python 2/3 compatible urllib import
from task_processor.decorators import register_task_handler # type: ignore[import-untyped]
from six.moves.urllib.parse import ( # type: ignore[import-untyped]
quote, # python 2/3 compatible urllib import
)
from task_processor.decorators import ( # type: ignore[import-untyped]
register_task_handler,
)

from app_analytics.influxdb_wrapper import InfluxDBWrapper
from environments.models import Environment
from util.util import postpone

Expand Down
16 changes: 8 additions & 8 deletions api/app_analytics/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import logging

from app_analytics.analytics_db_service import (
get_total_events_count,
get_usage_data,
)
from app_analytics.cache import FeatureEvaluationCache
from app_analytics.tasks import track_feature_evaluation_v2
from app_analytics.track import track_feature_evaluation_influxdb_v2
from django.conf import settings
from drf_yasg.utils import swagger_auto_schema # type: ignore[import-untyped]
from rest_framework import status
Expand All @@ -17,12 +10,19 @@
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.serializers import Serializer
from telemetry.serializers import TelemetrySerializer

from app_analytics.analytics_db_service import (
get_total_events_count,
get_usage_data,
)
from app_analytics.cache import FeatureEvaluationCache
from app_analytics.tasks import track_feature_evaluation_v2
from app_analytics.track import track_feature_evaluation_influxdb_v2
from environments.authentication import EnvironmentKeyAuthentication
from environments.permissions.permissions import EnvironmentKeyPermissions
from features.models import FeatureState
from organisations.models import Organisation
from telemetry.serializers import TelemetrySerializer

from .permissions import UsageDataPermission
from .serializers import (
Expand Down
3 changes: 2 additions & 1 deletion api/audit/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ def get_change_details(
) -> typing.List[typing.Dict[str, typing.Any]]:
if history_record := instance.history_record:
return AuditLogChangeDetailsSerializer( # type: ignore[return-value]
instance=history_record.get_change_details(), many=True # type: ignore[attr-defined]
instance=history_record.get_change_details(), # type: ignore[attr-defined]
many=True,
).data

return []
Expand Down
3 changes: 1 addition & 2 deletions api/audit/services.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from core.models import AbstractBaseAuditableModel

from audit.models import AuditLog
from audit.related_object_type import RelatedObjectType
from core.models import AbstractBaseAuditableModel
from features.models import Feature, FeatureState
from features.versioning.models import EnvironmentFeatureVersion

Expand Down
3 changes: 2 additions & 1 deletion api/audit/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def send_audit_log_event_to_datadog(sender, instance, **kwargs): # type: ignore
return

data_dog = DataDogWrapper(
base_url=data_dog_config.base_url, api_key=data_dog_config.api_key # type: ignore[arg-type]
base_url=data_dog_config.base_url, # type: ignore[arg-type]
api_key=data_dog_config.api_key,
)
_track_event_async(instance, data_dog) # type: ignore[no-untyped-call]

Expand Down
4 changes: 3 additions & 1 deletion api/audit/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from django.contrib.auth import get_user_model
from django.utils import timezone
from task_processor.decorators import register_task_handler # type: ignore[import-untyped]
from task_processor.decorators import ( # type: ignore[import-untyped]
register_task_handler,
)
from task_processor.models import TaskPriority # type: ignore[import-untyped]

from audit.constants import (
Expand Down
Loading

0 comments on commit f7487ac

Please sign in to comment.