You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mypy error "Incompatible return value type" on webdriver.find_element #1147
Mypy errors on the fact that appium.webdriver.WebDriver.find_element isn't overridden but indeed returns a different WebElement.
Reproducible Code
# no need to actually run this code, just send it through MyPy to see the error.fromappium.webdriver.common.appiumbyimportAppiumByfromtypingimportTYPE_CHECKINGifTYPE_CHECKING:
fromappium.webdriverimportRemotefromappium.webdriver.webelementimportWebElementdeffind_element(driver: Remote) ->WebElement:
# should get a mypy error on next linereturndriver.find_element(AppiumBy.ACCESSIBILITY_ID, "demo button")
deffind_elements(driver: Remote) ->WebElement:
# should get a mypy error on next linereturndriver.find_elements(AppiumBy.ACCESSIBILITY_ID, "demo confetti")
mypy output:
mypy .
screenpy_appium/target.py:89: error: Incompatible return value type (got "selenium.webdriver.remote.webelement.WebElement", expected "appium.webdriver.webelement.WebElement") [return-value]
screenpy_appium/target.py:98: error: Incompatible return value type (got "list[selenium.webdriver.remote.webelement.WebElement]", expected "list[appium.webdriver.webelement.WebElement]") [return-value]
Found 2 errors in 1 file (checked 35 source files)
Solution
Annotations need to be added to appium.webdriver.WebDriver
The inheritance of the protocol in this manner doesn't result in mypy understanding that the methods return the appium subclassed WebElement. As a result, the sample code in this issue still errors in mypy. (though, for a moment it seemed like clever way to solve the problem)
In this case, I think you have to include the TYPE_CHECKING logic.
Activity
KazuCocoa commentedon Jul 24, 2025
Hm, let me check. Here returns
appium.webdriver.webelement.WebElementas their types:python-client/appium/protocols/webdriver/can_find_elements.py
Lines 15 to 24 in d43a190
KazuCocoa commentedon Jul 24, 2025
What about this?
#1148
KazuCocoa commentedon Jul 24, 2025
Please try out
5.1.2bandophahita commentedon Jul 25, 2025
The inheritance of the protocol in this manner doesn't result in mypy understanding that the methods return the appium subclassed WebElement. As a result, the sample code in this issue still errors in mypy. (though, for a moment it seemed like clever way to solve the problem)
In this case, I think you have to include the TYPE_CHECKING logic.
KazuCocoa commentedon Aug 2, 2025
Hm, it looks like
Protocolpython-client/appium/protocols/webdriver/can_find_elements.py
Lines 15 to 24 in d43a190
KazuCocoa commentedon Aug 2, 2025
#1153
KazuCocoa commentedon Aug 7, 2025
5.1.3has the change. cc @bandophahita#1153
It has a change like you suggested.
Protocolusage for mypy needed some work, so the suggested one was the simplest so far.