Fix flake8-bugbear B042 violations in exception classes

This commit is contained in:
Alex Janousek
2025-10-24 14:03:35 -04:00
parent 6a4b69a493
commit 1357664dbd
12 changed files with 57 additions and 28 deletions

View File

@@ -29,6 +29,7 @@ GENERAL_TOKEN_ERROR_MESSAGE = TOKEN_MESSAGE_ONE + TOKEN_MESSAGE_TWO
class AuthError(Exception):
def __init__(self, message, code, service_id=None, api_key_id=None):
super().__init__(message, code, service_id, api_key_id)
self.message = {"token": [message]}
self.short_message = message
self.code = code

View File

@@ -4,6 +4,7 @@ from flask import current_app
class DocumentDownloadError(Exception):
def __init__(self, message, status_code):
super().__init__(message, status_code)
self.message = message
self.status_code = status_code

View File

@@ -10,6 +10,7 @@ class SmsClientResponseException(ClientException):
"""
def __init__(self, message):
super().__init__(message)
self.message = message
def __str__(self):

View File

@@ -699,7 +699,9 @@ def query_organization_sms_usage_for_year(organization_id, year):
)
def fetch_usage_year_for_organization(organization_id, year, include_all_services=False):
def fetch_usage_year_for_organization(
organization_id, year, include_all_services=False
):
year_start, year_end = get_calendar_year_dates(year)
today = utc_now().date()

View File

@@ -351,10 +351,7 @@ def dao_get_notification_counts_for_organization(service_ids, current_year):
end_date = datetime(current_year + 1, 6, 16)
stmt1 = (
select(
Notification.service_id,
func.count().label("count")
)
select(Notification.service_id, func.count().label("count"))
.where(
Notification.service_id.in_(service_ids),
Notification.status
@@ -370,10 +367,7 @@ def dao_get_notification_counts_for_organization(service_ids, current_year):
)
stmt2 = (
select(
NotificationHistory.service_id,
func.count().label("count")
)
select(NotificationHistory.service_id, func.count().label("count"))
.where(
NotificationHistory.service_id.in_(service_ids),
NotificationHistory.status

View File

@@ -15,7 +15,7 @@ class InvalidRequest(Exception):
fields = []
def __init__(self, message, status_code):
super().__init__()
super().__init__(message, status_code)
self.message = message
self.status_code = status_code
@@ -115,16 +115,20 @@ class TooManyRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded send limits ({}) for today"
def __init__(self, sending_limit):
def __init__(self, sending_limit): # noqa: B042
self.message = self.message_template.format(sending_limit)
self.sending_limit = sending_limit
super().__init__(self.message, self.status_code)
class TotalRequestsError(InvalidRequest):
status_code = 429
message_template = "Exceeded total application limits ({}) for today"
def __init__(self, sending_limit):
def __init__(self, sending_limit): # noqa: B042
self.message = self.message_template.format(sending_limit)
self.sending_limit = sending_limit
super().__init__(self.message, self.status_code)
class RateLimitError(InvalidRequest):
@@ -133,7 +137,7 @@ class RateLimitError(InvalidRequest):
"Exceeded rate limit for key type {} of {} requests per {} seconds"
)
def __init__(self, sending_limit, interval, key_type):
def __init__(self, sending_limit, interval, key_type): # noqa: B042
# normal keys are spoken of as "live" in the documentation
# so using this in the error messaging
if key_type == KeyType.NORMAL:
@@ -142,12 +146,17 @@ class RateLimitError(InvalidRequest):
self.message = self.message_template.format(
key_type.upper(), sending_limit, interval
)
self.sending_limit = sending_limit
self.interval = interval
self.key_type = key_type
super().__init__(self.message, self.status_code)
class BadRequestError(InvalidRequest):
message = "An error occurred"
def __init__(self, fields=None, message=None, status_code=400):
self.status_code = status_code
def __init__(self, fields=None, message=None, status_code=400): # noqa: B042
self.fields = fields or []
self.message = message if message else self.message
self.status_code = status_code
super().__init__(self.message, self.status_code)

View File

@@ -1,5 +1,6 @@
class DVLAException(Exception):
def __init__(self, message):
super().__init__(message)
self.message = message

View File

@@ -153,7 +153,9 @@ def get_organization_services_usage(organization_id):
return jsonify(result="error", message="No valid year provided"), 400
include_all = request.args.get("include_all_services", "false").lower() == "true"
services = fetch_usage_year_for_organization(organization_id, year, include_all_services=include_all)
services = fetch_usage_year_for_organization(
organization_id, year, include_all_services=include_all
)
list_services = services.values()
sorted_services = sorted(
list_services, key=lambda s: (-s["active"], s["service_name"].lower())
@@ -266,7 +268,9 @@ def send_notifications_on_mou_signed(organization_id):
)
@organization_blueprint.route("/<uuid:organization_id>/message-allowance", methods=["GET"])
@organization_blueprint.route(
"/<uuid:organization_id>/message-allowance", methods=["GET"]
)
def get_organization_message_allowance(organization_id):
check_suspicious_id(organization_id)
@@ -276,11 +280,16 @@ def get_organization_message_allowance(organization_id):
services = dao_get_organization_services(organization_id)
if not services:
return jsonify({
"messages_sent": 0,
"messages_remaining": 0,
"total_message_limit": 0,
}), 200
return (
jsonify(
{
"messages_sent": 0,
"messages_remaining": 0,
"total_message_limit": 0,
}
),
200,
)
current_year = datetime.now(tz=ZoneInfo("UTC")).year
service_ids = [service.id for service in services]
@@ -293,8 +302,13 @@ def get_organization_message_allowance(organization_id):
total_message_limit = sum(s.total_message_limit for s in services)
total_messages_remaining = total_message_limit - total_messages_sent
return jsonify({
"messages_sent": total_messages_sent,
"messages_remaining": total_messages_remaining,
"total_message_limit": total_message_limit,
}), 200
return (
jsonify(
{
"messages_sent": total_messages_sent,
"messages_remaining": total_messages_remaining,
"total_message_limit": total_message_limit,
}
),
200,
)

View File

@@ -18,6 +18,7 @@ class TokenError(Exception):
else TOKEN_ERROR_DEFAULT_ERROR_MESSAGE
)
self.token = token
super().__init__(self.message, token)
class TokenExpiredError(TokenError):
@@ -48,6 +49,7 @@ class APIError(Exception):
def __init__(self, response: Response = None, message: str = None):
self.response = response
self._message = message
super().__init__(response, message)
def __str__(self):
return f"{self.status_code} - {self.message}"

View File

@@ -4,6 +4,7 @@ from flask import current_app
class AntivirusError(Exception):
def __init__(self, message=None, status_code=None):
super().__init__(message, status_code)
self.message = message
self.status_code = status_code

View File

@@ -4,6 +4,7 @@ from flask import current_app
class ZendeskError(Exception):
def __init__(self, response):
super().__init__(str(response))
self.response = response

View File

@@ -963,7 +963,9 @@ def test_get_organization_message_allowance(admin_request, sample_organization,
mock_get_counts.assert_called_once_with([service_1.id, service_2.id], 2025)
def test_get_organization_message_allowance_no_services(admin_request, sample_organization):
def test_get_organization_message_allowance_no_services(
admin_request, sample_organization
):
response = admin_request.get(
"organization.get_organization_message_allowance",
organization_id=sample_organization.id,