Skip to content

Commit 4e1aafd

Browse files
authored
chore: fix some mypy errors (#1153)
* chore: fix some mypy
1 parent f8922da commit 4e1aafd

File tree

6 files changed

+35
-24
lines changed

6 files changed

+35
-24
lines changed

appium/webdriver/common/appiumby.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ class AppiumBy(By):
4949
'-flutter semantics label',
5050
'-flutter type',
5151
'-flutter key',
52+
'-flutter text',
5253
'-flutter text containing',
5354
]

appium/webdriver/extensions/android/activities.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def wait_activity(self, activity: str, timeout: int, interval: int = 1) -> bool:
5050
`True` if the target activity is shown
5151
"""
5252
try:
53-
WebDriverWait(self, timeout, interval).until(lambda d: d.current_activity == activity)
53+
WebDriverWait(self, timeout, interval).until( # type: ignore[type-var]
54+
lambda d: d.current_activity == activity
55+
)
5456
return True
5557
except TimeoutException:
5658
return False

appium/webdriver/extensions/flutter_integration/flutter_commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def execute_flutter_command(self, scriptName: str, params: dict) -> Any:
290290
"""
291291
return self.driver.execute_script(f'flutter: {scriptName}', params)
292292

293-
def __get_locator_options(self, locator: Union[WebElement, 'FlutterFinder']) -> Dict[str, dict]:
293+
def __get_locator_options(self, locator: Union[WebElement, 'FlutterFinder']) -> Dict[str, Union[dict, WebElement]]:
294294
if isinstance(locator, WebElement):
295295
return {'element': locator}
296296
return {'locator': locator.to_dict()}

appium/webdriver/extensions/flutter_integration/flutter_finder.py

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

18-
from typing import Tuple, Union
18+
from typing import Tuple, Union, cast
1919

2020
from selenium.webdriver.common.by import ByType as SeleniumByType
2121

@@ -30,23 +30,23 @@ def __init__(self, using: Union[SeleniumByType, AppiumByType], value: str) -> No
3030

3131
@staticmethod
3232
def by_key(value: str) -> 'FlutterFinder':
33-
return FlutterFinder(AppiumBy.FLUTTER_INTEGRATION_KEY, value)
33+
return FlutterFinder(cast(AppiumByType, AppiumBy.FLUTTER_INTEGRATION_KEY), value)
3434

3535
@staticmethod
3636
def by_text(value: str) -> 'FlutterFinder':
37-
return FlutterFinder(AppiumBy.FLUTTER_INTEGRATION_TEXT, value)
37+
return FlutterFinder(cast(AppiumByType, AppiumBy.FLUTTER_INTEGRATION_TEXT), value)
3838

3939
@staticmethod
4040
def by_semantics_label(value: str) -> 'FlutterFinder':
41-
return FlutterFinder(AppiumBy.FLUTTER_INTEGRATION_SEMANTICS_LABEL, value)
41+
return FlutterFinder(cast(AppiumByType, AppiumBy.FLUTTER_INTEGRATION_SEMANTICS_LABEL), value)
4242

4343
@staticmethod
4444
def by_type(value: str) -> 'FlutterFinder':
45-
return FlutterFinder(AppiumBy.FLUTTER_INTEGRATION_TYPE, value)
45+
return FlutterFinder(cast(AppiumByType, AppiumBy.FLUTTER_INTEGRATION_TYPE), value)
4646

4747
@staticmethod
4848
def by_text_containing(value: str) -> 'FlutterFinder':
49-
return FlutterFinder(AppiumBy.FLUTTER_INTEGRATION_TEXT_CONTAINING, value)
49+
return FlutterFinder(cast(AppiumByType, AppiumBy.FLUTTER_INTEGRATION_TEXT_CONTAINING), value)
5050

5151
def to_dict(self) -> dict:
5252
return {'using': self.using, 'value': self.value}

appium/webdriver/webdriver.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
15+
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union
1616

1717
from selenium import webdriver
1818
from selenium.common.exceptions import (
@@ -21,14 +21,12 @@
2121
UnknownMethodException,
2222
WebDriverException,
2323
)
24-
from selenium.webdriver.common.by import By
2524
from selenium.webdriver.remote.command import Command as RemoteCommand
2625
from selenium.webdriver.remote.remote_connection import RemoteConnection
2726
from typing_extensions import Self
2827

