Skip to content

Commit e0f3729

Browse files
Merge pull request #2030 from VWS-Python/check-N818
Check and fix N818 ruff rule
2 parents 6650b92 + a644a2a commit e0f3729

30 files changed

+258
-246
lines changed

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,6 @@ ignore = [
215215
"D415",
216216
# Ruff warns that this conflicts with the formatter.
217217
"ISC001",
218-
# We have an existing interface to support and so we do not want to change
219-
# exception names.
220-
"N818",
221218
# Ignore "too-many-*" errors as they seem to get in the way more than
222219
# helping.
223220
"PLR0913",

src/mock_vws/_flask_server/vwq.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
from mock_vws._query_validators import run_query_validators
2020
from mock_vws._query_validators.exceptions import (
21-
ValidatorException,
21+
ValidatorError,
2222
)
2323
from mock_vws.database import VuforiaDatabase
2424
from mock_vws.image_matchers import (
@@ -100,8 +100,8 @@ def set_terminate_wsgi_input() -> None:
100100
request.environ["wsgi.input_terminated"] = True
101101

102102

103-
@CLOUDRECO_FLASK_APP.errorhandler(ValidatorException)
104-
def handle_exceptions(exc: ValidatorException) -> Response:
103+
@CLOUDRECO_FLASK_APP.errorhandler(ValidatorError)
104+
def handle_exceptions(exc: ValidatorError) -> Response:
105105
"""
106106
Return the error response associated with the given exception.
107107
"""

src/mock_vws/_flask_server/vws.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
from mock_vws._mock_common import json_dump
2323
from mock_vws._services_validators import run_services_validators
2424
from mock_vws._services_validators.exceptions import (
25-
Fail,
26-
TargetStatusNotSuccess,
27-
TargetStatusProcessing,
28-
ValidatorException,
25+
FailError,
26+
TargetStatusNotSuccessError,
27+
TargetStatusProcessingError,
28+
ValidatorError,
2929
)
3030
from mock_vws.database import VuforiaDatabase
3131
from mock_vws.image_matchers import (
@@ -131,8 +131,8 @@ def validate_request() -> None:
131131
)
132132

133133

134-
@VWS_FLASK_APP.errorhandler(ValidatorException)
135-
def handle_exceptions(exc: ValidatorException) -> Response:
134+
@VWS_FLASK_APP.errorhandler(ValidatorError)
135+
def handle_exceptions(exc: ValidatorError) -> Response:
136136
"""
137137
Return the error response associated with the given exception.
138138
"""
@@ -299,7 +299,7 @@ def delete_target(target_id: str) -> Response:
299299
)
300300

301301
if target.status == TargetStatuses.PROCESSING.value:
302-
raise TargetStatusProcessing
302+
raise TargetStatusProcessingError
303303

304304
databases_url = f"{settings.target_manager_base_url}/databases"
305305
requests.delete(
@@ -563,7 +563,7 @@ def update_target(target_id: str) -> Response:
563563
)
564564

565565
if target.status != TargetStatuses.SUCCESS.value:
566-
raise TargetStatusNotSuccess
566+
raise TargetStatusNotSuccessError
567567

568568
update_values: dict[str, str | int | float | bool | None] = {}
569569
if "width" in request_json:
@@ -578,7 +578,7 @@ def update_target(target_id: str) -> Response:
578578
"This is not allowed. "
579579
),
580580
)
581-
raise Fail(status_code=HTTPStatus.BAD_REQUEST)
581+
raise FailError(status_code=HTTPStatus.BAD_REQUEST)
582582
update_values["active_flag"] = active_flag
583583

584584
if "application_metadata" in request_json:
@@ -590,7 +590,7 @@ def update_target(target_id: str) -> Response:
590590
"This is not allowed."
591591
),
592592
)
593-
raise Fail(status_code=HTTPStatus.BAD_REQUEST)
593+
raise FailError(status_code=HTTPStatus.BAD_REQUEST)
594594
update_values["application_metadata"] = application_metadata
595595

596596
if "name" in request_json:

src/mock_vws/_query_validators/accept_header_validators.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import logging
66

7-
from mock_vws._query_validators.exceptions import InvalidAcceptHeader
7+
from mock_vws._query_validators.exceptions import InvalidAcceptHeaderError
88

99
_LOGGER = logging.getLogger(__name__)
1010

