Skip to content

Commit e21b34f

Browse files
author
welli7ngton
committed
MNT: Add type hints to Mouse methods
- Added explicit return types to various DesktopBot methods. - Introduced Literal for button parameters to restrict values: - I realized that the Button type is an Enum, from now on the bot only accepts the literal values of left, right and middle because I didn't find a better way to represent this enum, what do you guys think. - Ensured consistent type annotations across mouse-related functions.
1 parent 8151961 commit e21b34f

File tree

1 file changed

+57
-49
lines changed

1 file changed

+57
-49
lines changed

botcity/core/bot.py

Lines changed: 57 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import subprocess
77
import time
88
import webbrowser
9-
from typing import Union, Tuple, Optional, List, Dict, Generator, Any
9+
from typing import Union, Tuple, Optional, List, Dict, Generator, Any, Literal
1010

1111
from numpy import ndarray
1212

@@ -761,7 +761,7 @@ def get_element_coords_centered(
761761
height: Optional[int] = None,
762762
matching: float = 0.9,
763763
best: bool = True,
764-
):
764+
) -> Union[Tuple[int, int], Tuple[None, None]]:
765765
"""
766766
Find an element defined by label on screen and returns its centered coordinates.
767767
@@ -807,7 +807,7 @@ def browse(self, url, location=0):
807807
# Mouse
808808
#######
809809

810-
def click_on(self, label):
810+
def click_on(self, label: str) -> None:
811811
"""
812812
Click on the element.
813813
@@ -819,7 +819,7 @@ def click_on(self, label):
819819
raise ValueError(f"Element not available. Cannot find {label}.")
820820
_mouse_click(self._mouse_controller, x, y)
821821

822-
def get_last_x(self):
822+
def get_last_x(self) -> int:
823823
"""
824824
Get the last X position for the mouse.
825825
@@ -828,7 +828,7 @@ def get_last_x(self):
828828
"""
829829
return self._mouse_controller.position[0]
830830

831-
def get_last_y(self):
831+
def get_last_y(self) -> int:
832832
"""
833833
Get the last Y position for the mouse.
834834
@@ -837,7 +837,7 @@ def get_last_y(self):
837837
"""
838838
return self._mouse_controller.position[1]
839839

