Skip to content

Commit 8b4e370

Browse files
[py] Add python type hinting
1 parent 147334f commit 8b4e370

19 files changed

+223
-199
lines changed

py/selenium/webdriver/chrome/options.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@
2222
class Options(ChromiumOptions):
2323

2424
@property
25-
def default_capabilities(self):
25+
def default_capabilities(self) -> dict:
2626
return DesiredCapabilities.CHROME.copy()

py/selenium/webdriver/chrome/service.py

+3-2
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 List
1819
from selenium.webdriver.chromium import service
1920

2021

@@ -23,8 +24,8 @@ class Service(service.ChromiumService):
2324
Object that manages the starting and stopping of the ChromeDriver
2425
"""
2526

26-
def __init__(self, executable_path, port=0, service_args=None,
27-
log_path=None, env=None):
27+
def __init__(self, executable_path: str, port: int = 0, service_args: List[str] = None,
28+
log_path: str = None, env: str = None):
2829
"""
2930
Creates a new instance of the Service
3031

py/selenium/webdriver/chrome/webdriver.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ class WebDriver(ChromiumDriver):
3333
"""
3434

3535
def __init__(self, executable_path="chromedriver", port=DEFAULT_PORT,
36-
options=None, service_args=None,
36+
options: Options = None, service_args=None,
3737
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
38-
chrome_options=None, service=None, keep_alive=True):
38+
chrome_options=None, service: Service = None, keep_alive=True):
3939
"""
4040
Creates a new instance of the chrome driver.
4141
Starts the service and then creates new instance of chrome driver.
@@ -66,5 +66,5 @@ def __init__(self, executable_path="chromedriver", port=DEFAULT_PORT,
6666
service_args, desired_capabilities,
6767
service_log_path, service, keep_alive)
6868

69-
def create_options(self):
69+
def create_options(self) -> Options:
7070
return Options()

py/selenium/webdriver/chromium/options.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import base64
1919
import os
20+
from typing import List, NoReturn, Union
2021

2122
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2223
from selenium.webdriver.common.options import ArgOptions
@@ -34,14 +35,14 @@ def __init__(self):
3435
self._debugger_address = None
3536

3637
@property
37-
def binary_location(self):
38+
def binary_location(self) -> str:
3839
"""
3940
:Returns: The location of the binary, otherwise an empty string
4041
"""
4142
return self._binary_location
4243

4344
@binary_location.setter
44-
def binary_location(self, value):
45+
def binary_location(self, value: str):
4546
"""
4647
Allows you to set where the chromium binary lives
4748
:Args:
@@ -50,14 +51,14 @@ def binary_location(self, value):
5051
self._binary_location = value
5152

5253
@property
53-
def debugger_address(self):
54+
def debugger_address(self: str):
5455
"""
5556
:Returns: The address of the remote devtools instance
5657
"""
5758
return self._debugger_address
5859

5960
@debugger_address.setter
60-
def debugger_address(self, value):
61+
def debugger_address(self, value: str):
6162
"""
6263
Allows you to set the address of the remote devtools instance
6364
that the ChromeDriver instance will try to connect to during an
@@ -68,7 +69,7 @@ def debugger_address(self, value):
6869
self._debugger_address = value
6970

7071
@property
71-
def extensions(self):
72+
def extensions(self) -> List[str]:
7273
"""
7374
:Returns: A list of encoded extensions that will be loaded
7475
"""
@@ -83,7 +84,7 @@ def extensions(self):
8384
file_.close()
8485
return encoded_extensions + self._extensions
8586

86-
def add_extension(self, extension):
87+
def add_extension(self, extension: str) -> NoReturn:
8788
"""
8889
Adds the path to the extension to a list that will be used to extract it
8990
to the ChromeDriver
@@ -100,7 +101,7 @@ def add_extension(self, extension):
100101
else:
101102
raise ValueError("argument can not be null")
102103

103-
def add_encoded_extension(self, extension):
104+
def add_encoded_extension(self, extension: str) -> NoReturn:
104105
"""
105106
Adds Base64 encoded string with extension data to a list that will be used to extract it
106107
to the ChromeDriver
@@ -114,13 +115,13 @@ def add_encoded_extension(self, extension):
114115
raise ValueError("argument can not be null")
115116

116117
@property
117-
def experimental_options(self):
118+
def experimental_options(self) -> dict:
118119
"""
119120
:Returns: A dictionary of experimental options for chromium
120121
"""
121122
return self._experimental_options
122123

