From aa100efc638bf35b465a68ce0f4a7d54c1827a60 Mon Sep 17 00:00:00 2001 From: Ronnie Dutta <61982285+MetRonnie@users.noreply.github.com> Date: Mon, 11 Mar 2024 15:47:28 +0000 Subject: [PATCH] Fix type checking errors (#6016) --- cylc/flow/clean.py | 3 ++- cylc/flow/exceptions.py | 15 +++++++-------- cylc/flow/task_remote_mgr.py | 11 +++++------ cylc/flow/util.py | 5 ++--- cylc/flow/workflow_files.py | 18 ++++++++++++------ 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/cylc/flow/clean.py b/cylc/flow/clean.py index c4434dc9244..aec2c4efeb4 100644 --- a/cylc/flow/clean.py +++ b/cylc/flow/clean.py @@ -42,6 +42,7 @@ Optional, Set, Union, + cast, ) from cylc.flow import LOG @@ -399,7 +400,7 @@ def remote_clean( excp = PlatformError( PlatformError.MSG_TIDY, this_platform['name'], - cmd=item.proc.args, + cmd=cast('List[str]', item.proc.args), ret_code=ret_code, out=out, err=err, diff --git a/cylc/flow/exceptions.py b/cylc/flow/exceptions.py index 3ad8c093757..0800a914888 100644 --- a/cylc/flow/exceptions.py +++ b/cylc/flow/exceptions.py @@ -19,8 +19,8 @@ from textwrap import wrap from typing import ( Dict, - Iterable, Optional, + Sequence, Union, TYPE_CHECKING, ) @@ -184,7 +184,7 @@ def __init__( platform_name: str, *, ctx: 'Optional[SubFuncContext]' = None, - cmd: Optional[Union[str, Iterable]] = None, + cmd: Union[str, Sequence[str], None] = None, ret_code: Optional[int] = None, out: Optional[str] = None, err: Optional[str] = None @@ -419,13 +419,12 @@ def get_hint(self): ) ): # likely an issue with the ranking expression - ranking = "\n".join( - wrap( - self.data.get("ranking"), - initial_indent=' ', - subsequent_indent=' ', - ) + lines = wrap( + self.data.get("ranking"), + initial_indent=' ', + subsequent_indent=' ', ) + ranking = "\n".join(lines) return ( 'This is likely an error in the ranking expression:' f'\n{ranking}' diff --git a/cylc/flow/task_remote_mgr.py b/cylc/flow/task_remote_mgr.py index 3743d0545f0..6c2649ca0e4 100644 --- a/cylc/flow/task_remote_mgr.py +++ b/cylc/flow/task_remote_mgr.py @@ -41,6 +41,7 @@ Set, TYPE_CHECKING, Tuple, + cast, ) from cylc.flow import LOG @@ -434,7 +435,7 @@ def remote_tidy(self) -> None: PlatformError( PlatformError.MSG_TIDY, item.platform['name'], - cmd=item.proc.args, + cmd=cast('List[str]', item.proc.args), ret_code=item.proc.returncode, out=out, err=err @@ -451,7 +452,7 @@ def remote_tidy(self) -> None: PlatformError( PlatformError.MSG_TIDY, item.platform['name'], - cmd=item.proc.args, + cmd=cast('List[str]', item.proc.args), ret_code=item.proc.returncode, out=out, err=err, @@ -508,10 +509,8 @@ def _remote_init_callback( install_target=install_target ) old_umask = os.umask(0o177) - with open( - public_key.full_key_path, - 'w', encoding='utf8') as text_file: - text_file.write(key) + with open(public_key.full_key_path, 'w', encoding='utf8') as f: + f.write(key) os.umask(old_umask) # configure_curve must be called every time certificates are # added or removed, in order to update the Authenticator's diff --git a/cylc/flow/util.py b/cylc/flow/util.py index 11cb83c5c25..059237f4e16 100644 --- a/cylc/flow/util.py +++ b/cylc/flow/util.py @@ -22,8 +22,7 @@ from typing import ( Any, List, - Tuple, - Union, + Sequence, ) @@ -77,7 +76,7 @@ def natural_sort(items: List[str], fcns=(int, str)) -> None: items.sort(key=partial(natural_sort_key, fcns=fcns)) -def format_cmd(cmd: Union[List[str], Tuple[str]], maxlen: int = 60) -> str: +def format_cmd(cmd: Sequence[str], maxlen: int = 60) -> str: r"""Convert a shell command list to a user-friendly representation. Examples: diff --git a/cylc/flow/workflow_files.py b/cylc/flow/workflow_files.py index 2f03c8d4394..7d4a67756ad 100644 --- a/cylc/flow/workflow_files.py +++ b/cylc/flow/workflow_files.py @@ -110,15 +110,21 @@ class KeyInfo(): # noqa: SIM119 (not really relevant here) """ - def __init__(self, key_type, key_owner, full_key_path=None, - workflow_srv_dir=None, install_target=None, server_held=True): + def __init__( + self, + key_type: KeyType, + key_owner: KeyOwner, + full_key_path: Optional[str] = None, + workflow_srv_dir: Optional[str] = None, + install_target: Optional[str] = None, + server_held: bool = True + ): self.key_type = key_type self.key_owner = key_owner - self.full_key_path = full_key_path self.workflow_srv_dir = workflow_srv_dir self.install_target = install_target - if self.full_key_path is not None: - self.key_path, self.file_name = os.path.split(self.full_key_path) + if full_key_path is not None: + self.key_path, self.file_name = os.path.split(full_key_path) elif self.workflow_srv_dir is not None: # noqa: SIM106 # Build key filename file_name = key_owner.value @@ -129,7 +135,7 @@ def __init__(self, key_type, key_owner, full_key_path=None, and self.install_target is not None): file_name = f"{file_name}_{self.install_target}" - if key_type == KeyType.PRIVATE: + if key_type is KeyType.PRIVATE: file_extension = WorkflowFiles.Service.PRIVATE_FILE_EXTENSION else: file_extension = WorkflowFiles.Service.PUBLIC_FILE_EXTENSION