Skip to content

Commit

Permalink
killing pragma: no cover statements
Browse files Browse the repository at this point in the history
  • Loading branch information
eacharles committed Nov 18, 2024
1 parent f236c4e commit a8a1d28
Show file tree
Hide file tree
Showing 28 changed files with 209 additions and 203 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ exclude_lines = [
"if settings.DEBUG",
"raise AssertionError",
"raise NotImplementedError",
"except Exception as msg",
"except KeyError as msg",
"except IntegrityError as msg",
"if 0:",
"if __name__ == .__main__.:",
"if TYPE_CHECKING:",
Expand Down
8 changes: 4 additions & 4 deletions src/lsst/cmservice/client/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,21 +338,21 @@ def campaign_cl(

try:
prod_config = config_data["Production"]
except KeyError as msg: # pragma: no cover
except KeyError as msg:
raise CMYamlParseError(
f"Could not find 'Production' tag in {campaign_yaml}",
) from msg
try:
parent_name = prod_config["name"]
except KeyError as msg: # pragma: no cover
except KeyError as msg:
raise CMYamlParseError(
f"Could not find 'name' tag in {campaign_yaml}#Production",
) from msg

try:
camp_config = config_data["Campaign"]
camp_config["parent_name"] = parent_name
except KeyError as msg: # pragma: no cover
except KeyError as msg:
raise CMYamlParseError(
f"Could not find 'Campaign' tag in {campaign_yaml}",
) from msg
Expand Down Expand Up @@ -471,7 +471,7 @@ def error_types_cl(
for error_type_ in error_types:
try:
val = error_type_["PipetaskErrorType"]
except KeyError as msg: # pragma: no cover
except KeyError as msg:
raise CMYamlParseError(
f"Expecting PipetaskErrorType items not: {error_type_.keys()})",
) from msg
Expand Down
10 changes: 5 additions & 5 deletions src/lsst/cmservice/client/queues.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pydantic import TypeAdapter, ValidationError

from .. import db, models
from ..common.errors import test_type_and_raise
from . import wrappers

if TYPE_CHECKING:
Expand Down Expand Up @@ -100,8 +101,7 @@ def process(
True if processing can continue
"""
results = self._client.get(f"{router_string}/process/{row_id}").json()
if not isinstance(results, bool): # pragma: no cover
raise ValueError(f"Bad response: {results}")
test_type_and_raise(results, bool, "Queue.process response")
return results

def pause_until_next_check(
Expand All @@ -122,7 +122,7 @@ def pause_until_next_check(
wait_time = min(sleep_time, queue.interval)
delta_t = timedelta(seconds=wait_time)
next_check = queue.time_updated + delta_t
except Exception as msg: # pragma: no cover
except Exception as msg:
print(f"failed to compute next_check time, making best guess: {msg}")
sleep_time = 300
wait_time = 300
Expand Down Expand Up @@ -151,10 +151,10 @@ def daemon(
self.pause_until_next_check(row_id)
try:
can_continue = self.process(row_id)
except Exception as msg: # pragma: no cover
except Exception as msg:
print(f"Caught exception in process: {msg}, continuing")
try:
self.update(row_id, time_updated=datetime.now())
except Exception as msg2: # pragma: no cover
except Exception as msg2:
print(f"Failed to modify time_updated: {msg2}, continuing")
can_continue = True
2 changes: 1 addition & 1 deletion src/lsst/cmservice/common/bash.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def run_bash_job(
assert process.stderr
msg = process.stderr.read().decode()
raise CMBashSubmitError(f"Bad bash submit: {msg}")
except Exception as msg: # pragma: no cover
except Exception as msg:
raise CMBashSubmitError(f"Bad bash submit: {msg}") from msg
with open(stamp_url, "w", encoding="utf-8") as fstamp:
fields = dict(status="accepted")
Expand Down
12 changes: 6 additions & 6 deletions src/lsst/cmservice/common/butler.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def remove_run_collections(
"""
try:
butler = Butler.from_config(butler_repo, collections=[collection_name], without_datastore=True)
except Exception as msg:
except Exception as e:
if fake_reset:
return
raise errors.CMNoButlerError(msg) from msg # pragma: no cover
raise errors.CMNoButlerError(e) from e # pragma: no cover
try: # pragma: no cover
butler.registry.removeCollection(collection_name)
except Exception as msg: # pragma: no cover
except Exception as msg:
raise errors.CMButlerCallError(msg) from msg


Expand All @@ -56,13 +56,13 @@ def remove_non_run_collections(
"""
try:
butler = Butler.from_config(butler_repo, collections=[collection_name], without_datastore=True)
except Exception as msg:
except Exception as e:
if fake_reset:
return
raise errors.CMNoButlerError(msg) from msg # pragma: no cover
raise errors.CMNoButlerError(e) from e # pragma: no cover
try: # pragma: no cover
butler.registry.removeCollection(collection_name)
except Exception as msg: # pragma: no cover
except Exception as msg:
raise errors.CMButlerCallError(msg) from msg


Expand Down
22 changes: 16 additions & 6 deletions src/lsst/cmservice/common/errors.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""cm-service specific error types"""

from typing import Any, TypeVar

from sqlalchemy.exc import IntegrityError

T = TypeVar("T")


class CMBashCheckError(KeyError):
"""Raised when bash checking fails"""


class CMBashSubmitError(KeyError):
class CMBashSubmitError(RuntimeError):
"""Raised when bash submisison fails"""


Expand All @@ -19,7 +23,7 @@ class CMBadStateTransitionError(ValueError):
"""Raised when requesting a bad transition states for a Node"""


class CMBadExecutionMethodError(ValueError):
class CMBadExecutionMethodError(RuntimeError):
"""Raised when requesting a bed execution method for a Node"""


Expand Down Expand Up @@ -67,27 +71,27 @@ class CMResolveCollectionsError(KeyError):
"""Raised when the collection name resolution fails"""


class CMSlurmSubmitError(KeyError):
class CMSlurmSubmitError(RuntimeError):
"""Raised when slurm submisison fails"""


class CMSlurmCheckError(KeyError):
"""Raised when slurm checking fails"""


class CMHTCondorSubmitError(KeyError):
class CMHTCondorSubmitError(RuntimeError):
"""Raised when htcondor submission fails"""


class CMHTCondorCheckError(KeyError):
"""Raised when htcondor checking fails"""


class CMNoButlerError(KeyError):
class CMNoButlerError(RuntimeError):
"""Raised when no butler is present"""


class CMButlerCallError(KeyError):
class CMButlerCallError(RuntimeError):
"""Raised when a call to butler fails"""


Expand All @@ -109,3 +113,9 @@ class CMTooManyActiveScriptsError(KeyError):

class CMYamlParseError(KeyError):
"""Raised when parsing a yaml file fails"""


def test_type_and_raise(object: Any, expected_type: type[T], var_name: str) -> T:
if not isinstance(object, expected_type):
raise CMBadParameterTypeError(f"{var_name} expected type {expected_type} got {type(object)}")
return object
12 changes: 6 additions & 6 deletions src/lsst/cmservice/common/htcondor.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def submit_htcondor_job(
assert sbatch.stderr
msg = sbatch.stderr.read().decode()
raise CMHTCondorSubmitError(f"Bad htcondor submit: {msg}")
except Exception as msg:
raise CMHTCondorSubmitError(f"Bad htcondor submit: {msg}") from msg
except Exception as e:
raise CMHTCondorSubmitError(f"Bad htcondor submit: {e}") from e


def check_htcondor_job(
Expand Down Expand Up @@ -143,10 +143,10 @@ def check_htcondor_job(
assert len(tokens) == 2
htcondor_status = int(tokens[0])
exit_code = tokens[1]
except Exception as msg:
raise CMHTCondorCheckError(f"Badly formatted htcondor check: {msg}") from msg
except Exception as msg:
raise CMHTCondorCheckError(f"Bad htcondor check: {msg}") from msg
except Exception as e:
raise CMHTCondorCheckError(f"Badly formatted htcondor check: {e}") from e
except Exception as e:
raise CMHTCondorCheckError(f"Bad htcondor check: {e}") from e

status = htcondor_status_map[htcondor_status] # pragma: no cover
if status == StatusEnum.reviewable: # pragma: no cover
Expand Down
12 changes: 6 additions & 6 deletions src/lsst/cmservice/common/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def submit_slurm_job(
assert sbatch.stdout
line = sbatch.stdout.read().decode().strip()
return line.split("|")[0]
except Exception as msg:
raise CMSlurmSubmitError(f"Bad slurm submit: {msg}") from msg
except Exception as e:
raise CMSlurmSubmitError(f"Bad slurm submit: {e}") from e


def check_slurm_job(
Expand Down Expand Up @@ -130,8 +130,8 @@ def check_slurm_job(
if len(tokens) < 2:
return slurm_status_map["PENDING"]
slurm_status = tokens[1]
except Exception as msg:
raise CMSlurmCheckError(f"Badly formatted slurm check: {msg}") from msg
except Exception as msg:
raise CMSlurmCheckError(f"Bad slurm check: {msg}") from msg
except Exception as e:
raise CMSlurmCheckError(f"Badly formatted slurm check: {e}") from e
except Exception as e:
raise CMSlurmCheckError(f"Bad slurm check: {e}") from e
return slurm_status_map[slurm_status] # pragma: no cover
6 changes: 3 additions & 3 deletions src/lsst/cmservice/db/campaign.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ async def get_create_kwargs(
try:
spec_name = kwargs["spec_name"]
spec_block_name = kwargs.get("spec_block_name", "campaign")
except KeyError as msg:
except KeyError as e:
raise CMMissingRowCreateInputError(
"Either spec_block_assoc_name or spec_name required",
) from msg
"Spec_name required",
) from e
else:
tokens = spec_block_assoc_name.split("#")
if len(tokens) != 2:
Expand Down
10 changes: 5 additions & 5 deletions src/lsst/cmservice/db/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ async def get_create_kwargs(
parent_name = kwargs["parent_name"]
name = kwargs["name"]
spec_block_name = kwargs["spec_block_name"]
except KeyError as msg:
raise CMMissingRowCreateInputError(f"Missing input to create Group: {msg}") from msg
except KeyError as e:
raise CMMissingRowCreateInputError(f"Missing input to create Group: {e}") from e
step = await Step.get_row_by_fullname(session, parent_name)
spec_aliases = await step.get_spec_aliases(session)
spec_block_name = spec_aliases.get(spec_block_name, spec_block_name)
Expand Down Expand Up @@ -199,11 +199,11 @@ async def rescue_job(
try:
new_job = await latest_resuable_job.copy_job(session, self)
await session.commit()
except IntegrityError as e: # pragma: no cover
except IntegrityError as msg:
if TYPE_CHECKING:
assert e.orig # for mypy
assert msg.orig # for mypy
await session.rollback()
raise CMIntegrityError(params=e.params, orig=e.orig, statement=e.statement) from e
raise CMIntegrityError(params=msg.params, orig=msg.orig, statement=msg.statement) from msg
return new_job

async def mark_job_rescued(
Expand Down
20 changes: 11 additions & 9 deletions src/lsst/cmservice/db/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from sqlalchemy.schema import ForeignKey

from ..common.enums import LevelEnum, StatusEnum
from ..common.errors import CMBadParameterTypeError, CMIntegrityError, CMMissingRowCreateInputError
from ..common.errors import (
CMIntegrityError,
CMMissingRowCreateInputError,
test_type_and_raise,
)
from ..models.merged_product_set import MergedProductSet, MergedProductSetDict
from ..models.merged_task_set import MergedTaskSet, MergedTaskSetDict
from ..models.merged_wms_task_report import MergedWmsTaskReport, MergedWmsTaskReportDict
Expand Down Expand Up @@ -202,8 +206,8 @@ async def get_create_kwargs(
parent_name = kwargs["parent_name"]
name = kwargs["name"]
spec_block_name = kwargs["spec_block_name"]
except KeyError as msg:
raise CMMissingRowCreateInputError(f"Missing input to create Job: {msg}") from msg
except KeyError as e:
raise CMMissingRowCreateInputError(f"Missing input to create Job: {e}") from e
attempt = kwargs.get("attempt", 0)
parent = await Group.get_row_by_fullname(session, parent_name)
spec_aliases = await parent.get_spec_aliases(session)
Expand Down Expand Up @@ -258,9 +262,7 @@ async def copy_job(

fullname = f"{parent.fullname}/{self.name}_{attempt:03}"
if self.data:
if not isinstance(self.data, dict): # pragma: no cover
raise CMBadParameterTypeError(f"job.data should be dict | None, not {type(self.data)}")
data = self.data.copy()
data = test_type_and_raise(self.data, dict, "job.data").copy()
else: # pragma: no cover
data = {}
data["rescue"] = True
Expand All @@ -284,11 +286,11 @@ async def copy_job(
async with session.begin_nested():
try:
session.add(new_job)
except IntegrityError as e: # pragma: no cover
except IntegrityError as msg:
await session.rollback()
if TYPE_CHECKING:
assert e.orig # for mypy
raise CMIntegrityError(params=e.params, orig=e.orig, statement=e.statement) from e
assert msg.orig # for mypy
raise CMIntegrityError(params=msg.params, orig=msg.orig, statement=msg.statement) from msg

await session.refresh(new_job)
return new_job
Expand Down
Loading

0 comments on commit a8a1d28

Please sign in to comment.