Skip to content

chore: fix some mypy #1153

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions appium/webdriver/common/appiumby.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ class AppiumBy(By):
'-flutter semantics label',
'-flutter type',
'-flutter key',
'-flutter text',
'-flutter text containing',
]
4 changes: 3 additions & 1 deletion appium/webdriver/extensions/android/activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def wait_activity(self, activity: str, timeout: int, interval: int = 1) -> bool:
`True` if the target activity is shown
"""
try:
WebDriverWait(self, timeout, interval).until(lambda d: d.current_activity == activity)
WebDriverWait(self, timeout, interval).until( # type: ignore[type-var]
lambda d: d.current_activity == activity
)
return True
except TimeoutException:
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ def execute_flutter_command(self, scriptName: str, params: dict) -> Any:
"""
return self.driver.execute_script(f'flutter: {scriptName}', params)

def __get_locator_options(self, locator: Union[WebElement, 'FlutterFinder']) -> Dict[str, dict]:
def __get_locator_options(self, locator: Union[WebElement, 'FlutterFinder']) -> Dict[str, Union[dict, WebElement]]:
if isinstance(locator, WebElement):
return {'element': locator}
return {'locator': locator.to_dict()}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

from typing import Tuple, Union
from typing import Tuple, Union, cast

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

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

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

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

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

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

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

def to_dict(self) -> dict:
return {'using': self.using, 'value': self.value}
Expand Down
28 changes: 14 additions & 14 deletions appium/webdriver/webdriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union

from selenium import webdriver
from selenium.common.exceptions import (
Expand All @@ -21,14 +21,12 @@
UnknownMethodException,
WebDriverException,
)
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.command import Command as RemoteCommand
from selenium.webdriver.remote.remote_connection import RemoteConnection
from typing_extensions import Self

from appium.common.logger import logger
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy

from .appium_connection import AppiumConnection
from .client_config import AppiumClientConfig
Expand Down Expand Up @@ -241,7 +239,7 @@ class WebDriver(
def __init__(
self,
command_executor: Union[str, AppiumConnection] = 'http://127.0.0.1:4723',
extensions: Optional[List['WebDriver']] = None,
extensions: Optional[List[Type['ExtensionBase']]] = None,
options: Union[AppiumOptions, List[AppiumOptions], None] = None,
client_config: Optional[AppiumClientConfig] = None,
):
Expand All @@ -250,28 +248,22 @@ def __init__(
)
super().__init__(
command_executor=command_executor,
options=options,
options=options, # type: ignore[arg-type]
locator_converter=AppiumLocatorConverter(),
web_element_cls=MobileWebElement,
client_config=client_config,
)

# to explicitly set type after the initialization
self.command_executor: RemoteConnection

self._add_commands()

self.error_handler = MobileErrorHandler()

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

# add new method to the `find_by_*` pantheon
By.IOS_PREDICATE = AppiumBy.IOS_PREDICATE
By.IOS_CLASS_CHAIN = AppiumBy.IOS_CLASS_CHAIN
By.ANDROID_UIAUTOMATOR = AppiumBy.ANDROID_UIAUTOMATOR
By.ANDROID_VIEWTAG = AppiumBy.ANDROID_VIEWTAG
By.ACCESSIBILITY_ID = AppiumBy.ACCESSIBILITY_ID
By.IMAGE = AppiumBy.IMAGE
By.CUSTOM = AppiumBy.CUSTOM

self._absent_extensions: Set[str] = set()

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

if TYPE_CHECKING:

def find_element(self, by: str, value: Union[str, Dict, None] = None) -> 'MobileWebElement': # type: ignore[override]
...

def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> List['MobileWebElement']: # type: ignore[override]
...

def delete_extensions(self) -> None:
"""Delete extensions added in the class with 'setattr'"""
for extension in self._extensions:
Expand Down
12 changes: 10 additions & 2 deletions appium/webdriver/webelement.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Callable, Dict, Optional, Union
from typing import TYPE_CHECKING, Callable, Dict, Optional, Union

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

if TYPE_CHECKING:

def find_element(self, by: str, value: Union[str, Dict, None] = None) -> Self: # type: ignore[override]
...

def find_elements(self, by: str, value: Union[str, Dict, None] = None) -> Self: # type: ignore[override]
...

def get_attribute(self, name: str) -> Optional[Union[str, Dict]]: # type: ignore[override]
"""Gets the given attribute or property of the element.

Expand Down Expand Up @@ -108,7 +116,7 @@ def location_in_view(self) -> Dict[str, int]:
return self._execute(Command.LOCATION_IN_VIEW)['value']

# Override
def send_keys(self, *value: str) -> Self:
def send_keys(self, *value: str) -> Self: # type: ignore[override]
"""Simulates typing into the element.

Args:
Expand Down