Skip to content

Commit af5ced9

Browse files
committed
Fix various typing issues
Can't use classvars for a lot of the stuff, those are essentially globals. Fixes the tests.
1 parent 9527c00 commit af5ced9

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

planemo/galaxy/invocations/polling.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def summarize(invocation_id: str):
5656
last_subworkflow_invocation = None
5757
last_subworkflow_invocation_jobs = None
5858
last_exception = None
59+
error_message: Optional[str] = None
5960

6061
done_polling = False
6162
while not done_polling:
@@ -120,6 +121,7 @@ def summarize(invocation_id: str):
120121

121122
ctx.vlog(f"The final state of all jobs and subworkflow invocations for invocation [{invocation_id}] is 'ok'")
122123
job_state = summary_job_state(last_invocation_jobs)
124+
assert last_invocation
123125
return last_invocation["state"], job_state, error_message
124126

125127

@@ -152,7 +154,7 @@ def workflow_in_error_message(
152154
# we're still mocking out the old history state by just picking out a random
153155
# job state of interest. Seems like we should drop this.
154156
def summary_job_state(job_states_summary: Optional[InvocationJobsSummary]):
155-
states = (job_states_summary or {"states": {}}).get("states").copy()
157+
states = (job_states_summary or {"states": {}}).get("states", {}).copy()
156158
states.pop("ok", None)
157159
states.pop("skipped", None)
158160
if states:
@@ -165,6 +167,7 @@ def subworkflow_invocation_ids(invocation_api: InvocationApi, invocation_id: str
165167
invocation = invocation_api.get_invocation(invocation_id)
166168
subworkflow_invocation_ids = []
167169
for step in invocation["steps"]:
168-
if step.get("subworkflow_invocation_id") is not None:
169-
subworkflow_invocation_ids.append(step["subworkflow_invocation_id"])
170+
subworkflow_invocation_id = step.get("subworkflow_invocation_id")
171+
if subworkflow_invocation_id:
172+
subworkflow_invocation_ids.append(subworkflow_invocation_id)
170173
return subworkflow_invocation_ids

planemo/galaxy/invocations/progress.py

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,35 @@ class InvocationJobsSummary(TypedDict, total=False):
4343

4444

4545
class WorkflowProgress(Progress):
46-
invocation_state: str = "new"
47-
step_count: Optional[int] = None
48-
job_count: int = 0
49-
steps_color: str
50-
jobs_color: str
51-
subworkflows_color: str
52-
step_states: Dict = {}
53-
num_ok: int = 0
54-
num_new: int = 0
55-
num_queued: int = 0
56-
num_running: int = 0
57-
num_errors: int = 0
58-
num_paused: int = 0
59-
60-
num_subworkflows: int = 0
61-
num_subworkflows_complete: int = 0
6246

6347
_jobs_task: TaskID
6448
_steps_task: TaskID
6549
_subworkflows_task: Optional[TaskID] = None
6650

6751
def __init__(self, display: DisplayConfiguration):
52+
self.invocation_state: str = "new"
53+
self.step_count: Optional[int] = None
54+
self.job_count: Optional[int] = 0
55+
self.jobs_completed: Optional[int] = None
56+
self.step_states: Dict[str, int] = {}
57+
self.num_ok: int = 0
58+
self.num_new: int = 0
59+
self.num_queued: int = 0
60+
self.num_running: int = 0
61+
self.num_errors: int = 0
62+
self.num_paused: int = 0
63+
64+
self.num_subworkflows: int = 0
65+
self.num_subworkflows_complete: int = 0
6866
self.display = display
6967
bar_column = BarColumn(
7068
style=self.display.style_bar_back,
7169
finished_style=self.display.style_bar_finished,
7270
complete_style=self.display.style_bar_complete,
7371
)
74-
self.jobs_color = self.display.style_initializing
75-
self.steps_color = self.display.style_initializing
76-
self.subworkflows_color = self.display.style_initializing
72+
self.jobs_color: str = self.display.style_initializing
73+
self.steps_color: str = self.display.style_initializing
74+
self.subworkflows_color: str = self.display.style_initializing
7775
super().__init__(
7876
TextColumn("[progress.description]{task.description}"),
7977
TextColumn(display.divider),
@@ -153,7 +151,7 @@ def handle_invocation(self, invocation: Invocation, job_state_summary: Invocatio
153151
self.jobs_completed = self.num_ok + self.num_errors
154152
self.num_paused = count_states(job_state_summary, ["paused"])
155153
self.jobs_terminal_count = self.jobs_completed + self.num_paused
156-
jobs_total = self.job_count
154+
jobs_total: Optional[int] = self.job_count
157155
if self.num_errors > 0:
158156
self.jobs_color = self.display.style_error
159157
elif self.job_count > 0:
@@ -251,18 +249,15 @@ def running_count(job_summary: InvocationJobsSummary) -> int:
251249

252250

253251
class WorkflowProgressDisplay(Live):
254-
workflow_progress: WorkflowProgress
255-
subworkflow_progress: Optional[WorkflowProgress] = None
256-
subworkflow_invocation_ids_seen: Set[str] = set()
257-
subworkflow_invocation_ids_completed: Set[str] = set()
258-
invocation_id: str
259-
subworkflow_invocation_id: Optional[str] = None
260252

261253
def __init__(
262254
self,
263255
invocation_id: str,
264256
display_configuration: Optional[DisplayConfiguration] = None,
265257
):
258+
self.subworkflow_invocation_ids_seen: Set[str] = set()
259+
self.subworkflow_invocation_ids_completed: Set[str] = set()
260+
self.subworkflow_invocation_id: Optional[str] = None
266261
self.invocation_id = invocation_id
267262
display = display_configuration or DisplayConfiguration()
268263
self.display = display
@@ -271,10 +266,12 @@ def __init__(
271266
super().__init__(self._panel())
272267

273268
def _register_subworkflow_invocation_ids_from(self, invocation: Invocation):
274-
subworkflow_invocation_ids = []
275-
for step in invocation["steps"]:
276-
if step.get("subworkflow_invocation_id") is not None:
277-
subworkflow_invocation_ids.append(step["subworkflow_invocation_id"])
269+
subworkflow_invocation_ids: List[str] = []
270+
steps = invocation.get("steps") or []
271+
for step in steps:
272+
subworkflow_invocation_id = step.get("subworkflow_invocation_id")
273+
if subworkflow_invocation_id:
274+
subworkflow_invocation_ids.append(subworkflow_invocation_id)
278275
self._register_subworkflow_invocation_ids(subworkflow_invocation_ids)
279276

280277
def _register_subworkflow_invocation_ids(self, ids: List[str]):

planemo/galaxy/invocations/simulations.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from collections import deque
44
from typing import (
5+
Dict,
56
List,
67
Optional,
78
)
@@ -11,6 +12,7 @@
1112

1213
from .api import Invocation as InvocationResponse
1314
from .api import InvocationJobsSummary
15+
from .api import InvocationStep as InvocationStepResponse
1416

1517

1618
class Ticks:
@@ -46,7 +48,7 @@ class HasState(Ticks):
4648
def __init__(self, after: int, states: List[StateWithDuration]):
4749
self.after = after or 0
4850
self.states = deque(states)
49-
self.final_state = None
51+
self.final_state: Optional[str] = None
5052

5153
def tick_when_active(self) -> None:
5254
if self.final_state is not None:
@@ -128,13 +130,13 @@ def get_subworkflow_invocation(self, subworkflow_invocation_id: str) -> "Invocat
128130
return step.invocation
129131
raise Exception(f"Unknown subworkflow invocation id ({subworkflow_invocation_id})")
130132

131-
def get_subworkflow_invocation_by_step_index(self, index: int) -> "Invocation":
133+
def get_subworkflow_invocation_by_step_index(self, index: int) -> Optional["Invocation"]:
132134
return self.steps[index].invocation
133135

134136
def get_api_invocation(self) -> InvocationResponse:
135-
steps = []
137+
steps: List[InvocationStepResponse] = []
136138
for step in self.active_steps:
137-
api_step = {
139+
api_step: InvocationStepResponse = {
138140
"state": step.state,
139141
}
140142
if step.invocation:
@@ -148,16 +150,16 @@ def get_api_invocation(self) -> InvocationResponse:
148150
}
149151

150152
def get_api_jobs_summary(self) -> InvocationJobsSummary:
151-
jobs = []
153+
job_states = []
152154
for step in self.active_steps:
153155
for job in step.active_jobs:
154156
api_job = {
155157
"state": job.state,
156158
}
157-
jobs.append(api_job)
158-
by_state = {}
159-
for job in jobs:
160-
state = job["state"]
159+
job_states.append(api_job)
160+
by_state: Dict[str, int] = {}
161+
for job_state in job_states:
162+
state = job_state["state"]
161163
if state not in by_state:
162164
by_state[state] = 0
163165
by_state[state] += 1
@@ -188,7 +190,7 @@ def parse_workflow_simulation_invocation_step(workflow_simulation_invocation_ste
188190
jobs = None
189191
if "jobs" in workflow_simulation_invocation_step:
190192
jobs = []
191-
for job in workflow_simulation_invocation_step.get("jobs"):
193+
for job in workflow_simulation_invocation_step.get("jobs") or []:
192194
jobs.append(parse_workflow_simulation_job(job))
193195
return InvocationStep(jobs, invocation, after, states)
194196

@@ -197,7 +199,7 @@ def parse_workflow_simulation_invocation(workflow_simulation_invocation: dict) -
197199
states = parse_states_from(workflow_simulation_invocation)
198200
after = parse_after_from(workflow_simulation_invocation)
199201
steps = []
200-
for step in workflow_simulation_invocation.get("steps"):
202+
for step in workflow_simulation_invocation.get("steps") or []:
201203
steps.append(parse_workflow_simulation_invocation_step(step))
202204

203205
return Invocation(steps, after, states)

tests/test_workflow_progress.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
WorkflowProgress,
55
WorkflowProgressDisplay,
66
)
7+
from planemo.galaxy.invocations.progress_display import DisplayConfiguration
78

89
STEP_NEW = {"state": "new"}
910
STEP_SCHEDULED = {"state": "scheduled"}
@@ -80,7 +81,7 @@ def test_workflow_progress_typical():
8081

8182

8283
def test_workflow_progress_scheduling_state_handling():
83-
with WorkflowProgress() as workflow_progress:
84+
with WorkflowProgress(DisplayConfiguration()) as workflow_progress:
8485
workflow_progress.handle_invocation({"state": "new", "steps": [STEP_NEW]}, {"states": {"new": 1}})
8586
assert not workflow_progress.invocation_scheduling_terminal
8687

@@ -124,7 +125,7 @@ def test_workflow_progress_scheduling_state_handling():
124125
def test_workflow_progress_job_state_handling():
125126
scheduled_invocation = {"state": "scheduled", "steps": [STEP_SCHEDULED]}
126127

127-
with WorkflowProgress() as workflow_progress:
128+
with WorkflowProgress(DisplayConfiguration()) as workflow_progress:
128129
workflow_progress.handle_invocation(scheduled_invocation, {"states": {"new": 1}})
129130
assert not workflow_progress.jobs_terminal
130131

0 commit comments

Comments
 (0)