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

Ruff: Fix issues via "target-version" #10846

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 2 additions & 3 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os
import re
from datetime import datetime
from typing import List

import six
import tagulous
Expand Down Expand Up @@ -1517,7 +1516,7 @@ def get_engagement(self, obj):
)

def validate(self, data):
def validate_findings_have_same_engagement(finding_objects: List[Finding]):
def validate_findings_have_same_engagement(finding_objects: list[Finding]):
engagements = finding_objects.values_list("test__engagement__id", flat=True).distinct().count()
if engagements > 1:
msg = "You are not permitted to add findings from multiple engagements"
Expand Down Expand Up @@ -2043,7 +2042,7 @@ def get_findings_count(self, obj) -> int:
return obj.findings_count

# TODO: maybe extend_schema_field is needed here?
def get_findings_list(self, obj) -> List[int]:
def get_findings_list(self, obj) -> list[int]:
return obj.open_findings_list


Expand Down
2 changes: 1 addition & 1 deletion dojo/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ def get_model_fields(default_fields, extra_fields=()):
def get_model_default_fields(model):
return tuple(
field.name for field in model._meta.fields if
isinstance(field, (models.CharField, models.TextField))
isinstance(field, models.CharField | models.TextField)
)
30 changes: 14 additions & 16 deletions dojo/engagement/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from functools import reduce
from tempfile import NamedTemporaryFile
from time import strftime
from typing import List, Optional, Tuple

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -427,7 +426,7 @@ def get_risks_accepted(self, eng):
def get_filtered_tests(
self,
request: HttpRequest,
queryset: List[Test],
queryset: list[Test],
engagement: Engagement,
):
filter_string_matching = get_system_setting("filter_string_matching", False)
Expand Down Expand Up @@ -710,9 +709,9 @@ def get_development_environment(
def get_engagement_or_product(
self,
user: Dojo_User,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
) -> Tuple[Engagement, Product, Product | Engagement]:
engagement_id: int | None = None,
product_id: int | None = None,
) -> tuple[Engagement, Product, Product | Engagement]:
"""Using the path parameters, either fetch the product or engagement"""
engagement = product = engagement_or_product = None
# Get the product if supplied
Expand Down Expand Up @@ -769,7 +768,7 @@ def get_jira_form(
self,
request: HttpRequest,
engagement_or_product: Engagement | Product,
) -> Tuple[JIRAImportScanForm | None, bool]:
) -> tuple[JIRAImportScanForm | None, bool]:
"""Returns a JiraImportScanForm if jira is enabled"""
jira_form = None
push_all_jira_issues = False
Expand All @@ -794,7 +793,7 @@ def get_product_tab(
self,
product: Product,
engagement: Engagement,
) -> Tuple[Product_Tab, dict]:
) -> tuple[Product_Tab, dict]:
"""
Determine how the product tab will be rendered, and what tab will be selected
as currently active
Expand All @@ -811,9 +810,9 @@ def get_product_tab(
def handle_request(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
) -> Tuple[HttpRequest, dict]:
engagement_id: int | None = None,
product_id: int | None = None,
) -> tuple[HttpRequest, dict]:
"""
Process the common behaviors between request types, and then return
the request and context dict back to be rendered
Expand Down Expand Up @@ -1046,8 +1045,8 @@ def failure_redirect(
def get(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
engagement_id: int | None = None,
product_id: int | None = None,
) -> HttpResponse:
"""Process GET requests for the Import View"""
# process the request and path parameters
Expand All @@ -1062,8 +1061,8 @@ def get(
def post(
self,
request: HttpRequest,
engagement_id: Optional[int] = None,
product_id: Optional[int] = None,
engagement_id: int | None = None,
product_id: int | None = None,
) -> HttpResponse:
"""Process POST requests for the Import View"""
# process the request and path parameters
Expand Down Expand Up @@ -1555,8 +1554,7 @@ def get_engagements(request):
if not url:
msg = "Please use the export button when exporting engagements"
raise ValidationError(msg)
if url.startswith("url="):
url = url[4:]
url = url.removeprefix("url=")

path_items = list(filter(None, re.split(r"/|\?", url)))

Expand Down
23 changes: 11 additions & 12 deletions dojo/finding/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import mimetypes
from collections import OrderedDict, defaultdict
from itertools import chain
from typing import Optional

from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -265,9 +264,9 @@ class BaseListFindings:
def __init__(
self,
filter_name: str = "All",
product_id: Optional[int] = None,
engagement_id: Optional[int] = None,
test_id: Optional[int] = None,
product_id: int | None = None,
engagement_id: int | None = None,
test_id: int | None = None,
order_by: str = "numerical_severity",
prefetch_type: str = "all",
):
Expand Down Expand Up @@ -420,7 +419,7 @@ def add_breadcrumbs(self, request: HttpRequest, context: dict):

return request, context

def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
# Store the product and engagement ids
self.product_id = product_id
self.engagement_id = engagement_id
Expand All @@ -446,43 +445,43 @@ def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement


class ListOpenFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Open"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListVerifiedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Verified"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListOutOfScopeFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Out of Scope"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListFalsePositiveFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "False Positive"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListInactiveFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Inactive"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListAcceptedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Accepted"
return super().get(request, product_id=product_id, engagement_id=engagement_id)


class ListClosedFindings(ListFindings):
def get(self, request: HttpRequest, product_id: Optional[int] = None, engagement_id: Optional[int] = None):
def get(self, request: HttpRequest, product_id: int | None = None, engagement_id: int | None = None):
self.filter_name = "Closed"
self.order_by = "-mitigated"
return super().get(request, product_id=product_id, engagement_id=engagement_id)
Expand Down
3 changes: 1 addition & 2 deletions dojo/finding_group/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ def view_finding_group(request, fgid):
if jira_issue:
# See if the submitted issue was a issue key or the full URL
jira_instance = jira_helper.get_jira_project(finding_group).jira_instance
if jira_issue.startswith(jira_instance.url + "/browse/"):
jira_issue = jira_issue[len(jira_instance.url + "/browse/"):]
jira_issue = jira_issue.removeprefix(jira_instance.url + "/browse/")

if finding_group.has_jira_issue and not jira_issue == jira_helper.get_jira_key(finding_group):
jira_helper.unlink_jira(request, finding_group)
Expand Down
5 changes: 1 addition & 4 deletions dojo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2382,10 +2382,7 @@ def get_jira_issue_template_dir_choices():
# template_list.append((os.path.join(base_dir, filename), filename))

for dirname in dirnames:
if base_dir.startswith(settings.TEMPLATE_DIR_PREFIX):
clean_base_dir = base_dir[len(settings.TEMPLATE_DIR_PREFIX):]
else:
clean_base_dir = base_dir
clean_base_dir = base_dir.removeprefix(settings.TEMPLATE_DIR_PREFIX)
template_dir_list.append((os.path.join(clean_base_dir, dirname), dirname))

logger.debug("templates: %s", template_dir_list)
Expand Down
3 changes: 1 addition & 2 deletions dojo/home/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from collections import defaultdict
from datetime import timedelta
from typing import Dict

from dateutil.relativedelta import relativedelta
from django.db.models import Count, Q
Expand Down Expand Up @@ -75,7 +74,7 @@ def support(request: HttpRequest) -> HttpResponse:
return render(request, "dojo/support.html", {})


def get_severities_all(findings) -> Dict[str, int]:
def get_severities_all(findings) -> dict[str, int]:
severities_all = findings.values("severity").annotate(count=Count("severity")).order_by()
return defaultdict(lambda: 0, {s["severity"]: s["count"] for s in severities_all})

Expand Down
30 changes: 15 additions & 15 deletions dojo/importers/auto_create_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from datetime import datetime, timedelta
from typing import Any, Optional
from typing import Any

from crum import get_current_user
from django.db import transaction
Expand Down Expand Up @@ -113,7 +113,7 @@ def process_import_meta_data_from_dict(
"""
def get_target_product_type_if_exists(
self,
product_type_name: Optional[str] = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product_Type | None:
"""
Expand All @@ -128,8 +128,8 @@ def get_target_product_type_if_exists(

def get_target_product_if_exists(
self,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
product_name: str | None = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product | None:
"""
Expand Down Expand Up @@ -168,7 +168,7 @@ def get_target_product_by_id_if_exists(
def get_target_engagement_if_exists(
self,
engagement_id: int = 0,
engagement_name: Optional[str] = None,
engagement_name: str | None = None,
product: Product = None,
**kwargs: dict,
) -> Engagement | None:
Expand All @@ -191,8 +191,8 @@ def get_target_engagement_if_exists(
def get_target_test_if_exists(
self,
test_id: int = 0,
test_title: Optional[str] = None,
scan_type: Optional[str] = None,
test_title: str | None = None,
scan_type: str | None = None,
engagement: Engagement = None,
**kwargs: dict,
) -> Test | None:
Expand Down Expand Up @@ -220,7 +220,7 @@ def get_target_test_if_exists(
"""
def get_or_create_product_type(
self,
product_type_name: Optional[str] = None,
product_type_name: str | None = None,
**kwargs: dict,
) -> Product_Type:
"""
Expand All @@ -243,8 +243,8 @@ def get_or_create_product_type(

def get_or_create_product(
self,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
product_name: str | None = None,
product_type_name: str | None = None,
*,
auto_create_context: bool = False,
**kwargs: dict,
Expand Down Expand Up @@ -278,14 +278,14 @@ def get_or_create_product(
def get_or_create_engagement(
self,
engagement_id: int = 0,
engagement_name: Optional[str] = None,
product_name: Optional[str] = None,
product_type_name: Optional[str] = None,
engagement_name: str | None = None,
product_name: str | None = None,
product_type_name: str | None = None,
*,
auto_create_context: bool = False,
deduplication_on_engagement: bool = False,
source_code_management_uri: Optional[str] = None,
target_end: Optional[datetime] = None,
source_code_management_uri: str | None = None,
target_end: datetime | None = None,
**kwargs: dict,
) -> Engagement:
"""Fetches an engagement by name or ID if one already exists."""
Expand Down
Loading
Loading