Skip to content

Commit

Permalink
[py]: Loosen mypy checks; be explicit in service args types and fix…
Browse files Browse the repository at this point in the history
… some mypy issues
  • Loading branch information
symonk committed Oct 9, 2022
1 parent 118f449 commit 9c0a284
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 32 deletions.
23 changes: 13 additions & 10 deletions py/mypy.ini
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
; The aim in future here is we would be able to turn (most) of these flags on, however the typing technical
; debt is quite colossal right now. For now we should maybe get everything working with the config here
; then look at going after partially or completely untyped defs as a phase-2.
[mypy]
files = selenium
; warn about per-module sections in the config file that do not match any files processed.
warn_unused_configs = True
; disallows subclassing of typing.Any.
disallow_subclassing_any = True
disallow_subclassing_any = False
; disallow usage of generic types that do not specify explicit type parameters.
disallow_any_generics = True
disallow_any_generics = False
; disallow calling functions without type annotations from functions that have type annotations.
disallow_untyped_calls = True
disallow_untyped_calls = False
; disallow defining functions without type annotations or with incomplete annotations.
disallow_untyped_defs = True
disallow_untyped_defs = False
; disallow defining functions with incomplete type annotations.
disallow_incomplete_defs = True
disallow_incomplete_defs = False
; type-checks the interior of functions without type annotations.
check_untyped_defs = True
check_untyped_defs = False
; reports an error whenever a function with type annotations is decorated with a decorator without annotations.
disallow_untyped_decorators = True
disallow_untyped_decorators = False
; changes the treatment of arguments with a default value of None by not implicitly making their type `typing.Optional`.
no_implicit_optional = True
no_implicit_optional = False
; warns about casting an expression to it's inferred type.
warn_redundant_casts = True
; warns about unneeded `# type: ignore` comments.
warn_unused_ignores = True
; warns when returning a value with typing.Any from a function with a non typing.Any return type.
warn_return_any = True
warn_return_any = False
; Shows a warning when encountering any code inferred to be unreachable after performing type analysis.
warn_unreachable = True
warn_unreachable = False