840-
def mouse_move(self, x, y):
840+
def mouse_move(self, x: int, y: int) -> None:
841841
"""
842842
Move the mouse to the coordinate defined by x and y
843843
@@ -849,7 +849,7 @@ def mouse_move(self, x, y):
849849
self._mouse_controller.position = (x, y)
850850
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
851851

852-
def click_at(self, x, y):
852+
def click_at(self, x: int, y: int) -> None:
853853
"""
854854
Click at the coordinate defined by x and y
855855
@@ -862,12 +862,12 @@ def click_at(self, x, y):
862862
@only_if_element
863863
def click(
864864
self,
865-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
865+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
866866
*,
867-
clicks=1,
868-
interval_between_clicks=0,
869-
button="left",
870-
):
867+
clicks: int = 1,
868+
interval_between_clicks: int = 0,
869+
button: Literal["left", "right", "middle"] = "left",
870+
) -> None:
871871
"""
872872
Click on the last found element.
873873
@@ -886,14 +886,14 @@ def click(
886886
@only_if_element
887887
def click_relative(
888888
self,
889-
x,
890-
y,
891-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
889+
x: int,
890+
y: int,
891+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
892892
*,
893-
clicks=1,
894-
interval_between_clicks=0,
895-
button="left",
896-
):
893+
clicks: int = 1,
894+
interval_between_clicks: int = 0,
895+
button: Literal["left", "right", "middle"] = "left",
896+
) -> None:
897897
"""
898898
Click Relative on the last found element.
899899
@@ -913,7 +913,7 @@ def click_relative(
913913
self.sleep(wait_after)
914914

915915
@only_if_element
916-
def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
916+
def double_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None:
917917
"""
918918
Double Click on the last found element.
919919
@@ -925,11 +925,11 @@ def double_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
925925
@only_if_element
926926
def double_click_relative(
927927
self,
928-
x,
929-
y,
930-
interval_between_clicks=0,
931-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
932-
):
928+
x: int,
929+
y: int,
930+
interval_between_clicks: int = 0,
931+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
932+
) -> None:
933933
"""
934934
Double Click Relative on the last found element.
935935
@@ -949,7 +949,7 @@ def double_click_relative(
949949
)
950950

951951
@only_if_element
952-
def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
952+
def triple_click(self, wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION) -> None:
953953
"""
954954
Triple Click on the last found element.
955955
@@ -961,11 +961,11 @@ def triple_click(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION):
961961
@only_if_element
962962
def triple_click_relative(
963963
self,
964-
x,
965-
y,
966-
interval_between_clicks=0,
967-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
968-
):
964+
x: int,
965+
y: int,
966+
interval_between_clicks: int = 0,
967+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
968+
) -> None:
969969
"""
970970
Triple Click Relative on the last found element.
971971
@@ -985,8 +985,11 @@ def triple_click_relative(
985985
)
986986

987987
def mouse_down(
988-
self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left"
989-
):
988+
self,
989+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
990+
*,
991+
button: Literal["left", "right", "middle"] = "left",
992+
) -> None:
990993
"""
991994
Holds down the requested mouse button.
992995
@@ -998,7 +1001,12 @@ def mouse_down(
9981001
self._mouse_controller.press(mouse_button)
9991002
self.sleep(wait_after)
10001003

1001-
def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left"):
1004+
def mouse_up(
1005+
self,
1006+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
1007+
*,
1008+
button: Literal["left", "right", "middle"] = "left",
1009+
) -> None:
10021010
"""
10031011
Releases the requested mouse button.
10041012
@@ -1010,7 +1018,7 @@ def mouse_up(self, wait_after=config.DEFAULT_SLEEP_AFTER_ACTION, *, button="left
10101018
self._mouse_controller.release(mouse_button)
10111019
self.sleep(wait_after)
10121020

1013-
def scroll_down(self, clicks):
1021+
def scroll_down(self, clicks: int) -> None:
10141022
"""
10151023
Scroll Down n clicks
10161024
@@ -1020,7 +1028,7 @@ def scroll_down(self, clicks):
10201028
self._mouse_controller.scroll(0, -1 * clicks)
10211029
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10221030

1023-
def scroll_up(self, clicks):
1031+
def scroll_up(self, clicks: int) -> None:
10241032
"""
10251033
Scroll Up n clicks
10261034
@@ -1031,15 +1039,15 @@ def scroll_up(self, clicks):
10311039
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10321040

10331041
@only_if_element
1034-
def move(self):
1042+
def move(self) -> None:
10351043
"""
10361044
Move to the center position of last found item.
10371045
"""
10381046
x, y = self.state.center()
10391047
self._mouse_controller.position = (x, y)
10401048
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10411049

1042-
def move_relative(self, x, y):
1050+
def move_relative(self, x: int, y: int) -> None:
10431051
"""
10441052
Move the mouse relative to its current position.
10451053
@@ -1053,7 +1061,7 @@ def move_relative(self, x, y):
10531061
self._mouse_controller.position = (x, y)
10541062
self.sleep(config.DEFAULT_SLEEP_AFTER_ACTION)
10551063

1056-
def move_random(self, range_x, range_y):
1064+
def move_random(self, range_x: int, range_y: int) -> None:
10571065
"""
10581066
Move randomly along the given x, y range.
10591067
@@ -1070,11 +1078,11 @@ def move_random(self, range_x, range_y):
10701078
@only_if_element
10711079
def right_click(
10721080
self,
1073-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
1081+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
10741082
*,
1075-
clicks=1,
1076-
interval_between_clicks=0,
1077-
):
1083+
clicks: int = 1,
1084+
interval_between_clicks: int = 0,
1085+
) -> None:
10781086
"""
10791087
Right click on the last found element.
10801088
@@ -1094,7 +1102,7 @@ def right_click(
10941102
)
10951103
self.sleep(wait_after)
10961104

1097-
def right_click_at(self, x, y):
1105+
def right_click_at(self, x: int, y: int) -> None:
10981106
"""
10991107
Right click at the coordinate defined by x and y
11001108
@@ -1107,11 +1115,11 @@ def right_click_at(self, x, y):
11071115
@only_if_element
11081116
def right_click_relative(
11091117
self,
1110-
x,
1111-
y,
1112-
interval_between_clicks=0,
1113-
wait_after=config.DEFAULT_SLEEP_AFTER_ACTION,
1114-
):
1118+
x: int,
1119+
y: int,
1120+
interval_between_clicks: int = 0,
1121+
wait_after: int = config.DEFAULT_SLEEP_AFTER_ACTION,
1122+
) -> None:
11151123
"""
11161124
Right Click Relative on the last found element.
11171125

0 commit comments

Comments
 (0)