@@ -17,7 +17,7 @@ def validate_accept_header(request_headers: dict[str, str]) -> None:
1717
request_headers: The headers sent with the request.
1818
1919
Raises:
20-
InvalidAcceptHeader: The Accept header is given and is not
20+
InvalidAcceptHeaderError: The Accept header is given and is not
2121
'application/json' or '*/*'.
2222
"""
2323
accept = request_headers.get("Accept")
@@ -27,4 +27,4 @@ def validate_accept_header(request_headers: dict[str, str]) -> None:
2727
_LOGGER.warning(
2828
msg="The Accept header is not 'application/json' or '*/*'.",
2929
)
30-
raise InvalidAcceptHeader
30+
raise InvalidAcceptHeaderError

src/mock_vws/_query_validators/auth_validators.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
from mock_vws._database_matchers import get_database_matching_client_keys
1111
from mock_vws._query_validators.exceptions import (
12-
AuthenticationFailure,
13-
AuthHeaderMissing,
14-
MalformedAuthHeader,
12+
AuthenticationFailureError,
13+
AuthHeaderMissingError,
14+
MalformedAuthHeaderError,
1515
)
1616

1717
_LOGGER = logging.getLogger(__name__)
@@ -28,13 +28,13 @@ def validate_auth_header_exists(request_headers: dict[str, str]) -> None:
2828
request_headers: The headers sent with the request.
2929
3030
Raises:
31-
AuthHeaderMissing: There is no "Authorization" header.
31+
AuthHeaderMissingError: There is no "Authorization" header.
3232
"""
3333
if "Authorization" in request_headers:
3434
return
3535

3636
_LOGGER.warning(msg="There is no authorization header.")
37-
raise AuthHeaderMissing
37+
raise AuthHeaderMissingError
3838

3939