[mypy-trio_websocket]
; suppress error messages about imports that cannot be resolved.
Expand Down
1 change: 1 addition & 0 deletions py/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ markers =
xfail_safari: Tests expected to fail in Safari
xfail_webkitgtk: Tests expected to fail in webkitgtk
no_driver_after_test: If there are no drivers after the test it will create a new one.
addopts =
4 changes: 2 additions & 2 deletions py/selenium/webdriver/chrome/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Service(service.ChromiumService):
:param executable_path: install path of the chromedriver executable, defaults to `chromedriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_path: (Optional) String to be passed to the executable as `--log-path`.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""
Expand All @@ -36,7 +36,7 @@ def __init__(
self,
executable_path: str = DEFAULT_EXECUTABLE_PATH,
port: int = 0,
service_args: typing.Optional[typing.Sequence[str]] = None,
service_args: typing.Optional[typing.List[str]] = None,
log_path: typing.Optional[str] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/chromium/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ChromiumService(service.Service):
:param executable_path: install path of the executable.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_path: (Optional) String to be passed to the executable as `--log-path`.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
:param start_error_message: (Optional) Error message that forms part of the error when problems occur
Expand All @@ -36,7 +36,7 @@ def __init__(
self,
executable_path: str,
port: int = 0,
service_args: typing.Optional[typing.Sequence[str]] = None,
service_args: typing.Optional[typing.List[str]] = None,
log_path: typing.Optional[str] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
start_error_message: typing.Optional[str] = None,
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/common/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ def __init__(
# Default value for every python subprocess: subprocess.Popen(..., creationflags=0)
self.creation_flags = 0
self.env = env or os.environ
self.process: typing.Optional[subprocess.Popen] = None

@property
def service_url(self) -> str:
Expand Down Expand Up @@ -161,7 +160,8 @@ def stop(self) -> None:
if self.log_file != PIPE and not (self.log_file == DEVNULL and _HAS_NATIVE_DEVNULL):
with contextlib.suppress(Exception):
# Todo: Be explicit in what we are catching here.
self.log_file.close()
if hasattr(self.log_file, "close"):
self.log_file.close()

if self.process is not None:
with contextlib.suppress(TypeError):
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/edge/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Service(service.ChromiumService):
:param verbose: (Deprecated) Whether to make the webdriver more verbose (passes the --verbose option to the binary).
Defaults to False.
:param log_path: (Optional) String to be passed to the executable as `--log-path`.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""

Expand All @@ -41,7 +41,7 @@ def __init__(
port: int = 0,
verbose: bool = False,
log_path: typing.Optional[str] = None,
service_args: typing.Optional[typing.Sequence[str]] = None,
service_args: typing.Optional[typing.List[str]] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
):
self.service_args = service_args or []
Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/firefox/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Service(service.Service):
:param executable_path: install path of the geckodriver executable, defaults to `geckodriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_path: (Optional) File path for the file to be opened and passed as the subprocess stdout/stderr handler,
defaults to `geckodriver.log`.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
Expand Down
14 changes: 7 additions & 7 deletions py/selenium/webdriver/remote/mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ def context(self):
"""
return self._driver.execute(Command.CURRENT_CONTEXT_HANDLE)

@property
def contexts(self):
"""
returns a list of available contexts
"""
return self._driver.execute(Command.CONTEXT_HANDLES)

@context.setter
def context(self, new_context) -> None:
"""
sets the current context
"""
self._driver.execute(Command.SWITCH_TO_CONTEXT, {"name": new_context})

@property
def contexts(self):
"""
returns a list of available contexts
"""
return self._driver.execute(Command.CONTEXT_HANDLES)
2 changes: 1 addition & 1 deletion py/selenium/webdriver/safari/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Service(service.Service):
:param executable_path: install path of the safaridriver executable, defaults to `/usr/bin/safaridriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param quiet: Suppress driver stdout & stderr, redirects to os.devnull if enabled.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""

Expand Down
2 changes: 1 addition & 1 deletion py/selenium/webdriver/support/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def groups(self) -> Sequence[str]:
if m.match(RGBA_PATTERN, str_):
return cls(*m.groups)
if m.match(RGBA_PCT_PATTERN, str_):
rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]]) # type: ignore
rgba = tuple([float(each) / 100 * 255 for each in m.groups[:3]] + [m.groups[3]])
return cls(*rgba)
if m.match(HEX_PATTERN, str_):
rgb = tuple(int(each, 16) for each in m.groups)
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/webkitgtk/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Service(service.Service):
:param executable_path: install path of the WebKitWebDriver executable, defaults to `WebKitWebDriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_path: (Optional) File path for the file to be opened and passed as the subprocess stdout/stderr handler.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""
Expand All @@ -37,7 +37,7 @@ def __init__(
executable_path: str = DEFAULT_EXECUTABLE_PATH,
port: int = 0,
log_path: typing.Optional[str] = None,
service_args: typing.Optional[typing.Sequence[str]] = None,
service_args: typing.Optional[typing.List[str]] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
):
self.service_args = service_args or []
Expand Down
4 changes: 2 additions & 2 deletions py/selenium/webdriver/wpewebkit/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Service(service.Service):
:param executable_path: install path of the WPEWebDriver executable, defaults to `WPEWebDriver`.
:param port: Port for the service to run on, defaults to 0 where the operating system will decide.
:param service_args: (Optional) Sequence of args to be passed to the subprocess when launching the executable.
:param service_args: (Optional) List of args to be passed to the subprocess when launching the executable.
:param log_path: (Optional) File path for the file to be opened and passed as the subprocess stdout/stderr handler.
:param env: (Optional) Mapping of environment variables for the new process, defaults to `os.environ`.
"""
Expand All @@ -37,7 +37,7 @@ def __init__(
executable_path: str = DEFAULT_EXECUTABLE_PATH,
port: int = 0,
log_path: typing.Optional[str] = None,
service_args: typing.Optional[typing.Sequence[str]] = None,
service_args: typing.Optional[typing.List[str]] = None,
env: typing.Optional[typing.Mapping[str, str]] = None,
):
self.service_args = service_args or []
Expand Down

0 comments on commit 9c0a284

Please sign in to comment.