From dc7458f69863e6d7b9edde0d0cb5f433e923bd62 Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Tue, 17 Jul 2018 17:03:48 +0200 Subject: [PATCH] Declare more types (add local mypy target), block pylint>=2 --- exec_helpers/_api.pyi | 12 ++++++------ exec_helpers/_ssh_client_base.pyi | 18 +++++++++--------- exec_helpers/proc_enums.py | 6 +++--- exec_helpers/subprocess_runner.pyi | 25 +++++++++++++++++++------ tox.ini | 8 +++++++- 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/exec_helpers/_api.pyi b/exec_helpers/_api.pyi index 5f27ad0..90660f3 100644 --- a/exec_helpers/_api.pyi +++ b/exec_helpers/_api.pyi @@ -15,7 +15,7 @@ class ExecHelper: @property def lock(self) -> threading.RLock: ... - def __enter__(self): ... + def __enter__(self) -> ExecHelper: ... def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None: ... @@ -29,7 +29,7 @@ class ExecHelper: open_stderr: bool=..., verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs + **kwargs: typing.Dict ) -> typing.Tuple[typing.Any, typing.Any, typing.Any, typing.Any,]: ... def _exec_command( @@ -41,7 +41,7 @@ class ExecHelper: timeout: typing.Union[int, None], verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs + **kwargs: typing.Dict ) -> exec_result.ExecResult: ... def execute( @@ -49,7 +49,7 @@ class ExecHelper: command: str, verbose: bool=..., timeout: typing.Union[int, None]=..., - **kwargs + **kwargs: typing.Type ) -> exec_result.ExecResult: ... def check_call( @@ -60,7 +60,7 @@ class ExecHelper: error_info: typing.Optional[str]=..., expected: typing.Optional[typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]]=..., raise_on_err: bool=..., - **kwargs + **kwargs: typing.Type ) -> exec_result.ExecResult: ... def check_stderr( @@ -70,5 +70,5 @@ class ExecHelper: timeout: typing.Union[int, None]=..., error_info: typing.Optional[str]=..., raise_on_err: bool=..., - **kwargs + **kwargs: typing.Dict ) -> exec_result.ExecResult: ... diff --git a/exec_helpers/_ssh_client_base.pyi b/exec_helpers/_ssh_client_base.pyi index 42f3d52..16b52c6 100644 --- a/exec_helpers/_ssh_client_base.pyi +++ b/exec_helpers/_ssh_client_base.pyi @@ -8,10 +8,10 @@ from exec_helpers import exec_result, ssh_auth, _api class _MemorizedSSH(type): @classmethod - def __prepare__(mcs, name: str, bases: typing.Iterable[typing.Type], **kwargs) -> collections.OrderedDict: ... + def __prepare__(mcs: typing.Type, name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ... def __call__( # type: ignore - cls, + cls: typing.Type[SSHClientBase], host: str, port: int=..., username: typing.Optional[str]=..., @@ -22,14 +22,14 @@ class _MemorizedSSH(type): ) -> SSHClientBase: ... @classmethod - def clear_cache(mcs) -> None: ... + def clear_cache(mcs: typing.Type[SSHClientBase]) -> None: ... # type: ignore @classmethod - def close_connections(mcs) -> None: ... + def close_connections(mcs: typing.Type[SSHClientBase]) -> None: ... # type: ignore class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH): - def __hash__(self): ... + def __hash__(self) -> int: ... def __init__( self, @@ -100,7 +100,7 @@ class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH): open_stderr: bool=..., verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs + **kwargs: typing.Dict ) -> typing.Tuple[paramiko.Channel, paramiko.ChannelFile, typing.Optional[paramiko.ChannelFile], typing.Optional[paramiko.ChannelFile]]: ... def _exec_command( @@ -112,7 +112,7 @@ class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH): timeout: typing.Union[int, None], verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs + **kwargs: typing.Dict ) -> exec_result.ExecResult: ... def execute_through_host( @@ -124,7 +124,7 @@ class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH): verbose: bool=..., timeout: typing.Union[int, None]=..., get_pty: bool=..., - **kwargs + **kwargs: typing.Dict ) -> exec_result.ExecResult: ... @classmethod @@ -135,7 +135,7 @@ class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH): timeout: typing.Union[int, None]=..., expected: typing.Optional[typing.Iterable[int]]=..., raise_on_err: bool=..., - **kwargs + **kwargs: typing.Dict ) -> typing.Dict[typing.Tuple[str, int], exec_result.ExecResult]: ... def open(self, path: str, mode: str = ...) -> paramiko.SFTPFile: ... diff --git a/exec_helpers/proc_enums.py b/exec_helpers/proc_enums.py index ddf4bb5..00fa4f4 100644 --- a/exec_helpers/proc_enums.py +++ b/exec_helpers/proc_enums.py @@ -72,9 +72,9 @@ class SigNum(enum.IntEnum): SIGPWR = 30 # Power failure restart (System V). SIGSYS = 31 # Bad system call. - def __str__(self): # pragma: no cover + def __str__(self): # type: () -> str """Representation for logs.""" - return "{name}<{value:d}(0x{value:02X})>".format( + return "{name}<{value:d}(0x{value:02X})>".format( # pragma: no cover name=self.name, value=self.value ) @@ -150,7 +150,7 @@ class ExitCodes(digit_type, enum.Enum): EX_SIGPWR = 128 + SigNum.SIGPWR EX_SIGSYS = 128 + SigNum.SIGSYS - def __str__(self): + def __str__(self): # type: () -> str """Representation for logs.""" return "{name}<{value:d}(0x{value:02X})>".format( name=self.name, diff --git a/exec_helpers/subprocess_runner.pyi b/exec_helpers/subprocess_runner.pyi index ff2c768..ced1f29 100644 --- a/exec_helpers/subprocess_runner.pyi +++ b/exec_helpers/subprocess_runner.pyi @@ -16,10 +16,10 @@ class SingletonMeta(type): _instances: typing.Dict[typing.Type, typing.Any] = ... _lock: threading.RLock = ... - def __call__(cls, *args, **kwargs): ... + def __call__(cls: typing.Type, *args: typing.Tuple, **kwargs: typing.Dict) -> typing.Any: ... @classmethod - def __prepare__(mcs, name: str, bases: typing.Iterable[typing.Type], **kwargs) -> collections.OrderedDict: ... + def __prepare__(mcs: typing.Type, name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ... def set_nonblocking_pipe(pipe: typing.Any) -> None: ... @@ -40,16 +40,29 @@ class Subprocess(_api.ExecHelper, metaclass=SingletonMeta): timeout: typing.Union[int, None], verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs + **kwargs: typing.Dict ) -> exec_result.ExecResult: ... + @typing.overload # type: ignore def execute_async( self, command: str, - stdin: typing.Union[typing.AnyStr, bytearray, None]=..., + stdin: typing.Union[typing.AnyStr, bytearray]=..., open_stdout: bool=..., open_stderr: bool=..., verbose: bool=..., log_mask_re: typing.Optional[str]=..., - **kwargs - ) -> typing.Tuple[subprocess.Popen, None, typing.Optional[typing.IO], typing.Optional[typing.IO]]: ... + **kwargs: typing.Dict + ) -> typing.Tuple[subprocess.Popen, None, None, None]: ... + + @typing.overload + def execute_async( + self, + command: str, + stdin: None=..., + open_stdout: bool=..., + open_stderr: bool=..., + verbose: bool=..., + log_mask_re: typing.Optional[str]=..., + **kwargs: typing.Dict + ) -> typing.Tuple[subprocess.Popen, None, typing.IO, typing.IO]: ... diff --git a/tox.ini b/tox.ini index ded0d26..9c78880 100644 --- a/tox.ini +++ b/tox.ini @@ -82,7 +82,7 @@ commands = pip install ./ -vvv -U basepython = python3.6 usedevelop = False deps = - pylint + pylint<2 -r{toxinidir}/CI_REQUIREMENTS.txt commands = pylint exec_helpers @@ -141,3 +141,9 @@ deps = . pipdeptree commands = pipdeptree + +[testenv:mypy] +deps = + mypy>=0.620 + -r{toxinidir}/CI_REQUIREMENTS.txt +commands = mypy --strict exec_helpers