Skip to content

Commit

Permalink
Typing tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
hjoliver committed Jan 26, 2024
1 parent d921641 commit 8ad68b9
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 58 deletions.
8 changes: 6 additions & 2 deletions cylc/flow/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import errno
from textwrap import wrap
from typing import (
TYPE_CHECKING,
Callable,
Dict,
Iterable,
Expand All @@ -28,10 +29,13 @@
Union,
)

from cylc.flow.subprocctx import SubFuncContext
from cylc.flow.util import format_cmd


if TYPE_CHECKING:
from cylc.flow.subprocctx import SubFuncContext


class CylcError(Exception):
"""Generic exception for Cylc errors.
Expand Down Expand Up @@ -198,7 +202,7 @@ def __init__(
message: str,
platform_name: str,
*,
ctx: Optional[SubFuncContext] = None,
ctx: Optional['SubFuncContext'] = None,
cmd: Optional[Union[str, Iterable]] = None,
ret_code: Optional[int] = None,
out: Optional[str] = None,
Expand Down
11 changes: 9 additions & 2 deletions cylc/flow/flow_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@

"""Manage flow counter and flow metadata."""

from typing import Dict, Set, Optional
from typing import (
Dict,
Set,
Optional,
TYPE_CHECKING
)
import datetime

from cylc.flow import LOG
from cylc.flow.workflow_db_mgr import WorkflowDatabaseManager

if TYPE_CHECKING:
from cylc.flow.workflow_db_mgr import WorkflowDatabaseManager


FlowNums = Set[int]
Expand Down
9 changes: 6 additions & 3 deletions cylc/flow/job_runner_handlers/documentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
not intended to be subclassed.
"""

import re
from typing import (
Iterable,
List,
Tuple,
TYPE_CHECKING
)

if TYPE_CHECKING:
import re


class ExampleHandler():
"""Documentation for writing job runner handlers.
Expand Down Expand Up @@ -258,15 +261,15 @@ class QSUBHandler(PBSHandler):
"""

REC_ID_FROM_SUBMIT_OUT: re.Pattern
REC_ID_FROM_SUBMIT_OUT: 're.Pattern'
"""Regular expression to extract job IDs from submission stderr.
A regular expression (compiled) to extract the job "id" from the standard
output or standard error of the job submission command.
"""