123-
def add_experimental_option(self, name, value):
124+
def add_experimental_option(self, name: str, value: Union[str, int]):
124125
"""
125126
Adds an experimental option which is passed to chromium.
126127
@@ -131,14 +132,14 @@ def add_experimental_option(self, name, value):
131132
self._experimental_options[name] = value
132133

133134
@property
134-
def headless(self):
135+
def headless(self) -> bool:
135136
"""
136137
:Returns: True if the headless argument is set, else False
137138
"""
138139
return '--headless' in self._arguments
139140

140141
@headless.setter
141-
def headless(self, value):
142+
def headless(self, value: bool):
142143
"""
143144
Sets the headless argument
144145
:Args:
@@ -151,17 +152,17 @@ def headless(self, value):
151152
self._arguments = list(set(self._arguments) - args)
152153

153154
@property
154-
def page_load_strategy(self):
155+
def page_load_strategy(self) -> str:
155156
return self._caps["pageLoadStrategy"]
156157

157158
@page_load_strategy.setter
158-
def page_load_strategy(self, strategy):
159+
def page_load_strategy(self, strategy: str):
159160
if strategy in ["normal", "eager", "none"]:
160161
self.set_capability("pageLoadStrategy", strategy)
161162
else:
162163
raise ValueError("Strategy can only be one of the following: normal, eager, none")
163164

164-
def to_capabilities(self):
165+
def to_capabilities(self) -> dict:
165166
"""
166167
Creates a capabilities with all the options that have been set
167168
:Returns: A dictionary with everything
@@ -180,5 +181,5 @@ def to_capabilities(self):
180181
return caps
181182

182183
@property
183-
def default_capabilities(self):
184+
def default_capabilities(self) -> dict:
184185
return DesiredCapabilities.CHROME.copy()

py/selenium/webdriver/chromium/service.py

+4-3
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 List
1819
from selenium.webdriver.common import service
1920

2021

@@ -23,8 +24,8 @@ class ChromiumService(service.Service):
2324
Object that manages the starting and stopping the WebDriver instance of the ChromiumDriver
2425
"""
2526

26-
def __init__(self, executable_path, port=0, service_args=None,
27-
log_path=None, env=None, start_error_message=None):
27+
def __init__(self, executable_path: str, port: int = 0, service_args: List[str] = None,
28+
log_path: str = None, env: str = None, start_error_message: str = None):
2829
"""
2930
Creates a new instance of the Service
3031
@@ -43,5 +44,5 @@ def __init__(self, executable_path, port=0, service_args=None,
4344

4445
service.Service.__init__(self, executable_path, port=port, env=env, start_error_message=start_error_message)
4546

46-
def command_line_args(self):
47+
def command_line_args(self) -> List[str]:
4748
return ["--port=%d" % self.port] + self.service_args

py/selenium/webdriver/chromium/webdriver.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from typing import NoReturn
19+
from selenium.webdriver.edge.options import Options
20+
from selenium.webdriver.edge.service import Service
1821
import warnings
1922

2023
from selenium.webdriver.chromium.remote_connection import ChromiumRemoteConnection
@@ -30,9 +33,9 @@ class ChromiumDriver(RemoteWebDriver):
3033
"""
3134

3235
def __init__(self, browser_name, vendor_prefix,
33-
port=DEFAULT_PORT, options=None, service_args=None,
36+
port=DEFAULT_PORT, options: Options = None, service_args=None,
3437
desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
35-
service=None, keep_alive=True):
38+
service: Service = None, keep_alive=True):
3639
"""
3740
Creates a new WebDriver instance of the ChromiumDriver.
3841
Starts the service and then creates new WebDriver instance of ChromiumDriver.
@@ -108,7 +111,7 @@ def get_network_conditions(self):
108111
"""
109112
return self.execute("getNetworkConditions")['value']
110113

111-
def set_network_conditions(self, **network_conditions):
114+
def set_network_conditions(self, **network_conditions) -> NoReturn:
112115
"""
113116
Sets Chromium network emulation settings.
114117
@@ -130,7 +133,7 @@ def set_network_conditions(self, **network_conditions):
130133
'network_conditions': network_conditions
131134
})
132135

133-
def execute_cdp_cmd(self, cmd, cmd_args):
136+
def execute_cdp_cmd(self, cmd: str, cmd_args: dict):
134137
"""
135138
Execute Chrome Devtools Protocol command and get returned result
136139
The command and command args should follow chrome devtools protocol domains/commands, refer to link
@@ -149,7 +152,7 @@ def execute_cdp_cmd(self, cmd, cmd_args):
149152
"""
150153
return self.execute("executeCdpCommand", {'cmd': cmd, 'params': cmd_args})['value']
151154

152-
def get_sinks(self):
155+
def get_sinks(self) -> list:
153156
"""
154157
:Returns: A list of sinks avaliable for Cast.
155158
"""
@@ -161,7 +164,7 @@ def get_issue_message(self):
161164
"""
162165
return self.execute('getIssueMessage')['value']
163166

