Skip to content

Commit 31d2469

Browse files
chore: Use proper type declarations for methods returning self instances
1 parent e4b40ae commit 31d2469

File tree

18 files changed

+102
-150
lines changed

18 files changed

+102
-150
lines changed

appium/webdriver/extensions/action_helpers.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, List, Optional, Tuple, cast
15+
from typing import TYPE_CHECKING, List, Optional, Self, Tuple, cast
1616

1717
from selenium.webdriver.common.action_chains import ActionChains
1818
from selenium.webdriver.common.actions import interaction
@@ -23,11 +23,12 @@
2323
from appium.webdriver.webelement import WebElement
2424

2525
if TYPE_CHECKING:
26+
# noinspection PyUnresolvedReferences
2627
from appium.webdriver.webdriver import WebDriver
2728

2829

2930
class ActionHelpers:
30-
def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> 'WebDriver':
31+
def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Optional[int] = None) -> Self:
3132
"""Scrolls from one element to another
3233
3334
Args:
@@ -48,7 +49,7 @@ def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Op
4849

4950
touch_input = PointerInput(interaction.POINTER_TOUCH, 'touch')
5051

51-
actions = ActionChains(self)
52+
actions = ActionChains(cast('WebDriver', self))
5253
actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
5354

5455
# https://github.com/SeleniumHQ/selenium/blob/3c82c868d4f2a7600223a1b3817301d0b04d28e4/py/selenium/webdriver/common/actions/pointer_actions.py#L83
@@ -59,11 +60,9 @@ def scroll(self, origin_el: WebElement, destination_el: WebElement, duration: Op
5960
actions.w3c_actions.pointer_action.move_to(destination_el)
6061
actions.w3c_actions.pointer_action.release()
6162
actions.perform()
62-
return cast('WebDriver', self)
63+
return self
6364

64-
def drag_and_drop(
65-
self, origin_el: WebElement, destination_el: WebElement, pause: Optional[float] = None
66-
) -> 'WebDriver':
65+
def drag_and_drop(self, origin_el: WebElement, destination_el: WebElement, pause: Optional[float] = None) -> Self:
6766
"""Drag the origin element to the destination element
6867
6968
Args:
@@ -74,17 +73,17 @@ def drag_and_drop(
7473
Returns:
7574
Union['WebDriver', 'ActionHelpers']: Self instance
7675
"""
77-
actions = ActionChains(self)
76+
actions = ActionChains(cast('WebDriver', self))
7877
# 'mouse' pointer action
7978
actions.w3c_actions.pointer_action.click_and_hold(origin_el)
8079
if pause is not None and pause > 0:
8180
actions.w3c_actions.pointer_action.pause(pause)
8281
actions.w3c_actions.pointer_action.move_to(destination_el)
8382
actions.w3c_actions.pointer_action.release()
8483
actions.perform()
85-
return cast('WebDriver', self)
84+
return self
8685

87-
def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None) -> 'WebDriver':
86+
def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None) -> Self:
8887
"""Taps on an particular place with up to five fingers, holding for a
8988
certain time
9089
@@ -100,7 +99,7 @@ def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None)
10099
Union['WebDriver', 'ActionHelpers']: Self instance
101100
"""
102101
if len(positions) == 1:
103-
actions = ActionChains(self)
102+
actions = ActionChains(cast('WebDriver', self))
104103
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, 'touch'))
105104
x = positions[0][0]
106105
y = positions[0][1]
@@ -114,7 +113,7 @@ def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None)
114113
actions.perform()
115114
else:
116115
finger = 0
117-
actions = ActionChains(self)
116+
actions = ActionChains(cast('WebDriver', self))
118117
actions.w3c_actions.devices = []
119118

120119
for position in positions:
@@ -132,9 +131,9 @@ def tap(self, positions: List[Tuple[int, int]], duration: Optional[int] = None)
132131
new_input.create_pause(0.1)
133132
new_input.create_pointer_up(MouseButton.LEFT)
134133
actions.perform()
135-
return cast('WebDriver', self)
134+
return self
136135

137-
def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> 'WebDriver':
136+
def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: int = 0) -> Self:
138137
"""Swipe from one point to another point, for an optional duration.
139138
140139
Args:
@@ -152,7 +151,7 @@ def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: in
152151
"""
153152
touch_input = PointerInput(interaction.POINTER_TOUCH, 'touch')
154153

155-
actions = ActionChains(self)
154+
actions = ActionChains(cast('WebDriver', self))
156155
actions.w3c_actions = ActionBuilder(self, mouse=touch_input)
157156
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
158157
actions.w3c_actions.pointer_action.pointer_down()
@@ -161,9 +160,9 @@ def swipe(self, start_x: int, start_y: int, end_x: int, end_y: int, duration: in
161160
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
162161
actions.w3c_actions.pointer_action.release()
163162
actions.perform()
164-
return cast('WebDriver', self)
163+
return self
165164

166-
def flick(self, start_x: int, start_y: int, end_x: int, end_y: int) -> 'WebDriver':
165+
def flick(self, start_x: int, start_y: int, end_x: int, end_y: int) -> Self:
167166
"""Flick from one point to another point.
168167
169168
Args:
@@ -178,11 +177,11 @@ def flick(self, start_x: int, start_y: int, end_x: int, end_y: int) -> 'WebDrive
178177
Returns:
179178
Union['WebDriver', 'ActionHelpers']: Self instance
180179
"""
181-
actions = ActionChains(self)
180+
actions = ActionChains(cast('WebDriver', self))
182181
actions.w3c_actions = ActionBuilder(self, mouse=PointerInput(interaction.POINTER_TOUCH, 'touch'))
183182
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
184183
actions.w3c_actions.pointer_action.pointer_down()
185184
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
186185
actions.w3c_actions.pointer_action.release()
187186
actions.perform()
188-
return cast('WebDriver', self)
187+
return self

appium/webdriver/extensions/android/common.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import TYPE_CHECKING, cast
14+
from typing import Self
1515

1616
from selenium.common.exceptions import UnknownMethodException
1717

@@ -20,12 +20,9 @@
2020
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2121
from appium.webdriver.mobilecommand import MobileCommand as Command
2222

23-
if TYPE_CHECKING:
24-
from appium.webdriver.webdriver import WebDriver
25-
2623

2724
class Common(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
28-
def open_notifications(self) -> 'WebDriver':
25+
def open_notifications(self) -> Self:
2926
"""Open notification shade in Android (API Level 18 and above)
3027
3128
Returns:
@@ -37,7 +34,7 @@ def open_notifications(self) -> 'WebDriver':
3734
except UnknownMethodException:
3835
# TODO: Remove the fallback
3936
self.mark_extension_absence(ext_name).execute(Command.OPEN_NOTIFICATIONS, {})
40-
return cast('WebDriver', self)
37+
return self
4138

