Skip to content

Commit de8ac45

Browse files
[py] PEP 484 type hints for selenium.webdriver.remote.errorhandler (#9605)
* [py] PEP 484 type hints for selenium.webdriver.remote.errorhandler Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com> * fix flake8 errors Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com> * make exception_class type more explicit by declaring it before status checks Signed-off-by: oleg.hoefling <oleg.hoefling@gmail.com> Co-authored-by: David Burns <david.burns@theautomatedtester.co.uk>
1 parent a67878e commit de8ac45

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

py/selenium/webdriver/remote/errorhandler.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18+
from typing import Any, Dict, Mapping, Type, TypeVar
19+
1820
from selenium.common.exceptions import (ElementClickInterceptedException,
1921
ElementNotInteractableException,
2022
ElementNotSelectableException,
@@ -45,6 +47,10 @@
4547
WebDriverException)
4648

4749

50+
_KT = TypeVar("_KT")
51+
_VT = TypeVar("_VT")
52+
53+
4854
class ErrorCode(object):
4955
"""
5056
Error codes defined in the WebDriver wire protocol.
@@ -95,7 +101,7 @@ class ErrorHandler(object):
95101
Handles errors returned by the WebDriver server.
96102
"""
97103

98-
def check_response(self, response):
104+
def check_response(self, response: Dict[str, Any]) -> None:
99105
"""
100106
Checks that a JSON response from the WebDriver does not have an error.
101107
@@ -110,7 +116,7 @@ def check_response(self, response):
110116
return
111117
value = None
112118
message = response.get("message", "")
113-
screen = response.get("screen", "")
119+
screen: str = response.get("screen", "")
114120
stacktrace = None
115121
if isinstance(status, int):
116122
value_json = response.get('value', None)
@@ -132,6 +138,7 @@ def check_response(self, response):
132138
except ValueError:
133139
pass
134140

141+
exception_class: Type[WebDriverException]
135142
if status in ErrorCode.NO_SUCH_ELEMENT:
136143
exception_class = NoSuchElementException
137144
elif status in ErrorCode.NO_SUCH_FRAME:
@@ -201,7 +208,7 @@ def check_response(self, response):
201208
if message == "" and 'message' in value:
202209
message = value['message']
203210

204-
screen = None
211+
screen = None # type: ignore[assignment]
205212
if 'screen' in value:
206213
screen = value['screen']
207214

@@ -232,8 +239,8 @@ def check_response(self, response):
232239
alert_text = value['data'].get('text')
233240
elif 'alert' in value:
234241
alert_text = value['alert'].get('text')
235-
raise exception_class(message, screen, stacktrace, alert_text)
242+
raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here
236243
raise exception_class(message, screen, stacktrace)
237244

238-
def _value_or_default(self, obj, key, default):
245+
def _value_or_default(self, obj: Mapping[_KT, _VT], key: _KT, default: _VT) -> _VT:
239246
return obj[key] if key in obj else default

0 commit comments

Comments
 (0)