164-
def set_sink_to_use(self, sink_name):
167+
def set_sink_to_use(self, sink_name: str) -> str:
165168
"""
166169
Sets a specific sink, using its name, as a Cast session receiver target.
167170
@@ -170,7 +173,7 @@ def set_sink_to_use(self, sink_name):
170173
"""
171174
return self.execute('setSinkToUse', {'sinkName': sink_name})
172175

173-
def start_tab_mirroring(self, sink_name):
176+
def start_tab_mirroring(self, sink_name: str) -> str:
174177
"""
175178
Starts a tab mirroring session on a specific receiver target.
176179
@@ -179,7 +182,7 @@ def start_tab_mirroring(self, sink_name):
179182
"""
180183
return self.execute('startTabMirroring', {'sinkName': sink_name})
181184

182-
def stop_casting(self, sink_name):
185+
def stop_casting(self, sink_name: str) -> str:
183186
"""
184187
Stops the existing Cast session on a specific receiver target.
185188
@@ -188,7 +191,7 @@ def stop_casting(self, sink_name):
188191
"""
189192
return self.execute('stopCasting', {'sinkName': sink_name})
190193

191-
def quit(self):
194+
def quit(self) -> NoReturn:
192195
"""
193196
Closes the browser and shuts down the ChromiumDriver executable
194197
that is started when starting the ChromiumDriver

py/selenium/webdriver/edge/options.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,22 @@ def __init__(self):
2828
self._use_webview = False
2929

3030
@property
31-
def use_chromium(self):
31+
def use_chromium(self) -> bool:
3232
return self._use_chromium
3333

3434
@use_chromium.setter
35-
def use_chromium(self, value):
35+
def use_chromium(self, value: bool):
3636
self._use_chromium = bool(value)
3737

3838
@property
39-
def use_webview(self):
39+
def use_webview(self) -> bool:
4040
return self._use_webview
4141

4242
@use_webview.setter
43-
def use_webview(self, value):
43+
def use_webview(self, value: bool):
4444
self._use_webview = bool(value)
4545

46-
def to_capabilities(self):
46+
def to_capabilities(self) -> dict:
4747
"""
4848
Creates a capabilities with all the options that have been set and
4949
:Returns: A dictionary with everything
@@ -61,5 +61,5 @@ def to_capabilities(self):
6161
return caps
6262

6363
@property
64-
def default_capabilities(self):
64+
def default_capabilities(self) -> dict:
6565
return DesiredCapabilities.EDGE.copy()

py/selenium/webdriver/edge/service.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from typing import List
1819
from selenium.webdriver.chromium import service
1920

2021

2122
class Service(service.ChromiumService):
2223

23-
def __init__(self, executable_path, port=0, verbose=False, log_path=None,
24-
service_args=None, env=None):
24+
def __init__(self, executable_path: str, port: int = 0, verbose: bool = False, log_path: str = None,
25+
service_args: List[str] = None, env=None):
2526
"""
2627
Creates a new instance of the EdgeDriver service.
2728
EdgeDriver provides an interface for Microsoft WebDriver to use

py/selenium/webdriver/edge/webdriver.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class WebDriver(ChromiumDriver):
3434
"""
3535

3636
def __init__(self, executable_path='MicrosoftWebDriver.exe', port=DEFAULT_PORT,
37-
options=None, service_args=None,
37+
options: Options = None, service_args=None,
3838
capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
39-
service=None, keep_alive=False, verbose=False):
39+
service: Service = None, keep_alive=False, verbose=False):
4040
"""
4141
Creates a new instance of the edge driver.
4242
Starts the service and then creates new instance of edge driver.
@@ -67,5 +67,5 @@ def __init__(self, executable_path='MicrosoftWebDriver.exe', port=DEFAULT_PORT,
6767
service_args, capabilities,
6868
service_log_path, service, keep_alive)
6969

70-
def create_options(self):
70+
def create_options(self) -> Options:
7171
return Options()

0 commit comments

Comments
 (0)