Skip to content

Commit 31cc432

Browse files
authored
[py] python3.7+ syntax throughout *.py files. (#10647)
* upgrading python to `3.7+` throughout * additional `f` conversions * fix `flake8` violations; tidy up formatting * make python3 changes apply to only `/py`
1 parent a4146c0 commit 31cc432

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+95
-101
lines changed

py/conftest.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def driver(request):
8888
pytest.skip("Webkit tests can only run on Linux")
8989

9090
# conditionally mark tests as expected to fail based on driver
91-
marker = request.node.get_closest_marker('xfail_{0}'.format(driver_class.lower()))
91+
marker = request.node.get_closest_marker(f'xfail_{driver_class.lower()}')
9292

9393
if marker is not None:
9494
if "run" in marker.kwargs:
@@ -149,7 +149,7 @@ def get_options(driver_class, config):
149149

150150
if browser_path or browser_args:
151151
if not options:
152-
options = getattr(webdriver, '{}Options'.format(driver_class))()
152+
options = getattr(webdriver, f'{driver_class}Options')()
153153
if driver_class == 'WebKitGTK':
154154
options.overlay_scrollbars_enabled = False
155155
if browser_path is not None:
@@ -213,21 +213,21 @@ def wait_for_server(url, timeout):
213213
try:
214214
urlopen(url)
215215
return 1
216-
except IOError:
216+
except OSError:
217217
time.sleep(0.2)
218218
return 0
219219

220220
_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
221-
url = 'http://{}:{}/status'.format(_host, _port)
221+
url = f'http://{_host}:{_port}/status'
222222
try:
223223
_socket.connect((_host, _port))
224224
print('The remote driver server is already running or something else'
225225
'is using port {}, continuing...'.format(_port))
226226
except Exception:
227227
print('Starting the Selenium server')
228228
process = subprocess.Popen(['java', '-jar', _path, 'standalone', '--port', '4444'])
229-
print('Selenium server running as process: {}'.format(process.pid))
230-
assert wait_for_server(url, 10), 'Timed out waiting for Selenium server at {}'.format(url)
229+
print(f'Selenium server running as process: {process.pid}')
230+
assert wait_for_server(url, 10), f'Timed out waiting for Selenium server at {url}'
231231
print('Selenium server is ready')
232232
yield process
233233
process.terminate()

py/docs/source/conf.py

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

18-
from __future__ import unicode_literals
1918

2019
import sys
2120
import os

py/generate.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def inline_doc(description):
124124
return ''
125125

126126
description = escape_backticks(description)
127-
lines = ['#: {}'.format(l) for l in description.split('\n')]
127+
lines = [f'#: {l}' for l in description.split('\n')]
128128
return '\n'.join(lines)
129129

130130

@@ -162,7 +162,7 @@ def ref_to_python(ref):
162162
'''
163163
if '.' in ref:
164164
domain, subtype = ref.split('.')
165-
ref = '{}.{}'.format(snake_case(domain), subtype)
165+
ref = f'{snake_case(domain)}.{subtype}'
166166
return f"{ref}"
167167

168168

@@ -228,7 +228,7 @@ def py_annotation(self):
228228
if self.items:
229229
if self.items.ref:
230230
py_ref = ref_to_python(self.items.ref)
231-
ann = "typing.List[{}]".format(py_ref)
231+
ann = f"typing.List[{py_ref}]"
232232
else:
233233
ann = 'typing.List[{}]'.format(
234234
CdpPrimitiveType.get_annotation(self.items.type))
@@ -500,7 +500,7 @@ def generate_code(self):
500500
py_type = f'typing.List[{nested_type}]'
501501
else:
502502
if self.ref:
503-
py_type = "{}".format(ref_to_python(self.ref))
503+
py_type = f"{ref_to_python(self.ref)}"
504504
else:
505505
py_type = CdpPrimitiveType.get_annotation(
506506
typing.cast(str, self.type))
@@ -939,7 +939,7 @@ def parse(json_path, output_path):
939939
:returns: a list of CDP domain objects
940940
'''
941941
global current_version
942-
with open(json_path, "r") as json_file:
942+
with open(json_path) as json_file:
943943
schema = json.load(json_file)
944944
version = schema['version']
945945
assert (version['major'], version['minor']) == ('1', '3')
@@ -960,7 +960,7 @@ def generate_init(init_path, domains):
960960
with open(init_path, "w") as init_file:
961961
init_file.write(INIT_HEADER)
962962
for domain in domains:
963-
init_file.write('from . import {}\n'.format(domain.module))
963+
init_file.write(f'from . import {domain.module}\n')
964964
init_file.write('from . import util\n\n')
965965

966966

py/selenium/common/exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ class UnexpectedAlertPresentException(WebDriverException):
138138
"""
139139

140140
def __init__(self, msg: Optional[str] = None, screen: Optional[str] = None, stacktrace: Optional[Sequence[str]] = None, alert_text: Optional[str] = None) -> None:
141-
super(UnexpectedAlertPresentException, self).__init__(msg, screen, stacktrace)
141+
super().__init__(msg, screen, stacktrace)
142142
self.alert_text = alert_text
143143

144144
def __str__(self) -> str:
145-
return "Alert Text: %s\n%s" % (self.alert_text, super(UnexpectedAlertPresentException, self).__str__())
145+
return f"Alert Text: {self.alert_text}\n{super().__str__()}"
146146

147147

148148
class NoAlertPresentException(WebDriverException):

py/selenium/webdriver/chrome/service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, executable_path: str = DEFAULT_EXECUTABLE_PATH,
3939
- service_args : List of args to pass to the chromedriver service
4040
- log_path : Path for the chromedriver service to log to"""
4141

42-
super(Service, self).__init__(
42+
super().__init__(
4343
executable_path,
4444
port,
4545
service_args,

py/selenium/webdriver/chrome/webdriver.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from .service import DEFAULT_EXECUTABLE_PATH, Service
2121
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2222

23-
2423
DEFAULT_PORT = 0
2524
DEFAULT_SERVICE_LOG_PATH = None
2625
DEFAULT_KEEP_ALIVE = None
@@ -67,7 +66,7 @@ def __init__(self, executable_path=DEFAULT_EXECUTABLE_PATH, port=DEFAULT_PORT,
6766
if not service:
6867
service = Service(executable_path, port, service_args, service_log_path)
6968

70-
super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
71-
port, options,
72-
service_args, desired_capabilities,
73-
service_log_path, service, keep_alive)
69+
super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
70+
port, options,
71+
service_args, desired_capabilities,
72+
service_log_path, service, keep_alive)

py/selenium/webdriver/chromium/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ChromiumOptions(ArgOptions):
2828
KEY = "goog:chromeOptions"
2929

3030
def __init__(self) -> None:
31-
super(ChromiumOptions, self).__init__()
31+
super().__init__()
3232
self._binary_location = ''
3333
self._extension_files = []
3434
self._extensions = []
@@ -98,7 +98,7 @@ def add_extension(self, extension: str) -> None:
9898
if os.path.exists(extension_to_add):
9999
self._extension_files.append(extension_to_add)
100100
else:
101-
raise IOError("Path to the extension doesn't exist")
101+
raise OSError("Path to the extension doesn't exist")
102102
else:
103103
raise ValueError("argument can not be null")
104104

py/selenium/webdriver/chromium/remote_connection.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ def __init__(self, remote_server_addr, vendor_prefix, browser_name, keep_alive=T
2727
self._commands["setNetworkConditions"] = ('POST', '/session/$sessionId/chromium/network_conditions')
2828
self._commands["getNetworkConditions"] = ('GET', '/session/$sessionId/chromium/network_conditions')
2929
self._commands["deleteNetworkConditions"] = ('DELETE', '/session/$sessionId/chromium/network_conditions')
30-
self._commands['executeCdpCommand'] = ('POST', '/session/$sessionId/{}/cdp/execute'.format(vendor_prefix))
31-
self._commands['getSinks'] = ('GET', '/session/$sessionId/{}/cast/get_sinks'.format(vendor_prefix))
32-
self._commands['getIssueMessage'] = ('GET', '/session/$sessionId/{}/cast/get_issue_message'.format(vendor_prefix))
33-
self._commands['setSinkToUse'] = ('POST', '/session/$sessionId/{}/cast/set_sink_to_use'.format(vendor_prefix))
34-
self._commands['startDesktopMirroring'] = ('POST', '/session/$sessionId/{}/cast/start_desktop_mirroring'.format(vendor_prefix))
35-
self._commands['startTabMirroring'] = ('POST', '/session/$sessionId/{}/cast/start_tab_mirroring'.format(vendor_prefix))
36-
self._commands['stopCasting'] = ('POST', '/session/$sessionId/{}/cast/stop_casting'.format(vendor_prefix))
30+
self._commands['executeCdpCommand'] = ('POST', f'/session/$sessionId/{vendor_prefix}/cdp/execute')
31+
self._commands['getSinks'] = ('GET', f'/session/$sessionId/{vendor_prefix}/cast/get_sinks')
32+
self._commands['getIssueMessage'] = ('GET', f'/session/$sessionId/{vendor_prefix}/cast/get_issue_message')
33+
self._commands['setSinkToUse'] = ('POST', f'/session/$sessionId/{vendor_prefix}/cast/set_sink_to_use')
34+
self._commands['startDesktopMirroring'] = ('POST', f'/session/$sessionId/{vendor_prefix}/cast/start_desktop_mirroring')
35+
self._commands['startTabMirroring'] = ('POST', f'/session/$sessionId/{vendor_prefix}/cast/start_tab_mirroring')
36+
self._commands['stopCasting'] = ('POST', f'/session/$sessionId/{vendor_prefix}/cast/stop_casting')

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
POINTER = "pointer"
2121
NONE = "none"
2222
WHEEL = "wheel"
23-
SOURCE_TYPES = set([KEY, POINTER, NONE])
23+
SOURCE_TYPES = {KEY, POINTER, NONE}
2424

2525
POINTER_MOUSE = "mouse"
2626
POINTER_TOUCH = "touch"
2727
POINTER_PEN = "pen"
2828

29-
POINTER_KINDS = set([POINTER_MOUSE, POINTER_TOUCH, POINTER_PEN])
29+
POINTER_KINDS = {POINTER_MOUSE, POINTER_TOUCH, POINTER_PEN}
3030

3131

3232
class Interaction:

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, source=None):
2525
if not source:
2626
source = KeyInput(KEY)
2727
self.source = source
28-
super(KeyActions, self).__init__(source)
28+
super().__init__(source)
2929

3030
def key_down(self, letter):
3131
return self._key_action("create_key_down", letter)

0 commit comments

Comments
 (0)