Skip to content

Commit 63a6a37

Browse files
MetRonniedwsutherland
authored andcommitted
Type annotations & generate protobuf type stubs
1 parent e118655 commit 63a6a37

File tree

8 files changed

+682
-51
lines changed

8 files changed

+682
-51
lines changed

cylc/flow/data_messages.proto

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ syntax = "proto3";
2525
* message modules.
2626
*
2727
* Command:
28-
* $ protoc -I=./ --python_out=./ data_messages.proto && sed -i '1i# type: ignore' data_messages_pb2.py
28+
* $ protoc -I=./ --python_out=./ --pyi_out=./ data_messages.proto
2929
*
3030
* Pre-compiled protoc binary may be download from:
3131
* https://github.com/protocolbuffers/protobuf/releases

cylc/flow/data_messages_pb2.py

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cylc/flow/data_messages_pb2.pyi

Lines changed: 630 additions & 0 deletions
Large diffs are not rendered by default.

cylc/flow/data_store_mgr.py

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
from cylc.flow import __version__ as CYLC_VERSION, LOG
7575
from cylc.flow.cycling.loader import get_point
76-
from cylc.flow.data_messages_pb2 import ( # type: ignore
76+
from cylc.flow.data_messages_pb2 import (
7777
PbEdge, PbEntireWorkflow, PbFamily, PbFamilyProxy, PbJob, PbTask,
7878
PbTaskProxy, PbWorkflow, PbRuntime, AllDeltas, EDeltas, FDeltas,
7979
FPDeltas, JDeltas, TDeltas, TPDeltas, WDeltas)
@@ -112,7 +112,7 @@
112112

113113
if TYPE_CHECKING:
114114
from cylc.flow.cycling import PointBase
115-
115+
from cylc.flow.flow_mgr import FlowNums
116116

117117
EDGES = 'edges'
118118
FAMILIES = 'families'
@@ -713,10 +713,10 @@ def generate_definition_elements(self):
713713
def increment_graph_window(
714714
self,
715715
source_tokens: Tokens,
716-
point,
717-
flow_nums,
718-
is_manual_submit=False,
719-
itask=None
716+
point: 'PointBase',
717+
flow_nums: 'FlowNums',
718+
is_manual_submit: bool = False,
719+
itask: Optional['TaskProxy'] = None
720720
) -> None:
721721
"""Generate graph window about active task proxy to n-edge-distance.
722722
@@ -731,16 +731,12 @@ def increment_graph_window(
731731
assessed for pruning eligibility.
732732
733733
Args:
734-
source_tokens (cylc.flow.id.Tokens)
735-
point (PointBase)
736-
flow_nums (set)
737-
is_manual_submit (bool)
738-
itask (cylc.flow.task_proxy.TaskProxy):
734+
source_tokens
735+
point
736+
flow_nums
737+
is_manual_submit
738+
itask:
739739
Active/Other task proxy, passed in with pool invocation.
740-
741-
Returns:
742-
None
743-
744740
"""
745741

746742
# common refrences
@@ -1149,28 +1145,23 @@ def add_pool_node(self, name, point):
11491145
def generate_ghost_task(
11501146
self,
11511147
tokens: Tokens,
1152-
point,
1153-
flow_nums,
1154-
is_parent=False,
1155-
itask=None,
1156-
n_depth=0,
1157-
):
1148+
point: 'PointBase',
1149+
flow_nums: 'FlowNums',
1150+
is_parent: bool = False,
1151+
itask: Optional['TaskProxy'] = None,
1152+
n_depth: int = 0,
1153+
) -> None:
11581154
"""Create task-point element populated with static data.
11591155
11601156
Args:
1161-
source_tokens (cylc.flow.id.Tokens)
1162-
point (PointBase)
1163-
flow_nums (set)
1164-
is_parent (bool):
1157+
source_tokens
1158+
point
1159+
flow_nums
1160+
is_parent:
11651161
Used to determine whether to load DB state.
1166-
itask (cylc.flow.task_proxy.TaskProxy):
1162+
itask:
11671163
Update task-node from corresponding task proxy object.
1168-
n_depth (int): n-window graph edge distance.
1169-
1170-
Returns:
1171-
1172-
None
1173-
1164+
n_depth: n-window graph edge distance.
11741165
"""
11751166
tp_id = tokens.id
11761167
if (
@@ -1486,7 +1477,11 @@ def apply_task_proxy_db_history(self):
14861477

14871478
self.db_load_task_proxies.clear()
14881479

1489-
def _process_internal_task_proxy(self, itask, tproxy):
1480+
def _process_internal_task_proxy(
1481+
self,
1482+
itask: 'TaskProxy',
1483+
tproxy: PbTaskProxy,
1484+
):
14901485
"""Extract information from internal task proxy object."""
14911486

14921487
update_time = time()
@@ -1569,6 +1564,7 @@ def insert_job(self, name, cycle_point, status, job_conf):
15691564
cycle=str(cycle_point),
15701565
task=name,
15711566
)
1567+
tproxy: Optional[PbTaskProxy]
15721568
tp_id, tproxy = self.store_node_fetcher(tp_tokens)
15731569
if not tproxy:
15741570
return
@@ -1642,6 +1638,7 @@ def insert_db_job(self, row_idx, row):
16421638
cycle=point_string,
16431639
task=name,
16441640
)
1641+
tproxy: Optional[PbTaskProxy]
16451642
tp_id, tproxy = self.store_node_fetcher(tp_tokens)
16461643
if not tproxy:
16471644
return
@@ -1761,9 +1758,8 @@ def update_workflow_states(self):
17611758
self.apply_delta_checksum()
17621759
self.publish_deltas = self.get_publish_deltas()
17631760

