From 95c7e986c8c742dc64ab24ef95ba002988eeba14 Mon Sep 17 00:00:00 2001 From: ajay-sentry <159853603+ajay-sentry@users.noreply.github.com> Date: Tue, 4 Jun 2024 14:43:15 -0700 Subject: [PATCH] dev: Remove all the excluded ruff rules except E501 (#235) * fix most of the F and E rules post excluding test * fix the unused import rule * remove this comment bit * merge main, update comment --- ruff.toml | 3 ++- shared/api_archive/archive.py | 1 - shared/bundle_analysis/__init__.py | 18 ++++++++++++++++++ shared/bundle_analysis/models.py | 2 +- shared/bundle_analysis/parser.py | 3 +-- shared/config/__init__.py | 2 +- .../codecov_auth/migrations/0001_initial.py | 1 - .../migrations/0003_auto_20210924_1003.py | 1 - .../migrations/0006_auto_20211123_1535.py | 1 - .../migrations/0009_auto_20220511_1313.py | 1 - .../migrations/0015_organizationleveltoken.py | 1 - .../codecov_auth/migrations/0018_usertoken.py | 1 - .../0020_ownerprofile_default_org.py | 1 - .../migrations/0031_user_owner_user.py | 1 - shared/django_apps/core/managers.py | 2 -- .../migrations/0034_remove_repository_cache.py | 2 -- shared/django_apps/dummy_settings.py | 4 ++-- .../migrations/0001_initial.py | 1 - shared/metrics/__init__.py | 10 ++++++++++ shared/reports/filtered.py | 8 ++++---- shared/reports/resources.py | 12 ++++++------ shared/reports/types.py | 2 +- shared/rollouts/__init__.py | 1 - shared/storage/aws.py | 9 ++++----- shared/storage/gcp.py | 1 - shared/storage/minio.py | 2 +- shared/torngit/base.py | 4 ++-- shared/torngit/bitbucket.py | 4 ++-- shared/torngit/bitbucket_server.py | 4 ++-- shared/torngit/github.py | 2 +- shared/typings/oauth_token_types.py | 2 +- shared/utils/sessions.py | 1 - shared/yaml/__init__.py | 8 ++++++++ 33 files changed, 67 insertions(+), 49 deletions(-) diff --git a/ruff.toml b/ruff.toml index 7eb9a0e5..6926433c 100644 --- a/ruff.toml +++ b/ruff.toml @@ -26,6 +26,7 @@ exclude = [ "node_modules", "site-packages", "venv", + "**test**" ] # Same as Black. @@ -39,7 +40,7 @@ target-version = "py312" # Currently only enabled for F (Pyflakes), I (isort), E (pycodestyle:Error), PLC (Pylint:Convention) # and PLE (Pylint:Error) rules: https://docs.astral.sh/ruff/rules/ select = ["F", "I", "E", "PLC", "PLE"] -ignore = ["F841", "F401","F405", "F403", "E501", "E712", "E711", "E741"] +ignore = ["E501"] # Allow fix for all enabled rules (when `--fix`) is provided. # The preferred method (for now) w.r.t. fixable rules is to manually update the makefile diff --git a/shared/api_archive/archive.py b/shared/api_archive/archive.py index 828b8b84..4be3f166 100644 --- a/shared/api_archive/archive.py +++ b/shared/api_archive/archive.py @@ -7,7 +7,6 @@ from django.conf import settings from django.utils import timezone -from minio import Minio from shared.api_archive.storage import StorageService from shared.config import get_config diff --git a/shared/bundle_analysis/__init__.py b/shared/bundle_analysis/__init__.py index f9c620b4..b48ffb73 100644 --- a/shared/bundle_analysis/__init__.py +++ b/shared/bundle_analysis/__init__.py @@ -16,3 +16,21 @@ ModuleReport, ) from shared.bundle_analysis.storage import BundleAnalysisReportLoader, StoragePaths + +__all__ = [ + "models", + "AssetChange", + "BundleAnalysisComparison", + "BundleChange", + "BundleComparison", + "MissingBaseReportError", + "MissingBundleError", + "MissingHeadReportError", + "Parser", + "AssetReport", + "BundleAnalysisReport", + "BundleReport", + "ModuleReport", + "BundleAnalysisReportLoader", + "StoragePaths", +] diff --git a/shared/bundle_analysis/models.py b/shared/bundle_analysis/models.py index 1cd11e52..a20da8ce 100644 --- a/shared/bundle_analysis/models.py +++ b/shared/bundle_analysis/models.py @@ -1,6 +1,6 @@ import logging from enum import Enum -from typing import List, Optional +from typing import Optional import sqlalchemy from sqlalchemy import Column, ForeignKey, Table, create_engine, types diff --git a/shared/bundle_analysis/parser.py b/shared/bundle_analysis/parser.py index 671d193d..87108713 100644 --- a/shared/bundle_analysis/parser.py +++ b/shared/bundle_analysis/parser.py @@ -2,13 +2,12 @@ import logging import re import uuid -from typing import Optional, Tuple +from typing import Tuple import ijson from sqlalchemy.orm import Session as DbSession from shared.bundle_analysis.models import ( - SCHEMA, Asset, AssetType, Bundle, diff --git a/shared/config/__init__.py b/shared/config/__init__.py index 9cf9be6a..85540d67 100644 --- a/shared/config/__init__.py +++ b/shared/config/__init__.py @@ -131,7 +131,7 @@ def _env_var_value_cast(self, data): elif re.match(r"^-?\d+\.\d+$", data): try: return float(data) - except ValueError as e: + except ValueError: pass return data diff --git a/shared/django_apps/codecov_auth/migrations/0001_initial.py b/shared/django_apps/codecov_auth/migrations/0001_initial.py index 746ec97d..daddd5cb 100644 --- a/shared/django_apps/codecov_auth/migrations/0001_initial.py +++ b/shared/django_apps/codecov_auth/migrations/0001_initial.py @@ -6,7 +6,6 @@ import django.contrib.postgres.fields import django.contrib.postgres.fields.citext import django.db.models.deletion -from django.conf import settings from django.contrib.postgres.operations import CITextExtension from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0003_auto_20210924_1003.py b/shared/django_apps/codecov_auth/migrations/0003_auto_20210924_1003.py index d51a5732..c304ef4d 100644 --- a/shared/django_apps/codecov_auth/migrations/0003_auto_20210924_1003.py +++ b/shared/django_apps/codecov_auth/migrations/0003_auto_20210924_1003.py @@ -4,7 +4,6 @@ import django.contrib.postgres.fields import django.db.models.deletion -from django.conf import settings from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0006_auto_20211123_1535.py b/shared/django_apps/codecov_auth/migrations/0006_auto_20211123_1535.py index d5ef25c2..819dfc3a 100644 --- a/shared/django_apps/codecov_auth/migrations/0006_auto_20211123_1535.py +++ b/shared/django_apps/codecov_auth/migrations/0006_auto_20211123_1535.py @@ -1,7 +1,6 @@ # Generated by Django 3.1.13 on 2021-11-23 15:35 import django.db.models.deletion -from django.conf import settings from django.db import migrations, models from shared.django_apps.codecov_auth.models import _generate_key diff --git a/shared/django_apps/codecov_auth/migrations/0009_auto_20220511_1313.py b/shared/django_apps/codecov_auth/migrations/0009_auto_20220511_1313.py index dbcdbac6..5f63bca6 100644 --- a/shared/django_apps/codecov_auth/migrations/0009_auto_20220511_1313.py +++ b/shared/django_apps/codecov_auth/migrations/0009_auto_20220511_1313.py @@ -2,7 +2,6 @@ import django.contrib.postgres.fields import django.db.models.deletion -from django.conf import settings from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0015_organizationleveltoken.py b/shared/django_apps/codecov_auth/migrations/0015_organizationleveltoken.py index a0f1558e..b710f0f9 100644 --- a/shared/django_apps/codecov_auth/migrations/0015_organizationleveltoken.py +++ b/shared/django_apps/codecov_auth/migrations/0015_organizationleveltoken.py @@ -3,7 +3,6 @@ import uuid import django.db.models.deletion -from django.conf import settings from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0018_usertoken.py b/shared/django_apps/codecov_auth/migrations/0018_usertoken.py index 679554a4..31e9ed4e 100644 --- a/shared/django_apps/codecov_auth/migrations/0018_usertoken.py +++ b/shared/django_apps/codecov_auth/migrations/0018_usertoken.py @@ -3,7 +3,6 @@ import uuid import django.db.models.deletion -from django.conf import settings from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0020_ownerprofile_default_org.py b/shared/django_apps/codecov_auth/migrations/0020_ownerprofile_default_org.py index d517a42f..ce7d55b4 100644 --- a/shared/django_apps/codecov_auth/migrations/0020_ownerprofile_default_org.py +++ b/shared/django_apps/codecov_auth/migrations/0020_ownerprofile_default_org.py @@ -1,7 +1,6 @@ # Generated by Django 3.2.12 on 2023-01-19 19:06 import django.db.models.deletion -from django.conf import settings from django.db import migrations, models diff --git a/shared/django_apps/codecov_auth/migrations/0031_user_owner_user.py b/shared/django_apps/codecov_auth/migrations/0031_user_owner_user.py index 649e1543..e80b4c91 100644 --- a/shared/django_apps/codecov_auth/migrations/0031_user_owner_user.py +++ b/shared/django_apps/codecov_auth/migrations/0031_user_owner_user.py @@ -1,6 +1,5 @@ # Generated by Django 4.1.7 on 2023-05-22 17:53 -import uuid import django.contrib.postgres.fields.citext import django.db.models.deletion diff --git a/shared/django_apps/core/managers.py b/shared/django_apps/core/managers.py index c47cb4be..83acccc5 100644 --- a/shared/django_apps/core/managers.py +++ b/shared/django_apps/core/managers.py @@ -4,7 +4,6 @@ from django.db.models import ( Avg, Count, - DateTimeField, F, FloatField, IntegerField, @@ -132,7 +131,6 @@ def with_latest_coverage_change(self): branch) of each repository. Depends on having called "with_latest_commit_totals_before" with "include_previous_totals=True". """ - from shared.django_apps.core.models import Commit return self.annotate( latest_coverage=Cast( diff --git a/shared/django_apps/core/migrations/0034_remove_repository_cache.py b/shared/django_apps/core/migrations/0034_remove_repository_cache.py index 558c5f40..d0a45400 100644 --- a/shared/django_apps/core/migrations/0034_remove_repository_cache.py +++ b/shared/django_apps/core/migrations/0034_remove_repository_cache.py @@ -2,8 +2,6 @@ from django.db import migrations -from shared.django_apps.migration_utils import RiskyRemoveField - class Migration(migrations.Migration): dependencies = [ diff --git a/shared/django_apps/dummy_settings.py b/shared/django_apps/dummy_settings.py index c684c4b0..2f192046 100644 --- a/shared/django_apps/dummy_settings.py +++ b/shared/django_apps/dummy_settings.py @@ -1,6 +1,6 @@ from pathlib import Path -from shared.django_apps.db_settings import * +from shared.django_apps.db_settings import * # noqa: F403 # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent @@ -61,7 +61,7 @@ AUTH_USER_MODEL = "codecov_auth.User" # Needed as certain migrations refer to it -SKIP_RISKY_MIGRATION_STEPS = get_config("migrations", "skip_risky_steps", default=False) +SKIP_RISKY_MIGRATION_STEPS = get_config("migrations", "skip_risky_steps", default=False) # noqa: F405 TEST = True diff --git a/shared/django_apps/legacy_migrations/migrations/0001_initial.py b/shared/django_apps/legacy_migrations/migrations/0001_initial.py index 4d7b870a..f6cabbed 100644 --- a/shared/django_apps/legacy_migrations/migrations/0001_initial.py +++ b/shared/django_apps/legacy_migrations/migrations/0001_initial.py @@ -1,6 +1,5 @@ # Generated by Django 3.1.6 on 2021-03-15 20:15 -from django.conf import settings from django.db import migrations from shared.django_apps.legacy_migrations.migrations.legacy_sql.main.main import ( diff --git a/shared/metrics/__init__.py b/shared/metrics/__init__.py index 3a5dd1cf..94b1aa73 100644 --- a/shared/metrics/__init__.py +++ b/shared/metrics/__init__.py @@ -4,3 +4,13 @@ metrics = statsd start_prometheus = start_http_server + + +__all__ = [ + "Counter", + "Histogram", + "Summary", + "statsd", + "metrics", + "start_prometheus", +] diff --git a/shared/reports/filtered.py b/shared/reports/filtered.py index a11c0607..874b8e9d 100644 --- a/shared/reports/filtered.py +++ b/shared/reports/filtered.py @@ -88,7 +88,7 @@ def calculate_diff(self, all_file_segments): [ (i, fg(i)) for i, line in enumerate( - [l for l in segment["lines"] if l[0] != "-"], + [ln for ln in segment["lines"] if ln[0] != "-"], start=int(segment["header"][2]) or 1, ) if line[0] == "+" @@ -96,7 +96,7 @@ def calculate_diff(self, all_file_segments): ) for segment in all_file_segments ] - lines = [l for l in lines if l[1] is not None] + lines = [ln for ln in lines if ln[1] is not None] return self.calculate_totals_from_lines(lines) def get(self, ln): @@ -112,9 +112,9 @@ def _process_totals(self): return self.calculate_totals_from_lines(self.lines) @classmethod - def sum_of_complexity(cls, l): + def sum_of_complexity(cls, ln): # (hit, total) - c = l[1].complexity + c = ln[1].complexity if not c: # no coverage data provided return (0, 0) diff --git a/shared/reports/resources.py b/shared/reports/resources.py index 3a3760c6..891f2203 100644 --- a/shared/reports/resources.py +++ b/shared/reports/resources.py @@ -163,7 +163,7 @@ def calculate_diff(self, all_file_segments): [ fg(i) for i, line in enumerate( - [l for l in segment["lines"] if l[0] != "-"], + [ln for ln in segment["lines"] if ln[0] != "-"], start=int(segment["header"][2]) or 1, ) if line[0] == "+" @@ -436,9 +436,9 @@ def _process_totals(self) -> ReportTotals: partials = cov.count(LineType.partial) total_lines = hits + misses + partials - def sum_of_complexity(l): + def sum_of_complexity(ln): # (hit, total) - c = l[1].complexity + c = ln[1].complexity if not c: # no coverage data provided return (0, 0) @@ -1480,12 +1480,12 @@ def _ignore_to_func(ignore): if eof: if isinstance(eof, str): # Sometimes eof is 'N', not sure which cases - return lambda l: str(l) > eof or l in lines + return lambda ln: str(ln) > eof or ln in lines # This means the eof as a number: the last line of the file and # anything after that should be ignored - return lambda l: l > eof or l in lines + return lambda ln: ln > eof or ln in lines else: - return lambda l: l in lines + return lambda ln: ln in lines def _dumps_not_none(value): diff --git a/shared/reports/types.py b/shared/reports/types.py index 82766c66..0564c354 100644 --- a/shared/reports/types.py +++ b/shared/reports/types.py @@ -300,7 +300,7 @@ def __bool__(self): return self.session_count > 0 def append(self, totals: SessionTotals): - if totals == None: + if totals is None: log.warning("Trying to append None session total to SessionTotalsArray") return new_totals_index = self.session_count diff --git a/shared/rollouts/__init__.py b/shared/rollouts/__init__.py index 9fe8ab21..b3cd6dc3 100644 --- a/shared/rollouts/__init__.py +++ b/shared/rollouts/__init__.py @@ -1,7 +1,6 @@ import json import logging import os -from enum import Enum from functools import cached_property from typing import Optional diff --git a/shared/storage/aws.py b/shared/storage/aws.py index 84584cdd..433ef211 100644 --- a/shared/storage/aws.py +++ b/shared/storage/aws.py @@ -1,6 +1,5 @@ import gzip import logging -from io import BytesIO import boto3 from botocore.exceptions import ClientError @@ -167,9 +166,9 @@ def delete_file(self, bucket_name, path): bool: True if the deletion was succesful """ try: - response = self.storage_client.delete_object(Bucket=bucket_name, Key=path) + self.storage_client.delete_object(Bucket=bucket_name, Key=path) return True - except ClientError as e: + except ClientError: raise def delete_files(self, bucket_name, paths=[]): @@ -195,7 +194,7 @@ def delete_files(self, bucket_name, paths=[]): response = self.storage_client.delete_objects( Bucket=bucket_name, Delete=objects_to_delete ) - except ClientError as e: + except ClientError: raise deletes = [error.get("Key") for error in response.get("Deleted")] return [key in deletes for key in paths] @@ -215,7 +214,7 @@ def list_folder_contents(self, bucket_name, prefix=None, recursive=True): response = self.storage_client.list_objects( Bucket=bucket_name, Prefix=prefix ) - except ClientError as e: + except ClientError: raise contents = response.get("Contents") return [ diff --git a/shared/storage/gcp.py b/shared/storage/gcp.py index 3524daab..0f536263 100644 --- a/shared/storage/gcp.py +++ b/shared/storage/gcp.py @@ -1,6 +1,5 @@ import gzip import logging -from io import BytesIO import google.cloud.exceptions from google.cloud import storage diff --git a/shared/storage/minio.py b/shared/storage/minio.py index dbd0b975..8de8440b 100644 --- a/shared/storage/minio.py +++ b/shared/storage/minio.py @@ -228,7 +228,7 @@ def read_file(self, bucket_name, path, file_obj=None): f"File {path} does not exist in {bucket_name}" ) raise e - except MinioException as e: + except MinioException: raise """ diff --git a/shared/torngit/base.py b/shared/torngit/base.py index 77c98ce4..fe844855 100644 --- a/shared/torngit/base.py +++ b/shared/torngit/base.py @@ -219,8 +219,8 @@ def diff_to_json(self, diff): elif sol4 == "@@ -": # ex: "@@ -31,8 +31,8 @@ blah blah blah" # ex: "@@ -0,0 +1 @@" - l = get_start_of_line(source).groups() - segment = dict(header=[l[0], l[1], l[2], l[3]], lines=[]) + ln = get_start_of_line(source).groups() + segment = dict(header=[ln[0], ln[1], ln[2], ln[3]], lines=[]) _file["segments"].append(segment) elif source == "": diff --git a/shared/torngit/bitbucket.py b/shared/torngit/bitbucket.py index c5317a0c..7b8a3657 100644 --- a/shared/torngit/bitbucket.py +++ b/shared/torngit/bitbucket.py @@ -1,7 +1,7 @@ import logging import os import urllib.parse as urllib_parse -from typing import List, Optional +from typing import List import httpx from oauthlib import oauth1 @@ -802,7 +802,7 @@ async def find_pull_request( self, commit=None, branch=None, state="open", token=None ): state = {"open": "OPEN", "merged": "MERGED", "close": "DECLINED"}.get(state, "") - pulls, page = [], 0 + page = 0 async with self.get_client() as client: if commit or branch: while True: diff --git a/shared/torngit/bitbucket_server.py b/shared/torngit/bitbucket_server.py index 0d88e9b1..f98f54a9 100644 --- a/shared/torngit/bitbucket_server.py +++ b/shared/torngit/bitbucket_server.py @@ -150,10 +150,10 @@ def diff_to_json(self, diff_json): _file["segments"].append(segment) for seg in hunk["segments"]: t = seg["type"][0] - for l in seg["lines"]: + for ln in seg["lines"]: segment["lines"].append( ("-" if t == "R" else "+" if t == "A" else " ") - + l["line"] + + ln["line"] ) if results: diff --git a/shared/torngit/github.py b/shared/torngit/github.py index ea2fd52a..595372f0 100644 --- a/shared/torngit/github.py +++ b/shared/torngit/github.py @@ -1659,7 +1659,7 @@ async def set_commit_status( ), token=token, ) - except TorngitClientError as ce: + except TorngitClientError: raise if merge_commit: api_url = self.count_and_get_url_template( diff --git a/shared/typings/oauth_token_types.py b/shared/typings/oauth_token_types.py index 1e238683..46a75b6a 100644 --- a/shared/typings/oauth_token_types.py +++ b/shared/typings/oauth_token_types.py @@ -1,4 +1,4 @@ -from typing import Any, Awaitable, Callable, Optional, TypedDict +from typing import Awaitable, Callable, Optional, TypedDict class Token(TypedDict): diff --git a/shared/utils/sessions.py b/shared/utils/sessions.py index 8b98480e..aa70955d 100644 --- a/shared/utils/sessions.py +++ b/shared/utils/sessions.py @@ -1,7 +1,6 @@ from enum import Enum from shared.reports.types import ReportTotals -from shared.utils.migrate import migrate_totals class SessionType(Enum): diff --git a/shared/yaml/__init__.py b/shared/yaml/__init__.py index dc10a8f8..0631a837 100644 --- a/shared/yaml/__init__.py +++ b/shared/yaml/__init__.py @@ -16,3 +16,11 @@ fetch_current_yaml_from_provider_via_reference, ) from .user_yaml import UserYaml, merge_yamls + +__all__ = [ + "deepcopy", + "determine_commit_yaml_location", + "fetch_current_yaml_from_provider_via_reference", + "UserYaml", + "merge_yamls", +]