4239
@property
4340
def current_package(self) -> str:

appium/webdriver/extensions/android/gsm.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, cast
15+
from typing import Self
1616

1717
from selenium.common.exceptions import UnknownMethodException
1818

@@ -23,9 +23,6 @@
2323
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2424
from appium.webdriver.mobilecommand import MobileCommand as Command
2525

26-
if TYPE_CHECKING:
27-
from appium.webdriver.webdriver import WebDriver
28-
2926

3027
class GsmCallActions:
3128
CALL = 'call'
@@ -53,7 +50,7 @@ class GsmVoiceState:
5350

5451

5552
class Gsm(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
56-
def make_gsm_call(self, phone_number: str, action: str) -> 'WebDriver':
53+
def make_gsm_call(self, phone_number: str, action: str) -> Self:
5754
"""Make GSM call (Emulator only)
5855
5956
Android only.
@@ -82,9 +79,9 @@ def make_gsm_call(self, phone_number: str, action: str) -> 'WebDriver':
8279
except UnknownMethodException:
8380
# TODO: Remove the fallback
8481
self.mark_extension_absence(ext_name).execute(Command.MAKE_GSM_CALL, args)
85-
return cast('WebDriver', self)
82+
return self
8683

87-
def set_gsm_signal(self, strength: int) -> 'WebDriver':
84+
def set_gsm_signal(self, strength: int) -> Self:
8885
"""Set GSM signal strength (Emulator only)
8986
9087
Android only.
@@ -113,9 +110,9 @@ def set_gsm_signal(self, strength: int) -> 'WebDriver':
113110
self.mark_extension_absence(ext_name).execute(
114111
Command.SET_GSM_SIGNAL, {'signalStrength': strength, 'signalStrengh': strength}
115112
)
116-
return cast('WebDriver', self)
113+
return self
117114

118-
def set_gsm_voice(self, state: str) -> 'WebDriver':
115+
def set_gsm_voice(self, state: str) -> Self:
119116
"""Set GSM voice state (Emulator only)
120117
121118
Android only.
@@ -143,7 +140,7 @@ def set_gsm_voice(self, state: str) -> 'WebDriver':
143140
except UnknownMethodException:
144141
# TODO: Remove the fallback
145142
self.mark_extension_absence(ext_name).execute(Command.SET_GSM_VOICE, args)
146-
return cast('WebDriver', self)
143+
return self
147144

148145
def _add_commands(self) -> None:
149146
# noinspection PyProtectedMember,PyUnresolvedReferences

appium/webdriver/extensions/android/network.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, cast
15+
from typing import Self
1616

1717
from selenium.common.exceptions import UnknownMethodException
1818

@@ -23,9 +23,6 @@
2323
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2424
from appium.webdriver.mobilecommand import MobileCommand as Command
2525

26-
if TYPE_CHECKING:
27-
from appium.webdriver.webdriver import WebDriver
28-
2926

3027
class NetSpeed:
3128
GSM = 'gsm' # GSM/CSD (up: 14.4(kbps), down: 14.4(kbps))
@@ -114,7 +111,7 @@ def set_network_connection(self, connection_type: int) -> int:
114111
Command.SET_NETWORK_CONNECTION, {'parameters': {'type': connection_type}}
115112
)['value']
116113

117-
def toggle_wifi(self) -> 'WebDriver':
114+
def toggle_wifi(self) -> Self:
118115
"""Toggle the wifi on the device, Android only.
119116
This API only works reliably on emulators (any version) and real devices
120117
since API level 31.
@@ -129,9 +126,9 @@ def toggle_wifi(self) -> 'WebDriver':
129126
)
130127
except UnknownMethodException:
131128
self.mark_extension_absence(ext_name).execute(Command.TOGGLE_WIFI, {})
132-
return cast('WebDriver', self)
129+
return self
133130

