Files
paperbenni bec3d3ad9c Drop support for Python 3.8
Drop Python 3.8 support in 5 Pulsar-compatible packages
(job_metrics, tool_util, tool_util_models, util, objectstore)
and run pyupgrade --py310-plus on their source code.

- Bump requires-python from >=3.8 to >=3.10 in all 5 packages
- Remove Python 3.8 and 3.9 classifiers
- Remove ruff per-file-ignores for UP rules on these paths
- Remove backports.zoneinfo conditional dependency
- Remove pydyf<0.11 pin from conditional-requirements.txt
- Update Makefile pyupgrade target (remove PY38_PYUPGRADE_PATHS)
- Update CI workflow to test with Python 3.10 instead of 3.8

Clean up unused deprecated typing imports after pyupgrade

Remove now-unused typing imports (Dict, List, Optional, Set, Tuple,
Type, Union) that became dead after pyupgrade --py310-plus converted
annotations to use built-in types and | syntax.

Also run ruff check --fix --select=UP007,UP045 across the entire
codebase to convert remaining Optional[X] -> X | None and
Union[X, Y] -> X | Y patterns.

Enable ruff UP007/UP045 for Python 3.10 union syntax

Remove UP007 (Union[X,Y] -> X | Y) and UP045 (Optional[X] -> X | None)
from the ruff ignore list and convert all type aliases across the
codebase. These rules were deferred while Python 3.9 was supported;
requires-python is now >=3.10.

A custom script was used because neither ruff --fix nor
pyupgrade --py310-plus converts Optional[X]/Union[X,Y] in type alias
positions (e.g. X = Union[A, B]) — they only handle annotation
positions (e.g. def f(x: Optional[int])). All 167 violations were
module-level type aliases. A few edge cases were fixed manually:
single-element Union[X,], typing.Union qualified refs, runtime
Optional[type] calls, and Annotated[Optional[...]] pydantic fields.

Fix UP007 autofix regression with string forward reference type aliases

Commit fa6bd955a0 enabled ruff UP007/UP045 and auto-fixed
module-level type aliases using Union with string forward
references, producing invalid 'str | str' expressions.

This was a known ruff bug (charliermarsh/ruff#826) that has since
been fixed in later ruff versions, but this codebase was
converted before the fix was in place.

Revert to Union syntax and restore TYPE_CHECKING imports that
ruff's TCH rule cleaned up as a side effect when it thought the
forward references were unused.
2026-06-25 15:12:05 +01:00

119 lines
4.2 KiB
Python

"""Utility methods for making assertions about Galaxy API responses, etc..."""
from typing import (
Any,
cast,
)
from requests import Response
from galaxy.exceptions.error_codes import ErrorCode
def assert_status_code_is(response: Response, expected_status_code: int, failure_message: str | None = None):
"""Assert that the supplied response has the expect status code."""
response_status_code = response.status_code
if expected_status_code != response_status_code:
_report_status_code_error(response, expected_status_code, failure_message)
def assert_status_code_is_ok(response: Response, failure_message: str | None = None):
"""Assert that the supplied response is okay.
This is an alternative to ``response.raise_for_status()`` with a more detailed
error message.
.. seealso:: :py:meth:`requests.Response.raise_for_status()`
"""
response_status_code = response.status_code
is_two_hundred_status_code = response_status_code >= 200 and response_status_code <= 300
if not is_two_hundred_status_code:
_report_status_code_error(response, "2XX", failure_message)
def assert_status_code_is_not_ok(response: Response, failure_message: str | None = None):
"""Assert that the supplied response is not okay.
.. seealso:: :py:meth:`assert_status_code_is_ok`
"""
response_status_code = response.status_code
is_two_hundred_status_code = response_status_code >= 200 and response_status_code <= 300
if is_two_hundred_status_code:
_report_status_code_error(response, "2XX", failure_message)
def _report_status_code_error(response: Response, expected_status_code: str | int, failure_message: str | None):
try:
body = response.json()
except Exception:
body = f"INVALID JSON RESPONSE <{response.text}>"
assertion_message = (
f"Request status code ({response.status_code}) was not expected value {expected_status_code}. Body was {body}"
)
if failure_message:
assertion_message = f"{failure_message}. {assertion_message}"
raise AssertionError(assertion_message)
def assert_has_keys(response: dict, *keys: str):
"""Assert that the supplied response (dict) has the supplied keys."""
for key in keys:
assert key in response, f"Response [{response}] does not contain key [{key}]"
def assert_not_has_keys(response: dict, *keys: str):
"""Assert that the supplied response (dict) does not have the supplied keys."""
for key in keys:
assert key not in response, f"Response [{response}] contains invalid key [{key}]"
def assert_error_code_is(response: Response | dict, error_code: int | ErrorCode):
"""Assert that the supplied response has the supplied Galaxy error code.
Galaxy error codes can be imported from :py:mod:`galaxy.exceptions.error_codes`
to test against.
"""
as_dict = _as_dict(response)
assert_has_keys(as_dict, "err_code")
err_code = as_dict["err_code"]
assert err_code == int(error_code), f"Expected Galaxy error code {error_code}, obtained {err_code}"
def assert_object_id_error(response: Response):
# for tests that use fake object IDs - API might throw MalformedId (400) or
# or ObjectNotFound (404) - depending if the ID happens to be parseable with
# servers API key.
error_code = response.status_code
assert error_code in [400, 404]
if error_code == 400:
assert_error_code_is(response, 400009)
else:
assert_error_code_is(response, 404001)
def assert_error_message_contains(response: Response | dict, expected_contains: str):
as_dict = _as_dict(response)
assert_has_keys(as_dict, "err_msg")
err_msg = as_dict["err_msg"]
assert expected_contains in err_msg, f"Expected error message [{err_msg}] to contain [{expected_contains}]."
def _as_dict(response: Response | dict) -> dict[str, Any]:
as_dict: dict[str, Any]
if isinstance(response, Response):
as_dict = cast(dict, response.json())
else:
as_dict = response
return as_dict
assert_has_key = assert_has_keys
def assert_file_looks_like_xlsx(xlsx_file: str):
# Check the file header
with open(xlsx_file, "rb") as file:
header = file.read(4)
# The ZIP file signature is 0x50 0x4B 0x03 0x04
return header == b"\x50\x4b\x03\x04"