Skip to content

Use named arguments in more places #2464

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

Merged
merged 5 commits into from
Dec 23, 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
2 changes: 1 addition & 1 deletion admin/create_secrets_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def main() -> None:
""",
)

file.write_text(file_contents)
file.write_text(data=file_contents)
sys.stdout.write(f"Created database {file.name}\n")
files_to_create.pop()

Expand Down
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
# This method of getting the release from the version goes hand in hand with
# the ``post-release`` versioning scheme chosen in the ``setuptools-scm``
# configuration.
release = version.split(".post")[0]
release = version.split(sep=".post")[0]


project_metadata = importlib.metadata.metadata(distribution_name=project)
Expand Down
17 changes: 11 additions & 6 deletions src/mock_vws/_database_matchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ def get_database_matching_client_keys(
Raises:
ValueError: No database matches the given request.
"""
content_type = request_headers.get("Content-Type", "").split(sep=";")[0]
auth_header = request_headers.get("Authorization")
date = request_headers.get("Date", "")
request_headers_dict = dict(request_headers)
content_type = request_headers_dict.get("Content-Type", "").split(sep=";")[
0
]
auth_header = request_headers_dict.get("Authorization")
date = request_headers_dict.get("Date", "")

for database in databases:
expected_authorization_header = authorization_header(
Expand Down Expand Up @@ -80,9 +83,11 @@ def get_database_matching_server_keys(
Raises:
ValueError: No database matches the given request.
"""
content_type = request_headers.get("Content-Type", "").split(sep=";")[0]
auth_header = request_headers.get("Authorization")
date = request_headers.get("Date", "")
request_headers_dict = dict(request_headers)
content_type_header = request_headers_dict.get("Content-Type", "")
content_type = content_type_header.split(sep=";")[0]
auth_header = request_headers_dict.get("Authorization")
date = request_headers_dict.get("Date", "")

for database in databases:
expected_authorization_header = authorization_header(
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_flask_server/healthcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def flask_app_healthy(port: int) -> bool:
"""
Check if the Flask app is healthy.
"""
conn = http.client.HTTPConnection("localhost", port)
conn = http.client.HTTPConnection(host="localhost", port=port)
try:
conn.request("GET", "/some-random-endpoint")
conn.request(method="GET", url="/some-random-endpoint")
response = conn.getresponse()
except (TimeoutError, http.client.HTTPException, socket.gaierror):
return False
Expand Down
14 changes: 7 additions & 7 deletions src/mock_vws/_flask_server/target_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class TargetManagerSettings(BaseSettings):


@TARGET_MANAGER_FLASK_APP.route(
"/databases/<string:database_name>",
rule="/databases/<string:database_name>",
methods=[HTTPMethod.DELETE],
)
@beartype
Expand All @@ -88,7 +88,7 @@ def delete_database(database_name: str) -> Response:
return Response(response="", status=HTTPStatus.OK)


@TARGET_MANAGER_FLASK_APP.route("/databases", methods=[HTTPMethod.GET])
@TARGET_MANAGER_FLASK_APP.route(rule="/databases", methods=[HTTPMethod.GET])
@beartype
def get_databases() -> Response:
"""
Expand All @@ -101,7 +101,7 @@ def get_databases() -> Response:
)


@TARGET_MANAGER_FLASK_APP.route("/databases", methods=[HTTPMethod.POST])
@TARGET_MANAGER_FLASK_APP.route(rule="/databases", methods=[HTTPMethod.POST])
@beartype
def create_database() -> Response:
"""Create a new database.
Expand Down Expand Up @@ -184,7 +184,7 @@ def create_database() -> Response:
TARGET_MANAGER.add_database(database=database)
except ValueError as exc:
return Response(
response=str(exc),
response=str(object=exc),
status=HTTPStatus.CONFLICT,
)

Expand All @@ -195,7 +195,7 @@ def create_database() -> Response:


@TARGET_MANAGER_FLASK_APP.route(
"/databases/<string:database_name>/targets",
rule="/databases/<string:database_name>/targets",
methods=[HTTPMethod.POST],
)
@beartype
Expand Down Expand Up @@ -233,7 +233,7 @@ def create_target(database_name: str) -> Response:


@TARGET_MANAGER_FLASK_APP.route(
"/databases/<string:database_name>/targets/<string:target_id>",
rule="/databases/<string:database_name>/targets/<string:target_id>",
methods={HTTPMethod.DELETE},
)
@beartype
Expand All @@ -258,7 +258,7 @@ def delete_target(database_name: str, target_id: str) -> Response:


@TARGET_MANAGER_FLASK_APP.route(
"/databases/<string:database_name>/targets/<string:target_id>",
rule="/databases/<string:database_name>/targets/<string:target_id>",
methods=[HTTPMethod.PUT],
)
def update_target(database_name: str, target_id: str) -> Response:
Expand Down
4 changes: 2 additions & 2 deletions src/mock_vws/_flask_server/vwq.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def set_terminate_wsgi_input() -> None:
request.environ["wsgi.input_terminated"] = True


@CLOUDRECO_FLASK_APP.errorhandler(ValidatorError)
@CLOUDRECO_FLASK_APP.errorhandler(code_or_exception=ValidatorError)
def handle_exceptions(exc: ValidatorError) -> Response:
"""
Return the error response associated with the given exception.
Expand All @@ -125,7 +125,7 @@ def handle_exceptions(exc: ValidatorError) -> Response:
return response


@CLOUDRECO_FLASK_APP.route("/v1/query", methods=[HTTPMethod.POST])
@CLOUDRECO_FLASK_APP.route(rule="/v1/query", methods=[HTTPMethod.POST])
def query() -> Response:
"""
Perform an image recognition query.
Expand Down
27 changes: 17 additions & 10 deletions src/mock_vws/_flask_server/vws.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def validate_request() -> None:
)


@VWS_FLASK_APP.errorhandler(ValidatorError)
@VWS_FLASK_APP.errorhandler(code_or_exception=ValidatorError)
def handle_exceptions(exc: ValidatorError) -> Response:
"""
Return the error response associated with the given exception.
Expand All @@ -156,7 +156,7 @@ def handle_exceptions(exc: ValidatorError) -> Response:
return response


@VWS_FLASK_APP.route("/targets", methods=[HTTPMethod.POST])
@VWS_FLASK_APP.route(rule="/targets", methods=[HTTPMethod.POST])
@beartype
def add_target() -> Response:
"""Add a target.
Expand Down Expand Up @@ -228,7 +228,9 @@ def add_target() -> Response:
)


@VWS_FLASK_APP.route("/targets/<string:target_id>", methods=[HTTPMethod.GET])
@VWS_FLASK_APP.route(
rule="/targets/<string:target_id>", methods=[HTTPMethod.GET]
)
@beartype
def get_target(target_id: str) -> Response:
"""Get details of a target.
Expand Down Expand Up @@ -283,7 +285,7 @@ def get_target(target_id: str) -> Response:


@VWS_FLASK_APP.route(
"/targets/<string:target_id>",
rule="/targets/<string:target_id>",
methods=[HTTPMethod.DELETE],
)
def delete_target(target_id: str) -> Response:
Expand Down Expand Up @@ -337,7 +339,7 @@ def delete_target(target_id: str) -> Response:
)


