1717import shlex
1818import subprocess
1919import urllib .parse
20+ from typing import Any , Optional
2021
2122logger = logging .getLogger ("testinfra" )
2223
2627class CommandResult :
2728 def __init__ (
2829 self ,
29- backend ,
30- exit_status ,
31- command ,
32- stdout_bytes ,
33- stderr_bytes ,
34- stdout = None ,
35- stderr = None ,
30+ backend : "BaseBackend" ,
31+ exit_status : int ,
32+ command : bytes ,
33+ stdout_bytes : bytes ,
34+ stderr_bytes : bytes ,
35+ stdout : Optional [ str ] = None ,
36+ stderr : Optional [ str ] = None ,
3637 ):
3738 self .exit_status = exit_status
3839 self ._stdout_bytes = stdout_bytes
@@ -44,7 +45,7 @@ def __init__(
4445 super ().__init__ ()
4546
4647 @property
47- def succeeded (self ):
48+ def succeeded (self ) -> bool :
4849 """Returns whether the command was successful
4950
5051 >>> host.run("true").succeeded
@@ -53,7 +54,7 @@ def succeeded(self):
5354 return self .exit_status == 0
5455
5556 @property
56- def failed (self ):
57+ def failed (self ) -> bool :
5758 """Returns whether the command failed
5859
5960 >>> host.run("false").failed
@@ -62,7 +63,7 @@ def failed(self):
6263 return self .exit_status != 0
6364
6465 @property
65- def rc (self ):
66+ def rc (self ) -> int :
6667 """Gets the returncode of a command
6768
6869 >>> host.run("true").rc
@@ -71,30 +72,30 @@ def rc(self):
7172 return self .exit_status
7273
7374 @property
74- def stdout (self ):
75+ def stdout (self ) -> str :
7576 if self ._stdout is None :
7677 self ._stdout = self ._backend .decode (self ._stdout_bytes )
7778 return self ._stdout
7879
7980 @property
80- def stderr (self ):
81+ def stderr (self ) -> str :
8182 if self ._stderr is None :
8283 self ._stderr = self ._backend .decode (self ._stderr_bytes )
8384 return self ._stderr
8485
8586 @property
86- def stdout_bytes (self ):
87+ def stdout_bytes (self ) -> bytes :
8788 if self ._stdout_bytes is None :
8889 self ._stdout_bytes = self ._backend .encode (self ._stdout )
8990 return self ._stdout_bytes
9091
9192 @property
92- def stderr_bytes (self ):
93+ def stderr_bytes (self ) -> bytes :
9394 if self ._stderr_bytes is None :
9495 self ._stderr_bytes = self ._backend .encode (self ._stderr )
9596 return self ._stderr_bytes
9697
97- def __repr__ (self ):
98+ def __repr__ (self ) -> str :
9899 return (
99100 "CommandResult(command={!r}, exit_status={}, stdout={!r}, " "stderr={!r})"
100101 ).format (
@@ -112,7 +113,14 @@ class BaseBackend(metaclass=abc.ABCMeta):
112113 HAS_RUN_ANSIBLE = False
113114 NAME : str
114115
115- def __init__ (self , hostname , sudo = False , sudo_user = None , * args , ** kwargs ):
116+ def __init__ (
117+ self ,
118+ hostname : str ,
119+ sudo : bool = False ,
120+ sudo_user : Optional [bool ] = None ,
121+ * args : Any ,
122+ ** kwargs : Any ,
123+ ):
116124 self ._encoding = None
117125 self ._host = None
118126 self .hostname = hostname
@@ -245,7 +253,7 @@ def parse_containerspec(containerspec):
245253 user , name = name .split ("@" , 1 )
246254 return name , user
247255
248- def get_encoding (self ) -> str :
256+ def get_encoding (self ):
249257 encoding = None
250258 for python in ("python3" , "python" ):
251259 cmd = self .run (
@@ -271,19 +279,19 @@ def encoding(self):
271279 self ._encoding = self .get_encoding ()
272280 return self ._encoding
273281
274- def decode (self , data ) :
282+ def decode (self , data : bytes ) -> str :
275283 try :
276284 return data .decode ("ascii" )
277285 except UnicodeDecodeError :
278286 return data .decode (self .encoding )
279287
280- def encode (self , data ) :
288+ def encode (self , data : str ) -> bytes :
281289 try :
282290 return data .encode ("ascii" )
283291 except UnicodeEncodeError :
284292 return data .encode (self .encoding )
285293
286- def result (self , * args , ** kwargs ) :
294+ def result (self , * args : Any , ** kwargs : Any ) -> CommandResult :
287295 result = CommandResult (self , * args , ** kwargs )
288296 logger .debug ("RUN %s" , result )
289297 return result
0 commit comments