Skip to content

Commit 702cddb

Browse files
authored
Fix typing annotations (#27)
* Fix typing annotations * Fix errors in type annotations * Reduce CI useless tasks amount * do not cythonize py27
1 parent 42ebef3 commit 702cddb

15 files changed

Lines changed: 88 additions & 138 deletions

README.rst

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -315,19 +315,7 @@ Kwargs set properties:
315315
Testing
316316
=======
317317
The main test mechanism for the package `exec-helpers` is using `tox`.
318-
Test environments available:
319-
320-
::
321-
322-
pep8
323-
py27
324-
py34
325-
py35
326-
py36
327-
pypy
328-
pypy3
329-
pylint
330-
pep257
318+
Available environments can be collected via `tox -l`
331319

332320
CI systems
333321
==========

doc/source/SSHClient.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ API: SSHClient and SSHAuth.
118118
:param log_mask_re: regex lookup rule to mask command for logger.
119119
all MATCHED groups will be replaced by '<*masked*>'
120120
:type log_mask_re: typing.Optional[str]
121-
:rtype: ``typing.Tuple[paramiko.Channel, paramiko.ChannelFile, paramiko.ChannelFile, paramiko.ChannelFile]``
121+
:rtype: ``typing.Tuple[paramiko.Channel, paramiko.ChannelFile, typing.Optional[paramiko.ChannelFile], typing.Optional[paramiko.ChannelFile]]``
122122

123123
.. versionchanged:: 1.2.0 open_stdout and open_stderr flags
124124
.. versionchanged:: 1.2.0 stdin data

doc/source/Subprocess.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ API: Subprocess
5656
:param log_mask_re: regex lookup rule to mask command for logger.
5757
all MATCHED groups will be replaced by '<*masked*>'
5858
:type log_mask_re: typing.Optional[str]
59-
:rtype: ``typing.Tuple[subprocess.Popen, None, typing.Optional[io.TextIOWrapper], typing.Optional[io.TextIOWrapper], ]``
59+
:rtype: ``typing.Tuple[subprocess.Popen, None, typing.Optional[typing.IO], typing.Optional[typing.IO], ]``
6060

6161
.. versionadded:: 1.2.0
6262

exec_helpers/_api.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import logging
2626
import re
2727
import threading
28-
import typing # noqa # pylint: disable=unused-import
28+
import typing
2929

3030
import six # noqa # pylint: disable=unused-import
3131

@@ -35,6 +35,9 @@
3535
from exec_helpers import proc_enums
3636
from exec_helpers import _log_templates
3737

38+
_type_exit_codes = typing.Union[int, proc_enums.ExitCodes]
39+
_type_expected = typing.Optional[typing.Iterable[_type_exit_codes]]
40+
3841

3942
class ExecHelper(object):
4043
"""ExecHelper global API."""
@@ -171,9 +174,9 @@ def execute_async(
171174
def _exec_command(
172175
self,
173176
command, # type: str
174-
interface, # type: typing.Any,
175-
stdout, # type: typing.Any,
176-
stderr, # type: typing.Any,
177+
interface, # type: typing.Any
178+
stdout, # type: typing.Any
179+
stderr, # type: typing.Any
177180
timeout, # type: int
178181
verbose=False, # type: bool
179182
log_mask_re=None, # type: typing.Optional[str]
@@ -227,10 +230,10 @@ def execute(
227230
"""
228231
with self.lock:
229232
(
230-
iface, # type: typing.Any
233+
iface,
231234
_,
232-
stderr, # type: typing.Any
233-
stdout, # type: typing.Any
235+
stderr,
236+
stdout,
234237
) = self.execute_async(
235238
command,
236239
verbose=verbose,

exec_helpers/_ssh_client_base.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@
6565
_type_execute_async = typing.Tuple[
6666
paramiko.Channel,
6767
paramiko.ChannelFile,
68-
paramiko.ChannelFile,
69-
paramiko.ChannelFile
68+
typing.Optional[paramiko.ChannelFile],
69+
typing.Optional[paramiko.ChannelFile]
7070
]
7171

7272
CPYTHON = 'CPython' == platform.python_implementation()
@@ -101,7 +101,7 @@ class _MemorizedSSH(type):
101101
duplicates is possible.
102102
"""
103103

104-
__cache = {}
104+
__cache = {} # type: typing.Dict[typing.Tuple[str, int], SSHClientBase]
105105

106106
@classmethod
107107
def __prepare__(
@@ -622,7 +622,7 @@ def poll_pipes(
622622
:type stop: Event
623623
:type channel: paramiko.channel.Channel
624624
"""
625-
while not stop.isSet():
625+
while not stop.is_set():
626626
time.sleep(0.1)
627627
if stdout or stderr:
628628
poll_streams(result=result)
@@ -662,7 +662,7 @@ def poll_pipes(
662662
concurrent.futures.wait([future], timeout)
663663

664664
# Process closed?
665-
if stop_event.isSet():
665+
if stop_event.is_set():
666666
stop_event.clear()
667667
interface.close()
668668
return result
@@ -764,7 +764,7 @@ def execute_together(
764764
remotes, # type: typing.Iterable[SSHClientBase]
765765
command, # type: str
766766
timeout=constants.DEFAULT_TIMEOUT, # type: typing.Optional[int]
767-
expected=None, # type: typing.Optional[typing.Iterable[]]
767+
expected=None, # type: typing.Optional[typing.Iterable[int]]
768768
raise_on_err=True, # type: bool
769769
**kwargs
770770
): # type: (...) -> _type_multiple_results
@@ -782,10 +782,8 @@ def execute_together(
782782
:type raise_on_err: bool
783783
:return: dictionary {(hostname, port): result}
784784
:rtype: typing.Dict[typing.Tuple[str, int], exec_result.ExecResult]
785-
:raises ParallelCallProcessError:
786-
Unexpected any code at lest on one target
787-
:raises ParallelCallExceptions:
788-
At lest one exception raised during execution (including timeout)
785+
:raises ParallelCallProcessError: Unexpected any code at lest on one target
786+
:raises ParallelCallExceptions: At lest one exception raised during execution (including timeout)
789787
790788
.. versionchanged:: 1.2.0 default timeout 1 hour
791789
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
@@ -796,11 +794,14 @@ def get_result(
796794
): # type: (...) -> exec_result.ExecResult
797795
"""Get result from remote call."""
798796
(
799-
chan, # type: paramiko.channel.Channel
797+
chan,
800798
_,
801-
stderr, # type: paramiko.channel.ChannelFile
802-
stdout, # type: paramiko.channel.ChannelFile
803-
) = remote.execute_async(command, **kwargs)
799+
stderr,
800+
stdout,
801+
) = remote.execute_async(
802+
command,
803+
**kwargs
804+
) # type: _type_execute_async
804805

805806
chan.status_event.wait(timeout)
806807
exit_code = chan.recv_exit_status()
@@ -832,19 +833,19 @@ def get_result(
832833
futures[remote] = get_result(remote)
833834

834835
(
835-
_, # type: typing.Set[concurrent.futures.Future]
836-
not_done, # type: typing.Set[concurrent.futures.Future]
836+
_,
837+
not_done,
837838
) = concurrent.futures.wait(
838839
list(futures.values()),
839840
timeout=timeout
840-
)
841+
) # type: typing.Set[concurrent.futures.Future], typing.Set[concurrent.futures.Future]
841842
for future in not_done:
842843
future.cancel()
843844

844845
for (
845-
remote, # type: SSHClientBase
846-
future, # type: concurrent.futures.Future
847-
) in futures.items():
846+
remote,
847+
future,
848+
) in futures.items(): # type: SSHClientBase, concurrent.futures.Future
848849
try:
849850
result = future.result()
850851
results[(remote.hostname, remote.port)] = result

exec_helpers/exceptions.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(
7171
self,
7272
result=None, # type: exec_result.ExecResult
7373
expected=None, # type: typing.Optional[typing.List[_type_exit_codes]]
74-
):
74+
): # type: (...) -> None
7575
"""Exception for error on process calls.
7676
7777
:param result: execution result
@@ -136,10 +136,10 @@ def __init__(
136136
self,
137137
command, # type: str
138138
exceptions, # type: typing.Dict[typing.Tuple[str, int], Exception]
139-
errors, # type: _type_multiple_results,
140-
results, # type: _type_multiple_results,
139+
errors, # type: _type_multiple_results
140+
results, # type: _type_multiple_results
141141
expected=None, # type: typing.Optional[typing.List[_type_exit_codes]]
142-
):
142+
): # type: (...) -> None
143143
"""Exception raised during parallel call as result of exceptions.
144144
145145
:param command: command
@@ -190,10 +190,10 @@ class ParallelCallProcessError(ExecCalledProcessError):
190190
def __init__(
191191
self,
192192
command, # type: str
193-
errors, # type: _type_multiple_results,
194-
results, # type: _type_multiple_results,
193+
errors, # type: _type_multiple_results
194+
results, # type: _type_multiple_results
195195
expected=None, # type: typing.Optional[typing.List[_type_exit_codes]]
196-
):
196+
): # type: (...) -> None
197197
"""Exception during parallel execution.
198198
199199
:param command: command

exec_helpers/exec_result.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __init__(
5555
stdout=None, # type: typing.Optional[typing.Iterable[bytes]]
5656
stderr=None, # type: typing.Optional[typing.Iterable[bytes]]
5757
exit_code=proc_enums.ExitCodes.EX_INVALID # type: _type_exit_codes
58-
):
58+
): # type: (...) -> None
5959
"""Command execution result.
6060
6161
:param cmd: command
@@ -77,8 +77,8 @@ def __init__(
7777
elif isinstance(stdin, bytearray):
7878
stdin = self._get_str_from_bin(stdin)
7979
self.__stdin = stdin
80-
self.__stdout = tuple(stdout) if stdout is not None else ()
81-
self.__stderr = tuple(stderr) if stderr is not None else ()
80+
self.__stdout = tuple(stdout) if stdout is not None else () # type: typing.Tuple[bytes]
81+
self.__stderr = tuple(stderr) if stderr is not None else () # type: typing.Tuple[bytes]
8282

8383
self.__exit_code = None
8484
self.__timestamp = None
@@ -99,7 +99,7 @@ def lock(self): # type: () -> threading.RLock
9999
return self.__lock
100100

101101
@property
102-
def timestamp(self): # type: () -> typing.Optional(datetime.datetime)
102+
def timestamp(self): # type: () -> typing.Optional[datetime.datetime]
103103
"""Timestamp.
104104
105105
:rtype: typing.Optional(datetime.datetime)
@@ -133,10 +133,10 @@ def _get_str_from_bin(src): # type: (bytearray) -> str
133133
def _get_brief(cls, data): # type: (typing.Tuple[bytes]) -> str
134134
"""Get brief output: 7 lines maximum (3 first + ... + 3 last).
135135
136-
:type data: typing.List[bytes]
136+
:type data: typing.Tuple[bytes]
137137
:rtype: str
138138
"""
139-
src = data if len(data) <= 7 else data[:3] + (b'...\n',) + data[-3:]
139+
src = data if len(data) <= 7 else data[:3] + (b'...\n',) + data[-3:] # type: typing.Tuple[bytes]
140140
return cls._get_str_from_bin(
141141
cls._get_bytearray_from_array(src)
142142
)

exec_helpers/proc_enums.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def exit_code_to_enum(code): # type: (_type_exit_codes) -> _type_exit_codes
169169

170170

171171
def exit_codes_to_enums(
172-
codes=None # type: typing.Optional[typing.List[_type_exit_codes]]
172+
codes=None # type: typing.Optional[typing.Iterable[_type_exit_codes]]
173173
): # type: (...) -> typing.List[_type_exit_codes]
174174
"""Convert integer exit codes to enums."""
175175
if codes is None:

exec_helpers/ssh_auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ def __init__(
5252
username=None, # type: typing.Optional[str]
5353
password=None, # type: typing.Optional[str]
5454
key=None, # type: typing.Optional[paramiko.RSAKey]
55-
keys=None, # type: typing.Optional[typing.Iterable[paramiko.RSAKey]],
55+
keys=None, # type: typing.Optional[typing.Iterable[paramiko.RSAKey]]
5656
key_filename=None, # type: typing.Union[typing.List[str], str, None]
5757
passphrase=None, # type: typing.Optional[str]
58-
):
58+
): # type: (...) -> None
5959
"""SSH credentials object.
6060
6161
Used to authorize SSHClient.
@@ -137,7 +137,7 @@ def enter_password(self, tgt): # type: (io.StringIO) -> None
137137
:type tgt: file
138138
"""
139139
# noinspection PyTypeChecker
140-
return tgt.write('{}\n'.format(self.__password))
140+
tgt.write('{}\n'.format(self.__password))
141141

142142
def connect(
143143
self,
@@ -166,7 +166,7 @@ def connect(
166166
'password': self.__password,
167167
'key_filename': self.key_filename,
168168
'passphrase': self.__passphrase,
169-
}
169+
} # type: typing.Dict[str, typing.Any]
170170
if hostname is not None:
171171
kwargs['hostname'] = hostname
172172
kwargs['port'] = port

0 commit comments

Comments
 (0)