@VWS_FLASK_APP.route("/summary", methods=[HTTPMethod.GET])
@VWS_FLASK_APP.route(rule="/summary", methods=[HTTPMethod.GET])
@beartype
def database_summary() -> Response:
"""Get a database summary report.
Expand Down Expand Up @@ -390,7 +392,10 @@ def database_summary() -> Response:
)


@VWS_FLASK_APP.route("/summary/<string:target_id>", methods=[HTTPMethod.GET])
@VWS_FLASK_APP.route(
rule="/summary/<string:target_id>",
methods=[HTTPMethod.GET],
)
def target_summary(target_id: str) -> Response:
"""Get a summary report for a target.

Expand All @@ -415,7 +420,7 @@ def target_summary(target_id: str) -> Response:
"result_code": ResultCodes.SUCCESS.value,
"database_name": database.database_name,
"target_name": target.name,
"upload_date": target.upload_date.strftime("%Y-%m-%d"),
"upload_date": target.upload_date.strftime(format="%Y-%m-%d"),
"active_flag": target.active_flag,
"tracking_rating": target.tracking_rating,
"total_recos": target.total_recos,
Expand All @@ -441,7 +446,7 @@ def target_summary(target_id: str) -> Response:


@VWS_FLASK_APP.route(
"/duplicates/<string:target_id>",
rule="/duplicates/<string:target_id>",
methods=[HTTPMethod.GET],
)
@beartype
Expand Down Expand Up @@ -503,7 +508,7 @@ def get_duplicates(target_id: str) -> Response:
)


@VWS_FLASK_APP.route("/targets", methods=[HTTPMethod.GET])
@VWS_FLASK_APP.route(rule="/targets", methods=[HTTPMethod.GET])
def target_list() -> Response:
"""Get a list of all targets.

Expand Down Expand Up @@ -543,7 +548,9 @@ def target_list() -> Response:
)


@VWS_FLASK_APP.route("/targets/<string:target_id>", methods=[HTTPMethod.PUT])
@VWS_FLASK_APP.route(
rule="/targets/<string:target_id>", methods=[HTTPMethod.PUT]
)
def update_target(target_id: str) -> Response:
"""Update a target.

Expand Down
7 changes: 5 additions & 2 deletions src/mock_vws/_query_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ def get_query_match_response_text(
content_length=len(request_body),
)

max_num_results = fields.get("max_num_results", "1")
include_target_data = fields.get("include_target_data", "top").lower()
max_num_results = fields.get(key="max_num_results", default="1")
include_target_data = fields.get(
key="include_target_data",
default="top",
).lower()

image_part = files["image"]
image_value = image_part.stream.read()
Expand Down
3 changes: 2 additions & 1 deletion src/mock_vws/_query_validators/content_type_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def validate_content_type_header(
NoContentTypeError: The content type header is either empty or not
given.
"""
content_type_header = request_headers.get("Content-Type", "")
request_headers_dict = dict(request_headers)
content_type_header = request_headers_dict.get("Content-Type", "")
if not content_type_header:
_LOGGER.warning(msg="The content type header is empty.")
raise NoContentTypeError
Expand Down
2 changes: 1 addition & 1 deletion src/mock_vws/_query_validators/date_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def validate_date_in_range(*, request_headers: Mapping[str, str]) -> None:
date_header = request_headers["Date"]
gmt = ZoneInfo(key="GMT")

date = datetime.datetime.fromtimestamp(0, tz=gmt)
date = datetime.datetime.fromtimestamp(timestamp=0, tz=gmt)
for date_format in _accepted_date_formats():
with contextlib.suppress(ValueError):
date = datetime.datetime.strptime(
Expand Down
Loading
Loading