134-
def set_network_speed(self, speed_type: str) -> 'WebDriver':
131+
def set_network_speed(self, speed_type: str) -> Self:
135132
"""Set the network speed emulation.
136133
137134
Android Emulator only.
@@ -158,7 +155,7 @@ def set_network_speed(self, speed_type: str) -> 'WebDriver':
158155
except UnknownMethodException:
159156
# TODO: Remove the fallback
160157
self.mark_extension_absence(ext_name).execute(Command.SET_NETWORK_SPEED, {'netspeed': speed_type})
161-
return cast('WebDriver', self)
158+
return self
162159

163160
def _add_commands(self) -> None:
164161
# noinspection PyProtectedMember,PyUnresolvedReferences

appium/webdriver/extensions/android/power.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, cast
15+
from typing import Self
1616

1717
from selenium.common.exceptions import UnknownMethodException
1818

@@ -21,14 +21,11 @@
2121
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2222
from appium.webdriver.mobilecommand import MobileCommand as Command
2323

24-
if TYPE_CHECKING:
25-
from appium.webdriver.webdriver import WebDriver
26-
2724

2825
class Power(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
2926
AC_OFF, AC_ON = 'off', 'on'
3027

31-
def set_power_capacity(self, percent: int) -> 'WebDriver':
28+
def set_power_capacity(self, percent: int) -> Self:
3229
"""Emulate power capacity change on the connected emulator.
3330
3431
Android only.
@@ -49,9 +46,9 @@ def set_power_capacity(self, percent: int) -> 'WebDriver':
4946
except UnknownMethodException:
5047
# TODO: Remove the fallback
5148
self.mark_extension_absence(ext_name).execute(Command.SET_POWER_CAPACITY, args)
52-
return cast('WebDriver', self)
49+
return self
5350

54-
def set_power_ac(self, ac_state: str) -> 'WebDriver':
51+
def set_power_ac(self, ac_state: str) -> Self:
5552
"""Emulate power state change on the connected emulator.
5653
5754
Android only.
@@ -73,7 +70,7 @@ def set_power_ac(self, ac_state: str) -> 'WebDriver':
7370
except UnknownMethodException:
7471
# TODO: Remove the fallback
7572
self.mark_extension_absence(ext_name).execute(Command.SET_POWER_AC, args)
76-
return cast('WebDriver', self)
73+
return self
7774

7875
def _add_commands(self) -> None:
7976
# noinspection PyProtectedMember,PyUnresolvedReferences

appium/webdriver/extensions/android/sms.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import TYPE_CHECKING, cast
15+
from typing import Self
1616

1717
from selenium.common.exceptions import UnknownMethodException
1818

@@ -21,12 +21,9 @@
2121
from appium.protocols.webdriver.can_remember_extension_presence import CanRememberExtensionPresence
2222
from appium.webdriver.mobilecommand import MobileCommand as Command
2323

24-
if TYPE_CHECKING:
25-
from appium.webdriver.webdriver import WebDriver
26-
2724

2825
class Sms(CanExecuteCommands, CanExecuteScripts, CanRememberExtensionPresence):
29-
def send_sms(self, phone_number: str, message: str) -> 'WebDriver':
26+
def send_sms(self, phone_number: str, message: str) -> Self:
3027
"""Emulate send SMS event on the connected emulator.
3128
3229
Android only.
@@ -48,7 +45,7 @@ def send_sms(self, phone_number: str, message: str) -> 'WebDriver':
4845
except UnknownMethodException:
4946
# TODO: Remove the fallback
5047
self.mark_extension_absence(ext_name).execute(Command.SEND_SMS, args)
51-
return cast('WebDriver', self)
48+
return self
5249

5350
def _add_commands(self) -> None:
5451
# noinspection PyProtectedMember,PyUnresolvedReferences

0 commit comments

Comments
 (0)