Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[py] PEP 484 type hints for selenium.webdriver.remote.errorhandler #9605

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions py/selenium/webdriver/remote/errorhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.

from typing import Any, Dict, Mapping, Type, TypeVar

from selenium.common.exceptions import (ElementClickInterceptedException,
ElementNotInteractableException,
ElementNotSelectableException,
Expand Down Expand Up @@ -45,6 +47,10 @@
WebDriverException)


_KT = TypeVar("_KT")
_VT = TypeVar("_VT")


class ErrorCode(object):
"""
Error codes defined in the WebDriver wire protocol.
Expand Down Expand Up @@ -95,7 +101,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.

Expand All @@ -110,7 +116,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)
Expand All @@ -132,6 +138,7 @@ def check_response(self, response):
except ValueError:
pass

exception_class: Type[WebDriverException]
if status in ErrorCode.NO_SUCH_ELEMENT:
exception_class = NoSuchElementException
elif status in ErrorCode.NO_SUCH_FRAME:
Expand Down Expand Up @@ -201,7 +208,7 @@ def check_response(self, response):
if message == "" and 'message' in value:
message = value['message']

screen = None
screen = None # type: ignore[assignment]
if 'screen' in value:
screen = value['screen']

Expand Down Expand Up @@ -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