Skip to content

Commit

Permalink
Add DeprecationWarnings for item getting, setting and deleting (#162)
Browse files Browse the repository at this point in the history
Closes #160
  • Loading branch information
jmsmkn authored and chrisvanrun committed Nov 5, 2024
1 parent 01145c4 commit dda0859
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 198 deletions.
36 changes: 17 additions & 19 deletions gcapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def run_external_job(self, *, algorithm: str, inputs: dict[str, Any]):
The created job
"""
alg = yield from self.__org_api_meta.algorithms.detail(slug=algorithm)
input_interfaces = {ci["slug"]: ci for ci in alg["inputs"]}
input_interfaces = {ci.slug: ci for ci in alg.inputs}

for ci in input_interfaces:
if (
Expand All @@ -663,33 +663,33 @@ def run_external_job(self, *, algorithm: str, inputs: dict[str, Any]):
):
raise ValueError(f"{ci} is not provided")

job = {"algorithm": alg["api_url"], "inputs": []}
job = {"algorithm": alg.api_url, "inputs": []}
for input_title, value in inputs.items():
ci = input_interfaces.get(input_title, None)
ci = input_interfaces.get(input_title, None) # type: ignore
if not ci:
raise ValueError(
f"{input_title} is not an input interface for this algorithm"
)

i = {"interface": ci["slug"]}
if ci["super_kind"].lower() == "image":
i = {"interface": ci.slug} # type: ignore
if ci.super_kind.lower() == "image": # type: ignore
if isinstance(value, list):
raw_image_upload_session = (
yield from self._upload_image_files(files=value)
)
i["upload_session"] = raw_image_upload_session["api_url"]
elif isinstance(value, str):
i["image"] = value
elif ci["super_kind"].lower() == "file":
elif ci["super_kind"].lower() == "file": # type: ignore
if len(value) != 1:
raise ValueError(
f"Only a single file can be provided for {ci['title']}."
f"Only a single file can be provided for {ci['title']}." # type: ignore
)
upload = yield from self._upload_file(value)
i["user_upload"] = upload["api_url"]
else:
i["value"] = value
job["inputs"].append(i)
job["inputs"].append(i) # type: ignore

return (yield from self.__org_api_meta.algorithm_jobs.create(**job))

Expand Down Expand Up @@ -744,26 +744,24 @@ def update_archive_item(
f"Please provide one from this list: "
f"https://grand-challenge.org/algorithms/interfaces/"
) from e
i = {"interface": ci["slug"]}
i = {"interface": ci.slug}

if not value:
raise ValueError(
f"You need to provide a value for {ci['slug']}"
)
raise ValueError(f"You need to provide a value for {ci.slug}")

if ci["super_kind"].lower() == "image":
if ci.super_kind.lower() == "image":
if isinstance(value, list):
raw_image_upload_session = (
yield from self._upload_image_files(files=value)
)
i["upload_session"] = raw_image_upload_session["api_url"]
elif isinstance(value, str):
i["image"] = value
elif ci["super_kind"].lower() == "file":
elif ci.super_kind.lower() == "file":
if len(value) != 1:
raise ValueError(
f"You can only upload one single file "
f"to a {ci['slug']} interface."
f"to a {ci.slug} interface."
)
upload = yield from self._upload_file(value)
i["user_upload"] = upload["api_url"]
Expand All @@ -773,7 +771,7 @@ def update_archive_item(

return (
yield from self.__org_api_meta.archive_items.partial_update(
pk=item["pk"], **civs
pk=item.pk, **civs
)
)

Expand All @@ -799,7 +797,7 @@ def _validate_display_set_values(self, values, interfaces):
else:
interface = yield from self._fetch_interface(slug)
interfaces[slug] = interface
super_kind = interface["super_kind"].casefold()
super_kind = interface.super_kind.casefold()
if super_kind != "value":
if not isinstance(value, list):
raise ValueError(
Expand Down Expand Up @@ -854,7 +852,7 @@ def add_cases_to_reader_study(
The pks of the newly created display sets.
"""
res = []
interfaces: dict[str, dict] = {}
interfaces: dict[str, gcapi.models.ComponentInterface] = {}
for display_set in display_sets:
new_interfaces = yield from self._validate_display_set_values(
display_set.items(), interfaces
Expand All @@ -869,7 +867,7 @@ def add_cases_to_reader_study(
for slug, value in display_set.items():
interface = interfaces[slug]
data = {"interface": slug}
super_kind = interface["super_kind"].casefold()
super_kind = interface.super_kind.casefold()
if super_kind == "image":
yield from self._upload_image_files(
display_set=ds["pk"], interface=slug, files=value
Expand Down
18 changes: 18 additions & 0 deletions gcapi/model_base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import warnings


class BaseModel:
def __getitem__(self, key):
self._warn_deprecated_access(key, "getting")
return getattr(self, key)

def __setitem__(self, key, value):
self._warn_deprecated_access(key, "setting")
return setattr(self, key, value)

def __delitem__(self, key):
self._warn_deprecated_access(key, "deleting")
return delattr(self, key)

@staticmethod
def _warn_deprecated_access(key, action):
warnings.warn(
message=(
f'Using ["{key}"] for {action} attributes is deprecated '
"and will be removed in the next release. "
f'Suggestion: Replace ["{key}"] with .{key}'
),
category=DeprecationWarning,
stacklevel=3,
)
6 changes: 3 additions & 3 deletions gcapi/retries.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class SelectiveBackoffStrategy(BaseRetryStrategy):
Each response code has its own backoff counter.
"""

def __init__(self, backoff_factor, maximum_number_of_retries):
self.backoff_factor: float = backoff_factor
self.maximum_number_of_retries: int = maximum_number_of_retries
def __init__(self, backoff_factor: float, maximum_number_of_retries: int):
self.backoff_factor = backoff_factor
self.maximum_number_of_retries = maximum_number_of_retries
self.earlier_number_of_retries: dict[int, int] = dict()

def __call__(self) -> BaseRetryStrategy:
Expand Down
Loading

0 comments on commit dda0859

Please sign in to comment.