4040
def validate_auth_header_number_of_parts(
@@ -47,7 +47,8 @@ def validate_auth_header_number_of_parts(
4747
request_headers: The headers sent with the request.
4848
4949
Raises:
50-
MalformedAuthHeader: The "Authorization" header is not as expected.
50+
MalformedAuthHeaderError: The "Authorization" header is not as
51+
expected.
5152
"""
5253
header = request_headers["Authorization"]
5354
parts = header.split(" ")
@@ -56,7 +57,7 @@ def validate_auth_header_number_of_parts(
5657
return
5758

5859
_LOGGER.warning(msg="The authorization header is malformed.")
59-
raise MalformedAuthHeader
60+
raise MalformedAuthHeaderError
6061

6162

6263
def validate_client_key_exists(
@@ -71,7 +72,7 @@ def validate_client_key_exists(
7172
databases: All Vuforia databases.
7273
7374
Raises:
74-
AuthenticationFailure: The client key is unknown.
75+
AuthenticationFailureError: The client key is unknown.
7576
"""
7677
header = request_headers["Authorization"]
7778
first_part, _ = header.split(":")
@@ -81,7 +82,7 @@ def validate_client_key_exists(
8182
return
8283

8384
_LOGGER.warning(msg="The client key is unknown.")
84-
raise AuthenticationFailure
85+
raise AuthenticationFailureError
8586

8687

8788
def validate_auth_header_has_signature(
@@ -94,14 +95,14 @@ def validate_auth_header_has_signature(
9495
request_headers: The headers sent with the request.
9596
9697
Raises:
97-
MalformedAuthHeader: The "Authorization" header has no signature.
98+
MalformedAuthHeaderError: The "Authorization" header has no signature.
9899
"""
99100
header = request_headers["Authorization"]
100101
if header.count(":") == 1 and header.split(":")[1]:
101102
return
102103

103104
_LOGGER.warning(msg="The authorization header has no signature.")
104-
raise MalformedAuthHeader
105+
raise MalformedAuthHeaderError
105106

106107

107108
def validate_authorization(
@@ -122,7 +123,8 @@ def validate_authorization(
122123
databases: All Vuforia databases.
123124
124125
Raises:
125-
AuthenticationFailure: The "Authorization" header is not as expected.
126+
AuthenticationFailureError: The "Authorization" header is not as
127+
expected.
126128
"""
127129
try:
128130
get_database_matching_client_keys(
@@ -136,4 +138,4 @@ def validate_authorization(
136138
_LOGGER.warning(
137139
msg="The authorization header does not match any databases.",
138140
)
139-
raise AuthenticationFailure from ValueError
141+
raise AuthenticationFailureError from ValueError

src/mock_vws/_query_validators/content_length_validators.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import logging
66

77
from mock_vws._query_validators.exceptions import (
8-
AuthenticationFailureGoodFormatting,
9-
ContentLengthHeaderNotInt,
10-
ContentLengthHeaderTooLarge,
8+
AuthenticationFailureGoodFormattingError,
9+
ContentLengthHeaderNotIntError,
10+
ContentLengthHeaderTooLargeError,
1111
)
1212

1313
_LOGGER = logging.getLogger(__name__)
@@ -23,15 +23,16 @@ def validate_content_length_header_is_int(
2323
request_headers: The headers sent with the request.
2424
2525
Raises:
26-
ContentLengthHeaderNotInt: ``Content-Length`` header is not an integer.
26+
ContentLengthHeaderNotIntError: ``Content-Length`` header is not an
27+
integer.
2728
"""
2829
given_content_length = request_headers["Content-Length"]
2930

3031
try:
3132
int(given_content_length)
3233
except ValueError as exc:
3334
_LOGGER.warning(msg="The Content-Length header is not an integer.")
34-
raise ContentLengthHeaderNotInt from exc
35+
raise ContentLengthHeaderNotIntError from exc
3536

3637

3738
def validate_content_length_header_not_too_large(
@@ -46,8 +47,8 @@ def validate_content_length_header_not_too_large(
4647
request_body: The body of the request.
4748
4849
Raises:
49-
ContentLengthHeaderTooLarge: The given content length header says that
50-
the content length is greater than the body length.
50+
ContentLengthHeaderTooLargeError: The given content length header says
51+
that the content length is greater than the body length.
5152
"""
5253
given_content_length = request_headers["Content-Length"]
5354

@@ -56,7 +57,7 @@ def validate_content_length_header_not_too_large(
5657
# We skip coverage here as running a test to cover this is very slow.
5758
if given_content_length_value > body_length: # pragma: no cover
5859
_LOGGER.warning(msg="The Content-Length header is too large.")
59-
raise ContentLengthHeaderTooLarge
60+
raise ContentLengthHeaderTooLargeError
6061

6162

6263
def validate_content_length_header_not_too_small(
@@ -71,8 +72,9 @@ def validate_content_length_header_not_too_small(
7172
request_body: The body of the request.
7273
7374
Raises:
74-
AuthenticationFailureGoodFormatting: The given content length header
75-
says that the content length is smaller than the body length.
75+
AuthenticationFailureGoodFormattingError: The given content length
76+
header says that the content length is smaller than the body
77+
length.
7678
"""
7779
given_content_length = request_headers["Content-Length"]
7880

@@ -81,4 +83,4 @@ def validate_content_length_header_not_too_small(
8183

8284
if given_content_length_value < body_length:
8385
_LOGGER.warning(msg="The Content-Length header is too small.")
84-
raise AuthenticationFailureGoodFormatting
86+
raise AuthenticationFailureGoodFormattingError

src/mock_vws/_query_validators/content_type_validators.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
from email.message import EmailMessage
77

88
from mock_vws._query_validators.exceptions import (
9-
ImageNotGiven,
10-
NoBoundaryFound,
11-
NoContentType,
12-
UnsupportedMediaType,
9+
ImageNotGivenError,
10+
NoBoundaryFoundError,
11+
NoContentTypeError,
12+
UnsupportedMediaTypeError,
1313
)
1414

1515
_LOGGER = logging.getLogger(__name__)
@@ -27,17 +27,18 @@ def validate_content_type_header(
2727
request_body: The body of the request.
2828
2929
Raises:
30-
UnsupportedMediaType: The ``Content-Type`` header main part is not
30+
UnsupportedMediaTypeError: The ``Content-Type`` header main part is not
3131
'multipart/form-data'.
32-
NoBoundaryFound: The ``Content-Type`` header does not contain a
32+
NoBoundaryFoundError: The ``Content-Type`` header does not contain a
3333
boundary.
34-
ImageNotGiven: The boundary is not in the request body.
35-
NoContentType: The content type header is either empty or not given.
34+
ImageNotGivenError: The boundary is not in the request body.
35+
NoContentTypeError: The content type header is either empty or not
36+
given.
3637
"""
3738
content_type_header = request_headers.get("Content-Type", "")
3839
if not content_type_header:
3940
_LOGGER.warning(msg="The content type header is empty.")
40-
raise NoContentType
41+
raise NoContentTypeError
4142

4243
email_message = EmailMessage()
4344
email_message["Content-Type"] = request_headers["Content-Type"]
@@ -47,15 +48,15 @@ def validate_content_type_header(
4748
"The content type header main part is not multipart/form-data."
4849
),
4950
)
50-
raise UnsupportedMediaType
51+
raise UnsupportedMediaTypeError
5152

5253
boundary = email_message.get_boundary()
5354
if boundary is None:
5455
_LOGGER.warning(
5556
msg="The content type header does not contain a boundary.",
5657
)
57-
raise NoBoundaryFound
58+
raise NoBoundaryFoundError
5859

5960
if boundary.encode() not in request_body:
6061
_LOGGER.warning(msg="The boundary is not in the request body.")
61-
raise ImageNotGiven
62+
raise ImageNotGivenError

0 commit comments

Comments
 (0)