plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.
https://plotly.com/dash
MIT License
21.53k stars 2.07k forks source link

provide `typing.Protocol` for `dash_duo` #2170

Open janosh opened 2 years ago

janosh commented 2 years ago

Is your feature request related to a problem? Please describe.

The DX for using the pytest fixture dash.testing.plugin.dash_duo() could be improved by providing a typing.Protocol to type hint available methods and their signatures.

Describe the solution you'd like

from typing import Protocol

import dash
from dash.testing.application_runners import import_app
from dash.testing.wait import wait_for_element_by_css_selector, wait_for_text_to_equal

class DashDuo(Protocol):
    "The dash_duo pytest fixture lives in dash.testing.plugin.dash_duo."

    def start_server(self, start_server) -> None:
        ...

    def find_element(self, selector: str) -> dash.development.base_component.Component:
        ...

def test_example_app(dash_duo: DashDuo):  # type hint for dash_duo enables auto-complete
    app = import_app("example")
    dash_duo.start_server(app)

    my_component = dash_duo.find_element("#hello-world")

    assert "my-value" == my_component.get_attribute("value")

    ....

Screenshot from VS Code showing auto-complete in action

Screen Shot 2022-07-27 at 11 11 43

Describe alternatives you've considered

None

alexcjohnson commented 2 years ago

Seems reasonable, @T4rk1n you've worked on typing for dash.testing a bit, any thoughts? Will it be possible to have this picked up automatically by VSCode when dash_duo is used as a pytest fixture, or will you still need to import a DashDuo and annotate every dash_duo?

janosh commented 2 years ago

You would have to annotate every single use of dash_duo unless you had some extension like pytest-fixtures installed.

T4rk1n commented 2 years ago

Pycharm pick it up by default, not a vscode user so I don't know about it, but seems like the plugin would work?

janosh commented 2 years ago

I made a rough start over here if you want to pick up from that. Or maybe DashDuo can be auto-generated?

kostyafarber commented 2 years ago

Hi this sounds interesting. Happy to pick it up with some guidance. Thanks.