From 26c094378f3f469fb9ca13590ff3d812caff083a Mon Sep 17 00:00:00 2001 From: "oleg.hoefling" Date: Sun, 23 May 2021 15:09:12 +0200 Subject: [PATCH 1/3] [py] PEP 484 type hints for selenium.webdriver.remote.errorhandler Signed-off-by: oleg.hoefling --- py/selenium/webdriver/remote/errorhandler.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index 314fa88a342e4..52ab6596a1c77 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -15,6 +15,13 @@ # specific language governing permissions and limitations # under the License. +from typing import Any, Dict, Mapping, Type, TypeVar + + +_KT = TypeVar("_KT") +_VT = TypeVar("_VT") + + from selenium.common.exceptions import (ElementClickInterceptedException, ElementNotInteractableException, ElementNotSelectableException, @@ -95,7 +102,7 @@ class ErrorHandler(object): Handles errors returned by the WebDriver server. """ - def check_response(self, response): + def check_response(self, response: Dict[str, Any]) -> None: """ Checks that a JSON response from the WebDriver does not have an error. @@ -110,7 +117,7 @@ def check_response(self, response): return value = None message = response.get("message", "") - screen = response.get("screen", "") + screen: str = response.get("screen", "") stacktrace = None if isinstance(status, int): value_json = response.get('value', None) @@ -133,7 +140,7 @@ def check_response(self, response): pass if status in ErrorCode.NO_SUCH_ELEMENT: - exception_class = NoSuchElementException + exception_class: Type[WebDriverException] = NoSuchElementException elif status in ErrorCode.NO_SUCH_FRAME: exception_class = NoSuchFrameException elif status in ErrorCode.NO_SUCH_WINDOW: @@ -232,8 +239,8 @@ def check_response(self, response): alert_text = value['data'].get('text') elif 'alert' in value: alert_text = value['alert'].get('text') - raise exception_class(message, screen, stacktrace, alert_text) + raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here raise exception_class(message, screen, stacktrace) - def _value_or_default(self, obj, key, default): + def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT: return obj[key] if key in obj else default From c87af94be27afe98b0e6f2ca266f03e5b55c3406 Mon Sep 17 00:00:00 2001 From: "oleg.hoefling" Date: Sun, 27 Jun 2021 17:46:18 +0200 Subject: [PATCH 2/3] fix flake8 errors Signed-off-by: oleg.hoefling --- py/selenium/webdriver/remote/errorhandler.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index 52ab6596a1c77..c5d65bbcba80c 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -17,11 +17,6 @@ from typing import Any, Dict, Mapping, Type, TypeVar - -_KT = TypeVar("_KT") -_VT = TypeVar("_VT") - - from selenium.common.exceptions import (ElementClickInterceptedException, ElementNotInteractableException, ElementNotSelectableException, @@ -52,6 +47,10 @@ WebDriverException) +_KT = TypeVar("_KT") +_VT = TypeVar("_VT") + + class ErrorCode(object): """ Error codes defined in the WebDriver wire protocol. From cea2d9515105f85428008e2a8b0a54f7f094b420 Mon Sep 17 00:00:00 2001 From: "oleg.hoefling" Date: Mon, 12 Jul 2021 23:07:57 +0200 Subject: [PATCH 3/3] make exception_class type more explicit by declaring it before status checks Signed-off-by: oleg.hoefling --- py/selenium/webdriver/remote/errorhandler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/py/selenium/webdriver/remote/errorhandler.py b/py/selenium/webdriver/remote/errorhandler.py index c5d65bbcba80c..79d199311eb1e 100644 --- a/py/selenium/webdriver/remote/errorhandler.py +++ b/py/selenium/webdriver/remote/errorhandler.py @@ -138,8 +138,9 @@ def check_response(self, response: Dict[str, Any]) -> None: except ValueError: pass + exception_class: Type[WebDriverException] if status in ErrorCode.NO_SUCH_ELEMENT: - exception_class: Type[WebDriverException] = NoSuchElementException + exception_class = NoSuchElementException elif status in ErrorCode.NO_SUCH_FRAME: exception_class = NoSuchFrameException elif status in ErrorCode.NO_SUCH_WINDOW: @@ -207,7 +208,7 @@ def check_response(self, response: Dict[str, Any]) -> None: if message == "" and 'message' in value: message = value['message'] - screen = None + screen = None # type: ignore[assignment] if 'screen' in value: screen = value['screen']