17
17
import shlex
18
18
import subprocess
19
19
import urllib .parse
20
+ from typing import Any , Optional
20
21
21
22
logger = logging .getLogger ("testinfra" )
22
23
26
27
class CommandResult :
27
28
def __init__ (
28
29
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 ,
36
37
):
37
38
self .exit_status = exit_status
38
39
self ._stdout_bytes = stdout_bytes
@@ -44,7 +45,7 @@ def __init__(
44
45
super ().__init__ ()
45
46
46
47
@property
47
- def succeeded (self ):
48
+ def succeeded (self ) -> bool :
48
49
"""Returns whether the command was successful
49
50
50
51
>>> host.run("true").succeeded
@@ -53,7 +54,7 @@ def succeeded(self):
53
54
return self .exit_status == 0
54
55
55
56
@property
56
- def failed (self ):
57
+ def failed (self ) -> bool :
57
58
"""Returns whether the command failed
58
59
59
60
>>> host.run("false").failed
@@ -62,7 +63,7 @@ def failed(self):
62
63
return self .exit_status != 0
63
64
64
65
@property
65
- def rc (self ):
66
+ def rc (self ) -> int :
66
67
"""Gets the returncode of a command
67
68
68
69
>>> host.run("true").rc
@@ -71,30 +72,30 @@ def rc(self):
71
72
return self .exit_status
72
73
73
74
@property
74
- def stdout (self ):
75
+ def stdout (self ) -> str :
75
76
if self ._stdout is None :
76
77
self ._stdout = self ._backend .decode (self ._stdout_bytes )
77
78
return self ._stdout
78
79
79
80
@property
80
- def stderr (self ):
81
+ def stderr (self ) -> str :
81
82
if self ._stderr is None :
82
83
self ._stderr = self ._backend .decode (self ._stderr_bytes )
83
84
return self ._stderr
84
85
85
86
@property
86
- def stdout_bytes (self ):
87
+ def stdout_bytes (self ) -> bytes :
87
88
if self ._stdout_bytes is None :
88
89
self ._stdout_bytes = self ._backend .encode (self ._stdout )
89
90
return self ._stdout_bytes
90
91
91
92
@property
92
- def stderr_bytes (self ):
93
+ def stderr_bytes (self ) -> bytes :
93
94
if self ._stderr_bytes is None :
94
95
self ._stderr_bytes = self ._backend .encode (self ._stderr )
95
96
return self ._stderr_bytes
96
97
97
- def __repr__ (self ):
98
+ def __repr__ (self ) -> str :
98
99
return (
99
100
"CommandResult(command={!r}, exit_status={}, stdout={!r}, " "stderr={!r})"
100
101
).format (
@@ -112,7 +113,14 @@ class BaseBackend(metaclass=abc.ABCMeta):
112
113
HAS_RUN_ANSIBLE = False
113
114
NAME : str
114
115
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
+ ):
116
124
self ._encoding = None
117
125
self ._host = None
118
126
self .hostname = hostname
@@ -245,7 +253,7 @@ def parse_containerspec(containerspec):
245
253
user , name = name .split ("@" , 1 )
246
254
return name , user
247
255
248
- def get_encoding (self ) -> str :
256
+ def get_encoding (self ):
249
257
encoding = None
250
258
for python in ("python3" , "python" ):
251
259
cmd = self .run (
@@ -271,19 +279,19 @@ def encoding(self):
271
279
self ._encoding = self .get_encoding ()
272
280
return self ._encoding
273
281
274
- def decode (self , data ) :
282
+ def decode (self , data : bytes ) -> str :
275
283
try :
276
284
return data .decode ("ascii" )
277
285
except UnicodeDecodeError :
278
286
return data .decode (self .encoding )
279
287
280
- def encode (self , data ) :
288
+ def encode (self , data : str ) -> bytes :
281
289
try :
282
290
return data .encode ("ascii" )
283
291
except UnicodeEncodeError :
284
292
return data .encode (self .encoding )
285
293
286
- def result (self , * args , ** kwargs ) :
294
+ def result (self , * args : Any , ** kwargs : Any ) -> CommandResult :
287
295
result = CommandResult (self , * args , ** kwargs )
288
296
logger .debug ("RUN %s" , result )
289
297
return result
0 commit comments