Skip to content

Commit 34a3aa2

Browse files
authored
[py] PEP 484 type hints for selenium.webdriver.common.actions.action_builder (#10108)
also method get_device_with was edited much more laconic new variant has the same execution time as old get_device_with method returns None (as old try/except) if no device was filtered try/except block is unnecessary anymore
1 parent 41a34df commit 34a3aa2

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from typing import Union, List
1819
from selenium.webdriver.remote.command import Command
1920
from . import interaction
2021
from .key_actions import KeyActions
@@ -26,7 +27,7 @@
2627

2728

2829
class ActionBuilder(object):
29-
def __init__(self, driver, mouse=None, wheel=None, keyboard=None, duration=250):
30+
def __init__(self, driver, mouse=None, wheel=None, keyboard=None, duration=250) -> None:
3031
if not mouse:
3132
mouse = PointerInput(interaction.POINTER_MOUSE, "mouse")
3233
if not keyboard:
@@ -39,49 +40,45 @@ def __init__(self, driver, mouse=None, wheel=None, keyboard=None, duration=250):
3940
self._wheel_action = WheelActions(wheel)
4041
self.driver = driver
4142

42-
def get_device_with(self, name):
43-
try:
44-
idx = self.devices.index(name)
45-
return self.devices[idx]
46-
except Exception:
47-
pass
43+
def get_device_with(self, name) -> Union["WheelInput","PointerInput","KeyInput"]:
44+
return next(filter(lambda x: x == name, self.devices), None)
4845

4946
@property
50-
def pointer_inputs(self):
47+
def pointer_inputs(self) -> List[PointerInput]:
5148
return [device for device in self.devices if device.type == interaction.POINTER]
5249

5350
@property
54-
def key_inputs(self):
51+
def key_inputs(self) -> List[KeyInput]:
5552
return [device for device in self.devices if device.type == interaction.KEY]
5653

5754
@property
58-
def key_action(self):
55+
def key_action(self) -> KeyActions:
5956
return self._key_action
6057

6158
@property
62-
def pointer_action(self):
59+
def pointer_action(self) -> PointerActions:
6360
return self._pointer_action
6461

6562
@property
66-
def wheel_action(self):
63+
def wheel_action(self) -> WheelActions:
6764
return self._wheel_action
6865

69-
def add_key_input(self, name):
66+
def add_key_input(self, name) -> KeyInput:
7067
new_input = KeyInput(name)
7168
self._add_input(new_input)
7269
return new_input
7370

74-
def add_pointer_input(self, kind, name):
71+
def add_pointer_input(self, kind, name) -> PointerInput:
7572
new_input = PointerInput(kind, name)
7673
self._add_input(new_input)
7774
return new_input
7875

79-
def add_wheel_input(self, kind, name):
76+
def add_wheel_input(self, kind, name) -> WheelInput:
8077
new_input = WheelInput(kind, name)
8178
self._add_input(new_input)
8279
return new_input
8380

84-
def perform(self):
81+
def perform(self) -> None:
8582
enc = {"actions": []}
8683
for device in self.devices:
8784
encoded = device.encode()
@@ -90,11 +87,11 @@ def perform(self):
9087
device.actions = []
9188
self.driver.execute(Command.W3C_ACTIONS, enc)
9289

93-
def clear_actions(self):
90+
def clear_actions(self) -> None:
9491
"""
9592
Clears actions that are already stored on the remote end
9693
"""
9794
self.driver.execute(Command.W3C_CLEAR_ACTIONS)
9895

99-
def _add_input(self, input):
96+
def _add_input(self, input) -> None:
10097
self.devices.append(input)

0 commit comments

Comments
 (0)