Skip to content

Commit 592d8e8

Browse files
[py] added type hinting support to common/actions libraries. (#12325)
[py] added additional typehints to actions
1 parent 2879d3e commit 592d8e8

6 files changed

Lines changed: 39 additions & 23 deletions

File tree

py/selenium/webdriver/common/actions/action_builder.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717

1818
from typing import List
19+
from typing import Optional
1920
from typing import Union
2021

2122
from selenium.webdriver.remote.command import Command
@@ -30,7 +31,14 @@
3031

3132

3233
class ActionBuilder:
33-
def __init__(self, driver, mouse=None, wheel=None, keyboard=None, duration=250) -> None:
34+
def __init__(
35+
self,
36+
driver,
37+
mouse: Optional[PointerInput] = None,
38+
wheel: Optional[WheelInput] = None,
39+
keyboard: Optional[KeyInput] = None,
40+
duration: int = 250,
41+
) -> None:
3442
if not mouse:
3543
mouse = PointerInput(interaction.POINTER_MOUSE, "mouse")
3644
if not keyboard:
@@ -43,7 +51,7 @@ def __init__(self, driver, mouse=None, wheel=None, keyboard=None, duration=250)
4351
self._wheel_action = WheelActions(wheel)
4452
self.driver = driver
4553

46-
def get_device_with(self, name) -> Union["WheelInput", "PointerInput", "KeyInput"]:
54+
def get_device_with(self, name: str) -> Union["WheelInput", "PointerInput", "KeyInput"]:
4755
return next(filter(lambda x: x == name, self.devices), None)
4856

4957
@property
@@ -66,17 +74,17 @@ def pointer_action(self) -> PointerActions:
6674
def wheel_action(self) -> WheelActions:
6775
return self._wheel_action
6876

69-
def add_key_input(self, name) -> KeyInput:
77+
def add_key_input(self, name: str) -> KeyInput:
7078
new_input = KeyInput(name)
7179
self._add_input(new_input)
7280
return new_input
7381

74-
def add_pointer_input(self, kind, name) -> PointerInput:
82+
def add_pointer_input(self, kind: str, name: str) -> PointerInput:
7583
new_input = PointerInput(kind, name)
7684
self._add_input(new_input)
7785
return new_input
7886

79-
def add_wheel_input(self, name) -> WheelInput:
87+
def add_wheel_input(self, name: str) -> WheelInput:
8088
new_input = WheelInput(name)
8189
self._add_input(new_input)
8290
return new_input
@@ -94,5 +102,5 @@ def clear_actions(self) -> None:
94102
"""Clears actions that are already stored on the remote end."""
95103
self.driver.execute(Command.W3C_CLEAR_ACTIONS)
96104

97-
def _add_input(self, new_input) -> None:
105+
def _add_input(self, new_input: Union[KeyInput, PointerInput, WheelInput]) -> None:
98106
self.devices.append(new_input)

py/selenium/webdriver/common/actions/input_device.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
# under the License.
1717

1818
import uuid
19+
from typing import Optional
1920

2021

2122
class InputDevice:
2223
"""Describes the input device being used for the action."""
2324

24-
def __init__(self, name=None):
25+
def __init__(self, name: Optional[str] = None):
2526
self.name = name or uuid.uuid4()
2627
self.actions = []
2728

@@ -32,5 +33,5 @@ def add_action(self, action):
3233
def clear_actions(self):
3334
self.actions = []
3435

35-
def create_pause(self, duration=0):
36+
def create_pause(self, duration: int = 0):
3637
pass

py/selenium/webdriver/common/actions/interaction.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17-
import typing
17+
from typing import Dict
18+
from typing import Union
1819

1920
KEY = "key"
2021
POINTER = "pointer"
@@ -32,7 +33,7 @@
3233
class Interaction:
3334
PAUSE = "pause"
3435

35-
def __init__(self, source) -> None:
36+
def __init__(self, source: str) -> None:
3637
self.source = source
3738

3839

@@ -41,5 +42,5 @@ def __init__(self, source, duration: float = 0) -> None:
4142
super().__init__(source)
4243
self.duration = duration
4344

44-
def encode(self) -> typing.Dict[str, typing.Union[str, int]]:
45+
def encode(self) -> Dict[str, Union[str, int]]:
4546
return {"type": self.PAUSE, "duration": int(self.duration * 1000)}

py/selenium/webdriver/common/actions/key_actions.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,42 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19+
from typing import Optional
20+
from typing import Union
21+
1922
from ..utils import keys_to_typing
2023
from .interaction import KEY
2124
from .interaction import Interaction
2225
from .key_input import KeyInput
26+
from .pointer_input import PointerInput
27+
from .wheel_input import WheelInput
2328

2429

2530
class KeyActions(Interaction):
26-
def __init__(self, source=None):
31+
def __init__(self, source: Optional[KeyInput, PointerInput, WheelInput] = None) -> None:
2732
if not source:
2833
source = KeyInput(KEY)
2934
self.source = source
3035
super().__init__(source)
3136

32-
def key_down(self, letter):
37+
def key_down(self, letter: str) -> KeyActions:
3338
return self._key_action("create_key_down", letter)
3439

35-
def key_up(self, letter):
40+
def key_up(self, letter: str) -> KeyActions:
3641
return self._key_action("create_key_up", letter)
3742

38-
def pause(self, duration=0):
43+
def pause(self, duration: int = 0) -> KeyActions:
3944
return self._key_action("create_pause", duration)
4045

41-
def send_keys(self, text):
46+
def send_keys(self, text: Union[str, list]) -> KeyActions:
4247
if not isinstance(text, list):
4348
text = keys_to_typing(text)
4449
for letter in text:
4550
self.key_down(letter)
4651
self.key_up(letter)
4752
return self
4853

49-
def _key_action(self, action, letter) -> KeyActions:
54+
def _key_action(self, action: str, letter) -> KeyActions:
5055
meth = getattr(self.source, action)
5156
meth(letter)
5257
return self

py/selenium/webdriver/common/actions/key_input.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
class KeyInput(InputDevice):
24-
def __init__(self, name) -> None:
24+
def __init__(self, name: str) -> None:
2525
super().__init__()
2626
self.name = name
2727
self.type = interaction.KEY

py/selenium/webdriver/common/actions/pointer_actions.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
from typing import Optional
1718

1819
from selenium.webdriver.remote.webelement import WebElement
1920

@@ -24,7 +25,7 @@
2425

2526

2627
class PointerActions(Interaction):
27-
def __init__(self, source=None, duration=250):
28+
def __init__(self, source: Optional[PointerInput] = None, duration: int = 250):
2829
"""
2930
Args:
3031
- source: PointerInput instance
@@ -165,17 +166,17 @@ def move_to_location(
165166
)
166167
return self
167168

168-
def click(self, element=None, button=MouseButton.LEFT):
169+
def click(self, element: Optional[WebElement] = None, button=MouseButton.LEFT):
169170
if element:
170171
self.move_to(element)
171172
self.pointer_down(button)
172173
self.pointer_up(button)
173174
return self
174175

175-
def context_click(self, element=None):
176+
def context_click(self, element: Optional[WebElement] = None):
176177
return self.click(element=element, button=MouseButton.RIGHT)
177178

178-
def click_and_hold(self, element=None, button=MouseButton.LEFT):
179+
def click_and_hold(self, element: Optional[WebElement] = None, button=MouseButton.LEFT):
179180
if element:
180181
self.move_to(element)
181182
self.pointer_down(button=button)
@@ -185,7 +186,7 @@ def release(self, button=MouseButton.LEFT):
185186
self.pointer_up(button=button)
186187
return self
187188

188-
def double_click(self, element=None):
189+
def double_click(self, element: Optional[WebElement] = None):
189190
if element:
190191
self.move_to(element)
191192
self.pointer_down(MouseButton.LEFT)

0 commit comments

Comments
 (0)