REC_ID_FROM_SUBMIT_ERR: re.Pattern
REC_ID_FROM_SUBMIT_ERR: 're.Pattern'
"""Regular expression to extract job IDs from submission stderr.
See :py:attr:`ExampleHandler.REC_ID_FROM_SUBMIT_OUT`.
Expand Down
5 changes: 2 additions & 3 deletions cylc/flow/network/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from time import sleep
from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Union

from graphql.execution import ExecutionResult
from graphql.execution.executors.asyncio import AsyncioExecutor
import zmq
from zmq.auth.thread import ThreadAuthenticator
Expand All @@ -41,7 +40,7 @@

if TYPE_CHECKING:
from cylc.flow.scheduler import Scheduler

from graphql.execution import ExecutionResult

# maps server methods to the protobuf message (for client/UIS import)
PB_METHOD_MAP = {
Expand Down Expand Up @@ -368,7 +367,7 @@ def graphql(
object: Execution result, or a list with errors.
"""
try:
executed: ExecutionResult = schema.execute(
executed: 'ExecutionResult' = schema.execute(
request_string,
variable_values=variables,
context_value={
Expand Down
37 changes: 20 additions & 17 deletions cylc/flow/parsec/fileparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"""

import os
from optparse import Values
from pathlib import Path
import re
import sys
import typing as t
from typing import (
Dict, Any, Optional, List, TYPE_CHECKING
)

from cylc.flow import __version__, iter_entry_points
from cylc.flow import LOG
Expand All @@ -50,6 +51,8 @@
from cylc.flow.workflow_files import (
get_workflow_source_dir, check_flow_file)

if TYPE_CHECKING:
from optparse import Values

# heading/sections can contain commas (namespace name lists) and any
# regex pattern characters (this was for pre cylc-6 satellite tasks).
Expand Down Expand Up @@ -304,9 +307,9 @@ def process_plugins(fpath, opts):


def merge_template_vars(
native_tvars: t.Dict[str, t.Any],
plugin_result: t.Dict[str, t.Any]
) -> t.Dict[str, t.Any]:
native_tvars: Dict[str, Any],
plugin_result: Dict[str, Any]
) -> Dict[str, Any]:
"""Manage the merger of Cylc Native and Plugin template variables.
Args:
Expand Down Expand Up @@ -347,7 +350,7 @@ def merge_template_vars(
return native_tvars


def _prepend_old_templatevars(fpath: str, template_vars: t.Dict) -> t.Dict:
def _prepend_old_templatevars(fpath: str, template_vars: Dict) -> Dict:
"""If the fpath is in a rundir, extract template variables from database.
Args:
Expand Down Expand Up @@ -386,10 +389,10 @@ def _get_fpath_for_source(fpath: str, opts: "Values") -> str:

def read_and_proc(
fpath: str,
template_vars: t.Optional[t.Dict[str, t.Any]] = None,
viewcfg: t.Any = None,
opts: t.Any = None,
) -> t.List[str]:
template_vars: Optional[Dict[str, Any]] = None,
viewcfg: Any = None,
opts: Any = None,
) -> List[str]:
"""
Read a cylc parsec config file (at fpath), inline any include files,
process with Jinja2, and concatenate continuation lines.
Expand Down Expand Up @@ -516,8 +519,8 @@ def read_and_proc(


def hashbang_and_plugin_templating_clash(
templating: str, flines: t.List[str]
) -> t.Optional[str]:
templating: str, flines: List[str]
) -> Optional[str]:
"""Return file's hashbang/shebang, but raise TemplateVarLanguageClash
if plugin-set template engine and hashbang do not match.
Expand All @@ -544,7 +547,7 @@ def hashbang_and_plugin_templating_clash(
...
cylc.flow.parsec.exceptions.TemplateVarLanguageClash: ...
"""
hashbang: t.Optional[str] = None
hashbang: Optional[str] = None
# Get hashbang if possible:
if flines:
match = re.match(r'^#!(\S+)', flines[0])
Expand All @@ -564,9 +567,9 @@ def hashbang_and_plugin_templating_clash(

def parse(
fpath: str,
output_fname: t.Optional[str] = None,
template_vars: t.Optional[t.Dict[str, t.Any]] = None,
opts: t.Any = None,
output_fname: Optional[str] = None,
template_vars: Optional[Dict[str, Any]] = None,
opts: Any = None,
) -> OrderedDictWithDefaults:
"""Parse file items line-by-line into a corresponding nested dict."""

Expand All @@ -579,7 +582,7 @@ def parse(

nesting_level = 0
config = OrderedDictWithDefaults()
parents: t.List[str] = []
parents: List[str] = []

maxline = len(flines) - 1
index = -1
Expand Down
10 changes: 5 additions & 5 deletions cylc/flow/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import asyncio
from contextlib import suppress
from collections import deque
from optparse import Values
import os
import inspect
from pathlib import Path
Expand Down Expand Up @@ -86,7 +85,6 @@
from cylc.flow.timer import Timer
from cylc.flow.network import API
from cylc.flow.network.authentication import key_housekeeping
from cylc.flow.network.resolvers import TaskMsg
from cylc.flow.network.schema import WorkflowStopMode
from cylc.flow.network.server import WorkflowRuntimeServer
from cylc.flow.option_parsers import (
Expand Down Expand Up @@ -155,6 +153,8 @@
# FROM: Python 3.7
# TO: Python 3.8
from typing_extensions import Literal
from optparse import Values
from cylc.flow.network.resolvers import TaskMsg


class SchedulerStop(CylcError):
Expand Down Expand Up @@ -228,7 +228,7 @@ class Scheduler:

# configuration
config: WorkflowConfig # flow config
options: Values
options: 'Values'
cylc_config: DictTree # [scheduler] config
template_vars: Dict[str, Any]

Expand Down Expand Up @@ -271,7 +271,7 @@ class Scheduler:

time_next_kill: Optional[float] = None

def __init__(self, id_: str, options: Values) -> None:
def __init__(self, id_: str, options: 'Values') -> None:
# flow information
self.workflow = id_
self.workflow_name = get_workflow_name_from_id(self.workflow)
Expand Down Expand Up @@ -875,7 +875,7 @@ def _load_task_run_times(self, row_idx, row):

def process_queued_task_messages(self) -> None:
"""Handle incoming task messages for each task proxy."""
messages: Dict[str, List[Tuple[Optional[int], TaskMsg]]] = {}
messages: Dict[str, List[Tuple[Optional[int], 'TaskMsg']]] = {}
while self.message_queue.qsize():
try:
task_msg = self.message_queue.get(block=False)
Expand Down
4 changes: 2 additions & 2 deletions cylc/flow/scripts/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from functools import partial
from typing import TYPE_CHECKING

from cylc.flow.id import Tokens
from cylc.flow.network.client_factory import get_client
from cylc.flow.network.multi import call_multi
from cylc.flow.option_parsers import (
Expand All @@ -45,6 +44,7 @@

if TYPE_CHECKING:
from optparse import Values
from cylc.flow.id import Tokens


MUTATION = '''
Expand Down Expand Up @@ -74,7 +74,7 @@ def get_option_parser() -> COP:
return parser


async def run(options: 'Values', workflow_id: str, *tokens_list: Tokens):
async def run(options: 'Values', workflow_id: str, *tokens_list: 'Tokens'):
pclient = get_client(workflow_id, timeout=options.comms_timeout)

mutation_kwargs = {
Expand Down
6 changes: 5 additions & 1 deletion cylc/flow/scripts/set_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"""

from functools import partial
from optparse import Values
from typing import TYPE_CHECKING

from cylc.flow.network.client_factory import get_client
from cylc.flow.network.multi import call_multi
Expand All @@ -58,6 +58,10 @@
)
from cylc.flow.terminal import cli_function

if TYPE_CHECKING:
from optparse import Values


MUTATION = '''
mutation (
$wFlows: [WorkflowID]!,
Expand Down
6 changes: 5 additions & 1 deletion cylc/flow/scripts/set_verbosity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"""

from functools import partial
from optparse import Values
from typing import TYPE_CHECKING

from cylc.flow import LOG_LEVELS
from cylc.flow.exceptions import InputError
Expand All @@ -38,6 +38,10 @@
)
from cylc.flow.terminal import cli_function

if TYPE_CHECKING:
from optparse import Values


MUTATION = '''
mutation (
$wFlows: [WorkflowID]!,
Expand Down
7 changes: 5 additions & 2 deletions cylc/flow/scripts/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
"""

import asyncio
import sys

from ansimarkup import parse as cparse
from copy import deepcopy
from optparse import Values
import sys
from typing import TYPE_CHECKING

from cylc.flow import LOG, __version__ as CYLC_VERSION
from cylc.flow.config import WorkflowConfig
Expand All @@ -56,6 +57,8 @@
from cylc.flow.terminal import cli_function
from cylc.flow.scheduler_cli import RUN_MODE

if TYPE_CHECKING:
from optparse import Values

VALIDATE_RUN_MODE = deepcopy(RUN_MODE)
VALIDATE_RUN_MODE.sources = {'validate'}
Expand Down
2 changes: 1 addition & 1 deletion cylc/flow/subprocpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def get_func(mod_name, func_name, src_dir):
Raises:
ImportError, if the module is not found
AtributeError, if the function is not found in the module
AttributeError, if the function is not found in the module
"""
if func_name in _XTRIG_FUNCS:
Expand Down
4 changes: 2 additions & 2 deletions cylc/flow/task_events_mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ def process_message(
if submit_num is None:
submit_num = itask.submit_num
if isinstance(severity, int):
severity = cast(str, getLevelName(severity))
severity = cast('str', getLevelName(severity))
lseverity = str(severity).lower()

# Any message represents activity.
Expand Down Expand Up @@ -811,7 +811,7 @@ def _process_message_check(
)
return False

severity = cast(int, LOG_LEVELS.get(severity, INFO))
severity = cast('int', LOG_LEVELS.get(severity, INFO))
# Demote log level to DEBUG if this is a message that duplicates what
# gets logged by itask state change anyway (and not manual poll)
if severity > DEBUG and flag != self.FLAG_POLLED and message in {
Expand Down
Loading

0 comments on commit 8ad68b9

Please sign in to comment.