Skip to content

Commit 1996ae9

Browse files
allisonkarlitskayamartinpitt
authored andcommitted
machine: type-annotate the Timeout class
This is not as nice as it could be: my first choice here would have been to eliminate the dependency inversion (that Timeout knows what a "machine" is) and avoid the cyclic import, but there are external users of this API which depend on the `machine=` kwarg, so we need to keep it for now.
1 parent ae6520d commit 1996ae9

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

machine/machine_core/timeout.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
# along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
1717

1818
import signal
19+
import typing
20+
21+
if typing.TYPE_CHECKING:
22+
from .ssh_connection import SSHConnection
1923

2024

2125
class Timeout:
2226
""" Add a timeout to an operation
2327
Specify machine to ensure that a machine's ssh operations are canceled when the timer expires.
2428
"""
25-
def __init__(self, seconds=1, error_message='Timeout', machine=None):
29+
def __init__(self, seconds: int = 1, error_message: str = 'Timeout', machine: 'SSHConnection | None' = None):
2630
if signal.getsignal(signal.SIGALRM) != signal.SIG_DFL:
2731
# there is already a different Timeout active
2832
self.seconds = None
@@ -32,20 +36,20 @@ def __init__(self, seconds=1, error_message='Timeout', machine=None):
3236
self.error_message = error_message
3337
self.machine = machine
3438

35-
def handle_timeout(self, signum, frame):
39+
def handle_timeout(self, _signum: int, _frame: object) -> None:
3640
if self.machine:
3741
if self.machine.ssh_process:
3842
self.machine.ssh_process.terminate()
3943
self.machine.disconnect()
4044

4145
raise RuntimeError(self.error_message)
4246

43-
def __enter__(self):
47+
def __enter__(self) -> None:
4448
if self.seconds:
4549
signal.signal(signal.SIGALRM, self.handle_timeout)
4650
signal.alarm(self.seconds)
4751

48-
def __exit__(self, _type, value, traceback):
52+
def __exit__(self, _type: object, _value: object, _traceback: object) -> None:
4953
if self.seconds:
5054
signal.alarm(0)
5155
signal.signal(signal.SIGALRM, signal.SIG_DFL)

0 commit comments

Comments
 (0)