SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.18k stars 8.11k forks source link

[🐛 Bug]: mypy detects incompatible types #13544

Open kellypilot opened 7 months ago

kellypilot commented 7 months ago

What happened?

When checking types with mypy v1.8.0, incompatible types are detected

url_to_be is declared as def url_to_be(url: str) -> Callable[[WebDriver], bool]:

but any_of requires other argument type def any_of(*expected_conditions: Callable[[D], T]) -> Callable[[D], Union[Literal[False], T]]:

How can we reproduce the issue?

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

self.__waiter: WebDriverWait
self.__data_url: str

elements = self.__waiter.until(
    EC.any_of(
        EC.visibility_of_any_elements_located((
            By.CSS_SELECTOR,
            "p.3yeW3"
        )),
        # mypy error: Argument 2 to "any_of" has incompatible type
        # "Callable[[WebDriver], bool]";
        # expected "Callable[[Any], Literal[False] | list[WebElement] | WebElement]"
        EC.url_to_be(self.__data_url)
    )
)

Relevant log output

error: Argument 2 to "any_of" has incompatible type "Callable[[WebDriver], bool]"; expected "Callable[[Any], Literal[False] | list[WebElement] | WebElement]"  [arg-type]

Operating System

Ubuntu

Selenium version

4.17.2

What are the browser(s) and version(s) where you see this issue?

does not matter

What are the browser driver(s) and version(s) where you see this issue?

does not matter

Are you using Selenium Grid?

No response

github-actions[bot] commented 7 months ago

@kellypilot, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

github-actions[bot] commented 7 months ago

This issue is looking for contributors.

Please comment below or reach out to us through our IRC/Slack/Matrix channels if you are interested.

titusfortner commented 7 months ago

Thanks for reporting. Please feel free to submit a PR.

navin772 commented 1 month ago

@titusfortner I have raised a PR for this issue, can you please review it?

navin772 commented 1 month ago

@kellypilot can you give the full executable code to reproduce the error? If I run mypy on the above code, I just get error: Name "self" is not defined error.