Skip to content

Commit 7c43ec6

Browse files
authored
Merge pull request #194 from openatx/copilot/fix-193
Optimize shell and shell2 method type hints using @overload and Literal
2 parents a19383b + 50102dc commit 7c43ec6

2 files changed

Lines changed: 101 additions & 3 deletions

File tree

adbutils/_device_base.py

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
import subprocess
1212
import threading
1313
import typing
14-
from typing import List, Optional, Union
14+
from typing import List, Optional, Union, overload
15+
16+
try:
17+
from typing import Literal
18+
except ImportError:
19+
from typing_extensions import Literal
1520

1621
from PIL import Image, UnidentifiedImageError
1722
from deprecation import deprecated
@@ -172,6 +177,47 @@ def open_shell(self, cmdargs: Union[str, list, tuple]) -> AdbConnection:
172177
c.check_okay()
173178
return c
174179

180+
@overload
181+
def shell(
182+
self,
183+
cmdargs: Union[str, list, tuple],
184+
stream: Literal[True],
185+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
186+
encoding: str | None = "utf-8",
187+
rstrip=True,
188+
) -> AdbConnection: ...
189+
190+
@overload
191+
def shell(
192+
self,
193+
cmdargs: Union[str, list, tuple],
194+
*,
195+
stream: Literal[False] = False,
196+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
197+
encoding: Literal[None],
198+
rstrip=True,
199+
) -> bytes: ...
200+
201+
@overload
202+
def shell(
203+
self,
204+
cmdargs: Union[str, list, tuple],
205+
stream: Literal[False] = False,
206+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
207+
*,
208+
encoding: str,
209+
rstrip=True,
210+
) -> str: ...
211+
212+
@overload
213+
def shell(
214+
self,
215+
cmdargs: Union[str, list, tuple],
216+
stream: Literal[False] = False,
217+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
218+
rstrip=True,
219+
) -> str: ...
220+
175221
def shell(
176222
self,
177223
cmdargs: Union[str, list, tuple],
@@ -218,6 +264,37 @@ def shell(
218264
return output.rstrip() if rstrip else output
219265
return output
220266

267+
@overload
268+
def shell2(
269+
self,
270+
cmdargs: Union[str, list, tuple],
271+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
272+
*,
273+
encoding: Literal[None],
274+
rstrip=False,
275+
v2=False,
276+
) -> ShellReturnRaw: ...
277+
278+
@overload
279+
def shell2(
280+
self,
281+
cmdargs: Union[str, list, tuple],
282+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
283+
*,
284+
encoding: str,
285+
rstrip=False,
286+
v2=False,
287+
) -> ShellReturn: ...
288+
289+
@overload
290+
def shell2(
291+
self,
292+
cmdargs: Union[str, list, tuple],
293+
timeout: Optional[float] = _DEFAULT_SOCKET_TIMEOUT,
294+
rstrip=False,
295+
v2=False,
296+
) -> ShellReturn: ...
297+
221298
def shell2(
222299
self,
223300
cmdargs: Union[str, list, tuple],

adbutils/_interfaces.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import abc
2-
from typing import List, Optional, Union
2+
from typing import List, Optional, Union, overload
3+
4+
try:
5+
from typing import Literal
6+
except ImportError:
7+
from typing_extensions import Literal
38

49
from adbutils._adb import AdbConnection, Network
510
from adbutils.sync import Sync
6-
from adbutils._proto import ShellReturn
11+
from adbutils._proto import ShellReturn, ShellReturnRaw
712

813
class AbstractShellDevice(abc.ABC):
914
@abc.abstractmethod
@@ -17,10 +22,26 @@ def sync(self) -> Sync:
1722

1823

1924
class AbstractDevice(abc.ABC):
25+
@overload
26+
@abc.abstractmethod
27+
def shell(self, cmd: str, stream: Literal[True]) -> AdbConnection: ...
28+
29+
@overload
30+
@abc.abstractmethod
31+
def shell(self, cmd: str, stream: Literal[False] = False) -> str: ...
32+
2033
@abc.abstractmethod
2134
def shell(self, cmd: str, stream: bool = False) -> Union[str, AdbConnection]:
2235
pass
2336

37+
@overload
38+
@abc.abstractmethod
39+
def shell2(self, cmd: str, encoding: Literal[None]) -> ShellReturnRaw: ...
40+
41+
@overload
42+
@abc.abstractmethod
43+
def shell2(self, cmd: str, encoding: str = "utf-8") -> ShellReturn: ...
44+
2445
@abc.abstractmethod
2546
def shell2(self, cmd: str) -> ShellReturn:
2647
pass

0 commit comments

Comments
 (0)