2928
from appium.common.logger import logger
3029
from appium.options.common.base import AppiumOptions
31-
from appium.webdriver.common.appiumby import AppiumBy
3230

3331
from .appium_connection import AppiumConnection
3432
from .client_config import AppiumClientConfig
@@ -241,7 +239,7 @@ class WebDriver(
241239
def __init__(
242240
self,
243241
command_executor: Union[str, AppiumConnection] = 'http://127.0.0.1:4723',
244-
extensions: Optional[List['WebDriver']] = None,
242+
extensions: Optional[List[Type['ExtensionBase']]] = None,
245243
options: Union[AppiumOptions, List[AppiumOptions], None] = None,
246244
client_config: Optional[AppiumClientConfig] = None,
247245
):
@@ -250,28 +248,22 @@ def __init__(
250248
)
251249
super().__init__(
252250
command_executor=command_executor,
253-
options=options,
251+
options=options, # type: ignore[arg-type]
254252
locator_converter=AppiumLocatorConverter(),
255253
web_element_cls=MobileWebElement,
256254
client_config=client_config,
257255
)
258256

257+
# to explicitly set type after the initialization
258+
self.command_executor: RemoteConnection
259+
259260
self._add_commands()
260261

261262
self.error_handler = MobileErrorHandler()
262263

263264
if client_config and client_config.direct_connection:
264265
self._update_command_executor(keep_alive=client_config.keep_alive)
265266

266-
# add new method to the `find_by_*` pantheon
267-
By.IOS_PREDICATE = AppiumBy.IOS_PREDICATE
268-
By.IOS_CLASS_CHAIN = AppiumBy.IOS_CLASS_CHAIN
269-
By.ANDROID_UIAUTOMATOR = AppiumBy.ANDROID_UIAUTOMATOR
270-
By.ANDROID_VIEWTAG = AppiumBy.ANDROID_VIEWTAG
271-
By.ACCESSIBILITY_ID = AppiumBy.ACCESSIBILITY_ID
272-
By.IMAGE = AppiumBy.IMAGE
273-
By.CUSTOM = AppiumBy.CUSTOM
274-
275267
self._absent_extensions: Set[str] = set()
276268

277269
self._extensions = extensions or []
@@ -286,6 +278,14 @@ def __init__(
286278
method, url_cmd = instance.add_command()
287279
self.command_executor.add_command(method_name, method.upper(), url_cmd)
288280

281+
if TYPE_CHECKING:
282+
283+
def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'MobileWebElement': # type: ignore[override]
284+
...
285+
286+
def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['MobileWebElement']: # type: ignore[override]
287+
...
288+
289289
def delete_extensions(self) -> None:
290290
"""Delete extensions added in the class with 'setattr'"""
291291
for extension in self._extensions:

appium/webdriver/webelement.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from typing import Callable, Dict, Optional, Union
15+
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union
1616

1717
from selenium.webdriver.common.utils import keys_to_typing
1818
from selenium.webdriver.remote.command import Command as RemoteCommand
@@ -26,6 +26,14 @@ class WebElement(SeleniumWebElement):
2626
_execute: Callable
2727
_id: str
2828

29+
if TYPE_CHECKING:
30+
31+
def find_element(self, by: str, value: Union[str, Dict, None] = None) -> Self: # type: ignore[override]
32+
...
33+
34+
def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List[Self]: # type: ignore[override]
35+
...
36+
2937
def get_attribute(self, name: str) -> Optional[Union[str, Dict]]: # type: ignore[override]
3038
"""Gets the given attribute or property of the element.
3139
@@ -108,7 +116,7 @@ def location_in_view(self) -> Dict[str, int]:
108116
return self._execute(Command.LOCATION_IN_VIEW)['value']
109117

110118
# Override
111-
def send_keys(self, *value: str) -> Self:
119+
def send_keys(self, *value: str) -> Self: # type: ignore[override]
112120
"""Simulates typing into the element.
113121
114122
Args:

0 commit comments

Comments
 (0)