Skip to content

Commit ead278b

Browse files
committed
WIP: Added more type hints - needs review
1 parent 9c17c61 commit ead278b

File tree

7 files changed

+78
-35
lines changed

7 files changed

+78
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def add_key_input(self, name: str) -> KeyInput:
103103
self._add_input(new_input)
104104
return new_input
105105

106-
def add_pointer_input(self, kind: str, name: str) -> PointerInput:
106+
def add_pointer_input(self, kind: interaction.POINTER_KINDS_LITERAL, name: str) -> PointerInput:
107107
"""Add a new pointer input device to the action builder.
108108
109109
Parameters:

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import Any
2020
from typing import List
2121
from typing import Optional
22+
from typing import Union
2223

2324

2425
class InputDevice:
@@ -35,5 +36,5 @@ def add_action(self, action: Any) -> None:
3536
def clear_actions(self) -> None:
3637
self.actions = []
3738

38-
def create_pause(self, duration: float = 0) -> None:
39+
def create_pause(self, pause_duration: Union[float, int] = 0) -> None:
3940
pass

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,39 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717
from typing import Dict
18+
from typing import Literal
1819
from typing import Union
1920

21+
from .key_input import KeyInput
22+
from .pointer_input import PointerInput
23+
from .wheel_input import WheelInput
24+
2025
KEY = "key"
2126
POINTER = "pointer"
2227
NONE = "none"
2328
WHEEL = "wheel"
2429
SOURCE_TYPES = {KEY, POINTER, NONE}
30+
SOURCE_TYPES_LITERAL = Literal['key', 'pointer', 'none', 'wheel']
2531

2632
POINTER_MOUSE = "mouse"
2733
POINTER_TOUCH = "touch"
2834
POINTER_PEN = "pen"
29-
3035
POINTER_KINDS = {POINTER_MOUSE, POINTER_TOUCH, POINTER_PEN}
36+
POINTER_KINDS_LITERAL = Literal['mouse', 'touch', 'pen']
3137

3238

3339
class Interaction:
3440
PAUSE = "pause"
3541

36-
def __init__(self, source: str) -> None:
42+
def __init__(self, source: Union[KeyInput, PointerInput, WheelInput]) -> None:
3743
self.source = source
3844

3945

4046
class Pause(Interaction):
41-
def __init__(self, source, duration: float = 0) -> None:
47+
def __init__(self, source: Union[KeyInput, PointerInput, WheelInput], duration: Union[int, float] = 0) -> None:
48+
"""
49+
:Args:
50+
- duration: duration of the pause in seconds"""
4251
super().__init__(source)
4352
self.duration = duration
4453

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,42 @@
1414
# KIND, either express or implied. See the License for the
1515
# specific language governing permissions and limitations
1616
# under the License.
17+
18+
from typing import Any
19+
from typing import Literal
20+
from typing import Union
21+
1722
from . import interaction
1823
from .input_device import InputDevice
1924
from .interaction import Interaction
2025
from .interaction import Pause
26+
from .interaction import SOURCE_TYPES_LITERAL
2127

2228

2329
class KeyInput(InputDevice):
2430
def __init__(self, name: str) -> None:
2531
super().__init__()
2632
self.name = name
27-
self.type = interaction.KEY
33+
self.type: SOURCE_TYPES_LITERAL = interaction.KEY
2834

29-
def encode(self) -> dict:
35+
def encode(self) -> dict[str, Any]:
3036
return {"type": self.type, "id": self.name, "actions": [acts.encode() for acts in self.actions]}
3137

32-
def create_key_down(self, key) -> None:
38+
def create_key_down(self, key: str) -> None:
3339
self.add_action(TypingInteraction(self, "keyDown", key))
3440

35-
def create_key_up(self, key) -> None:
41+
def create_key_up(self, key: str) -> None:
3642
self.add_action(TypingInteraction(self, "keyUp", key))
3743

38-
def create_pause(self, pause_duration: float = 0) -> None:
44+
def create_pause(self, pause_duration: Union[float, int] = 0) -> None:
3945
self.add_action(Pause(self, pause_duration))
4046

4147

4248
class TypingInteraction(Interaction):
43-
def __init__(self, source, type_, key) -> None:
49+
def __init__(self, source: str, type_: Literal["keyUp", "keyDown"], key: str) -> None:
4450
super().__init__(source)
4551
self.type = type_
4652
self.key = key
4753

48-
def encode(self) -> dict:
54+
def encode(self) -> dict[str, str]:
4955
return {"type": self.type, "value": self.key}

py/selenium/webdriver/common/actions/pointer_input.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,51 +26,52 @@
2626
from .input_device import InputDevice
2727
from .interaction import POINTER
2828
from .interaction import POINTER_KINDS
29+
from .interaction import POINTER_KINDS_LITERAL, SOURCE_TYPES_LITERAL
2930

3031

3132
class PointerInput(InputDevice):
3233
DEFAULT_MOVE_DURATION = 250
3334

34-
def __init__(self, kind, name):
35+
def __init__(self, kind: POINTER_KINDS_LITERAL, name: str) -> None:
3536
super().__init__()
3637
if kind not in POINTER_KINDS:
3738
raise InvalidArgumentException(f"Invalid PointerInput kind '{kind}'")
38-
self.type = POINTER
39-
self.kind = kind
39+
self.type: SOURCE_TYPES_LITERAL = POINTER
40+
self.kind: POINTER_KINDS_LITERAL = kind
4041
self.name = name
4142

4243
def create_pointer_move(
4344
self,
44-
duration=DEFAULT_MOVE_DURATION,
45-
x: float = 0,
46-
y: float = 0,
47-
origin: Optional[WebElement] = None,
45+
duration: Union[int, float] = DEFAULT_MOVE_DURATION,
46+
x: Union[int, float] = 0,
47+
y: Union[int, float] = 0,
48+
origin: Union[WebElement, str, None] = None,
4849
**kwargs,
49-
):
50+
) -> None:
5051
action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs}
5152
if isinstance(origin, WebElement):
5253
action["origin"] = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
5354
elif origin is not None:
5455
action["origin"] = origin
5556
self.add_action(self._convert_keys(action))
5657

57-
def create_pointer_down(self, **kwargs):
58+
def create_pointer_down(self, **kwargs) -> None:
5859
data = {"type": "pointerDown", "duration": 0, **kwargs}
5960
self.add_action(self._convert_keys(data))
6061

61-
def create_pointer_up(self, button):
62+
def create_pointer_up(self, button) -> None:
6263
self.add_action({"type": "pointerUp", "duration": 0, "button": button})
6364

64-
def create_pointer_cancel(self):
65+
def create_pointer_cancel(self) -> None:
6566
self.add_action({"type": "pointerCancel"})
6667

6768
def create_pause(self, pause_duration: Union[int, float] = 0) -> None:
6869
self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})
6970

70-
def encode(self):
71+
def encode(self) -> Dict[str, Any]:
7172
return {"type": self.type, "parameters": {"pointerType": self.kind}, "id": self.name, "actions": self.actions}
7273

73-
def _convert_keys(self, actions: Dict[str, Any]):
74+
def _convert_keys(self, actions: Dict[str, Any]) -> Dict[str, Any]:
7475
out = {}
7576
for k, v in actions.items():
7677
if v is None:

py/selenium/webdriver/common/actions/wheel_actions.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,34 @@
1616
# under the License.
1717

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

2021
from .interaction import Interaction
2122
from .wheel_input import WheelInput
2223

2324

2425
class WheelActions(Interaction):
25-
def __init__(self, source: Optional[WheelInput] = None):
26-
if not source:
26+
def __init__(self, source: Optional[WheelInput] = None) -> None:
27+
if source is None:
2728
source = WheelInput("wheel")
2829
super().__init__(source)
2930

30-
def pause(self, duration: float = 0):
31+
def pause(self, duration: Union[float, int] = 0) -> "WheelActions":
3132
self.source.create_pause(duration)
3233
return self
3334

34-
def scroll(self, x=0, y=0, delta_x=0, delta_y=0, duration=0, origin="viewport"):
35+
def scroll(
36+
self,
37+
x: int = 0,
38+
y: int = 0,
39+
delta_x: int = 0,
40+
delta_y: int = 0,
41+
duration: Union[float, int] = 0,
42+
origin: str = "viewport",
43+
) -> "WheelActions":
44+
"""
45+
:Args:
46+
- duration: The duration of the scroll, in seconds.
47+
"""
3548
self.source.create_scroll(x, y, delta_x, delta_y, duration, origin)
3649
return self

py/selenium/webdriver/common/actions/wheel_input.py

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

1922
from selenium.webdriver.remote.webelement import WebElement
@@ -29,11 +32,11 @@ def __init__(self, origin: Union[str, WebElement], x_offset: int, y_offset: int)
2932
self._y_offset = y_offset
3033

3134
@classmethod
32-
def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0):
35+
def from_element(cls, element: WebElement, x_offset: int = 0, y_offset: int = 0) -> "ScrollOrigin":
3336
return cls(element, x_offset, y_offset)
3437

3538
@classmethod
36-
def from_viewport(cls, x_offset: int = 0, y_offset: int = 0):
39+
def from_viewport(cls, x_offset: int = 0, y_offset: int = 0) -> "ScrollOrigin":
3740
return cls("viewport", x_offset, y_offset)
3841

3942
@property
@@ -50,15 +53,21 @@ def y_offset(self) -> int:
5053

5154

5255
class WheelInput(InputDevice):
53-
def __init__(self, name) -> None:
56+
def __init__(self, name: str) -> None:
5457
super().__init__(name=name)
5558
self.name = name
56-
self.type = interaction.WHEEL
59+
self.type: interaction.SOURCE_TYPES_LITERAL = interaction.WHEEL
5760

58-
def encode(self) -> dict:
61+
def encode(self) -> dict[str, Any]:
5962
return {"type": self.type, "id": self.name, "actions": self.actions}
6063

61-
def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: int, origin) -> None:
64+
def create_scroll(
65+
self, x: int, y: int, delta_x: int, delta_y: int, duration: Union[float, int], origin: Union[str, WebElement, Dict[str, str]]
66+
) -> None:
67+
"""
68+
:Args:
69+
- duration: The duration to pause for after the scroll, in seconds.
70+
"""
6271
if isinstance(origin, WebElement):
6372
origin = {"element-6066-11e4-a52e-4f735466cecf": origin.id}
6473
self.add_action(
@@ -74,4 +83,8 @@ def create_scroll(self, x: int, y: int, delta_x: int, delta_y: int, duration: in
7483
)
7584

7685
def create_pause(self, pause_duration: Union[int, float] = 0) -> None:
86+
"""
87+
:Args:
88+
- pause_duration: duration of the pause in seconds
89+
"""
7790
self.add_action({"type": "pause", "duration": int(pause_duration * 1000)})

0 commit comments

Comments
 (0)