From fa16d0de6ab1479d83576db0e3eb620ae866bdab Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Sat, 29 Aug 2026 00:10:44 +0300 Subject: [PATCH 1/2] refactor: Enhance type annotations in options, pointer_input, and proxy modules --- py/pyproject.toml | 3 +-- .../webdriver/common/actions/pointer_input.py | 2 +- py/selenium/webdriver/common/proxy.py | 20 +++++++++---------- py/selenium/webdriver/common/service.py | 7 +++---- py/selenium/webdriver/firefox/options.py | 2 +- 5 files changed, 16 insertions(+), 18 deletions(-) diff --git a/py/pyproject.toml b/py/pyproject.toml index f4768ea4617ce..21f644dc0510f 100644 --- a/py/pyproject.toml +++ b/py/pyproject.toml @@ -125,11 +125,10 @@ 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 = false # Shows a warning when encountering any code inferred to be unreachable after performing type analysis. -warn_unreachable = false +warn_unreachable = true [[tool.mypy.overrides]] module = [ - "selenium.webdriver.common.devtools.*", "selenium.webdriver.common.bidi.*", ] ignore_errors = true diff --git a/py/selenium/webdriver/common/actions/pointer_input.py b/py/selenium/webdriver/common/actions/pointer_input.py index 448b2c6b18390..d4d1fb837f397 100644 --- a/py/selenium/webdriver/common/actions/pointer_input.py +++ b/py/selenium/webdriver/common/actions/pointer_input.py @@ -39,7 +39,7 @@ def create_pointer_move( duration=DEFAULT_MOVE_DURATION, x: float = 0, y: float = 0, - origin: WebElement | None = None, + origin: WebElement | str | None = None, **kwargs, ): action = {"type": "pointerMove", "duration": duration, "x": x, "y": y, **kwargs} diff --git a/py/selenium/webdriver/common/proxy.py b/py/selenium/webdriver/common/proxy.py index eadf1d069709f..5cc34c0c7bf14 100644 --- a/py/selenium/webdriver/common/proxy.py +++ b/py/selenium/webdriver/common/proxy.py @@ -74,16 +74,16 @@ def __set__(self, obj, value): class Proxy: """Proxy configuration containing proxy type and necessary proxy settings.""" - proxyType = ProxyType.UNSPECIFIED - autodetect = False - httpProxy = "" - noProxy = "" - proxyAutoconfigUrl = "" - sslProxy = "" - socksProxy = "" - socksUsername = "" - socksPassword = "" - socksVersion = None + proxyType: dict = ProxyType.UNSPECIFIED + autodetect: bool = False + httpProxy: str = "" + noProxy: str | list[str] = "" + proxyAutoconfigUrl: str = "" + sslProxy: str = "" + socksProxy: str = "" + socksUsername: str = "" + socksPassword: str = "" + socksVersion: int | None = None # create descriptor type objects auto_detect = _ProxyTypeDescriptor("autodetect", ProxyType.AUTODETECT) diff --git a/py/selenium/webdriver/common/service.py b/py/selenium/webdriver/common/service.py index fa53be3988fe9..42c94ea12147e 100644 --- a/py/selenium/webdriver/common/service.py +++ b/py/selenium/webdriver/common/service.py @@ -22,7 +22,6 @@ import sys from abc import ABC, abstractmethod from collections.abc import Mapping -from io import IOBase from subprocess import PIPE from time import sleep from typing import IO, Any @@ -156,10 +155,10 @@ def send_remote_shutdown_command(self) -> None: def stop(self) -> None: """Stops the service.""" if self.log_output not in {PIPE, subprocess.DEVNULL}: - if isinstance(self.log_output, IOBase) and self._owns_log_output: - self.log_output.close() - elif isinstance(self.log_output, int): + if isinstance(self.log_output, int): os.close(self.log_output) + elif self.log_output is not None and self._owns_log_output: + self.log_output.close() if self.process is not None: try: if self.process.poll() is None: diff --git a/py/selenium/webdriver/firefox/options.py b/py/selenium/webdriver/firefox/options.py index ef3afe546fb91..598673fcfc915 100644 --- a/py/selenium/webdriver/firefox/options.py +++ b/py/selenium/webdriver/firefox/options.py @@ -24,7 +24,7 @@ class Log: def __init__(self) -> None: - self.level = None + self.level: str | None = None def to_capabilities(self) -> dict: if self.level: From 7c71f06f343308760fcdd0a93d8dd62ea7c92757 Mon Sep 17 00:00:00 2001 From: Alex Popov Date: Sat, 29 Aug 2026 03:02:51 +0300 Subject: [PATCH 2/2] applied same pattern as for method from_json at the line 598 --- py/selenium/webdriver/common/_bidi/serialization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/selenium/webdriver/common/_bidi/serialization.py b/py/selenium/webdriver/common/_bidi/serialization.py index 38fbcf62cd859..fcd8ed84300d0 100644 --- a/py/selenium/webdriver/common/_bidi/serialization.py +++ b/py/selenium/webdriver/common/_bidi/serialization.py @@ -287,7 +287,7 @@ def as_json(self) -> dict: return payload @classmethod - def from_json(cls, payload: dict) -> Any: + def from_json(cls, payload: Any) -> Any: if not isinstance(payload, dict): got = type(payload).__name__ raise BiDiSerializationError(f"{cls.__name__} expected an object on the wire, got {got} {payload!r}")