1764-
def window_resize_rewalk(self):
1761+
def window_resize_rewalk(self) -> None:
17651762
"""Re-create data-store n-window on resize."""
1766-
tokens: Tokens
17671763
# Gather pre-resize window nodes
17681764
if not self.all_n_window_nodes:
17691765
self.all_n_window_nodes = set().union(*(
@@ -1777,7 +1773,8 @@ def window_resize_rewalk(self):
17771773
self.n_window_node_walks.clear()
17781774
for tp_id in self.all_task_pool:
17791775
tokens = Tokens(tp_id)
1780-
tp_id, tproxy = self.store_node_fetcher(tokens)
1776+
tproxy: PbTaskProxy
1777+
_, tproxy = self.store_node_fetcher(tokens)
17811778
self.increment_graph_window(
17821779
tokens,
17831780
get_point(tokens['cycle']),
@@ -2279,6 +2276,7 @@ def delta_task_state(self, itask):
22792276
objects from the workflow task pool.
22802277
22812278
"""
2279+
tproxy: Optional[PbTaskProxy]
22822280
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
22832281
if not tproxy:
22842282
return
@@ -2331,7 +2329,7 @@ def delta_task_held(
23312329
task=name,
23322330
cycle=str(cycle),
23332331
)
2334-
2332+
tproxy: Optional[PbTaskProxy]
23352333
tp_id, tproxy = self.store_node_fetcher(tokens)
23362334
if not tproxy:
23372335
return
@@ -2351,6 +2349,7 @@ def delta_task_queued(self, itask: TaskProxy) -> None:
23512349
objects from the workflow task pool.
23522350
23532351
"""
2352+
tproxy: Optional[PbTaskProxy]
23542353
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
23552354
if not tproxy:
23562355
return
@@ -2370,6 +2369,7 @@ def delta_task_runahead(self, itask: TaskProxy) -> None:
23702369
objects from the workflow task pool.
23712370
23722371
"""
2372+
tproxy: Optional[PbTaskProxy]
23732373
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
23742374
if not tproxy:
23752375
return
@@ -2393,6 +2393,7 @@ def delta_task_output(
23932393
objects from the workflow task pool.
23942394
23952395
"""
2396+
tproxy: Optional[PbTaskProxy]
23962397
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
23972398
if not tproxy:
23982399
return
@@ -2419,6 +2420,7 @@ def delta_task_outputs(self, itask: TaskProxy) -> None:
24192420
objects from the workflow task pool.
24202421
24212422
"""
2423+
tproxy: Optional[PbTaskProxy]
24222424
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
24232425
if not tproxy:
24242426
return
@@ -2444,6 +2446,7 @@ def delta_task_prerequisite(self, itask: TaskProxy) -> None:
24442446
objects from the workflow task pool.
24452447
24462448
"""
2449+
tproxy: Optional[PbTaskProxy]
24472450
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
24482451
if not tproxy:
24492452
return
@@ -2472,13 +2475,14 @@ def delta_task_ext_trigger(
24722475
"""Create delta for change in task proxy external_trigger.
24732476
24742477
Args:
2475-
itask (cylc.flow.task_proxy.TaskProxy):
2478+
itask:
24762479
Update task-node from corresponding task proxy
24772480
objects from the workflow task pool.
2478-
trig (str): Trigger ID.
2479-
message (str): Trigger message.
2481+
trig: Trigger ID.
2482+
message: Trigger message.
24802483
24812484
"""
2485+
tproxy: Optional[PbTaskProxy]
24822486
tp_id, tproxy = self.store_node_fetcher(itask.tokens)
24832487
if not tproxy:
24842488
return

cylc/flow/network/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636
from cylc.flow.network.resolvers import Resolvers
3737
from cylc.flow.network.schema import schema
3838
from cylc.flow.data_store_mgr import DELTAS_MAP
39-
from cylc.flow.data_messages_pb2 import PbEntireWorkflow # type: ignore
39+
from cylc.flow.data_messages_pb2 import PbEntireWorkflow
4040

4141
if TYPE_CHECKING:
4242
from cylc.flow.scheduler import Scheduler
4343
from graphql.execution import ExecutionResult
4444

4545

4646
# maps server methods to the protobuf message (for client/UIS import)
47-
PB_METHOD_MAP = {
47+
PB_METHOD_MAP: Dict[str, Any] = {
4848
'pb_entire_workflow': PbEntireWorkflow,
4949
'pb_data_elements': DELTAS_MAP
5050
}

cylc/flow/prerequisite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from cylc.flow.cycling.loader import get_point
2424
from cylc.flow.exceptions import TriggerExpressionError
25-
from cylc.flow.data_messages_pb2 import ( # type: ignore
25+
from cylc.flow.data_messages_pb2 import (
2626
PbPrerequisite,
2727
PbCondition,
2828
)

cylc/flow/scripts/client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from google.protobuf.json_format import MessageToDict
2828
import json
2929
import sys
30-
from typing import TYPE_CHECKING
30+
from typing import TYPE_CHECKING, cast
3131

3232
from cylc.flow.id_cli import parse_id
3333
from cylc.flow.option_parsers import (
@@ -40,6 +40,7 @@
4040

4141
if TYPE_CHECKING:
4242
from optparse import Values
43+
from google.protobuf.message import Message
4344

4445

4546
INTERNAL = True
@@ -76,11 +77,12 @@ def main(_, options: 'Values', workflow_id: str, func: str) -> None:
7677
sys.stdin.close()
7778
res = pclient(func, kwargs)
7879
if func in PB_METHOD_MAP:
80+
pb_msg: Message
7981
if 'element_type' in kwargs:
8082
pb_msg = PB_METHOD_MAP[func][kwargs['element_type']]()
8183
else:
8284
pb_msg = PB_METHOD_MAP[func]()
83-
pb_msg.ParseFromString(res)
85+
pb_msg.ParseFromString(cast('bytes', res))
8486
res_msg: object = MessageToDict(pb_msg)
8587
else:
8688
res_msg = res

mypy.ini

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,3 @@ exclude= cylc/flow/etc/tutorial/.*
1919
# Suppress the following messages:
2020
# By default the bodies of untyped functions are not checked, consider using --check-untyped-defs
2121
disable_error_code = annotation-unchecked
22-
23-
# For some reason, couldn't exclude this with the exclude directive above
24-
[mypy-cylc.flow.data_messages_pb2]
25-
ignore_errors = True

0 commit comments

Comments
 (0)