dash icon indicating copy to clipboard operation
dash copied to clipboard

[Feature Request] `typing.Protocol` for `dash_duo`

Open janosh opened this issue 2 years ago • 4 comments

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

janosh avatar Jul 27 '22 18:07 janosh

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?

alexcjohnson avatar Aug 01 '22 12:08 alexcjohnson

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

janosh avatar Aug 01 '22 14:08 janosh

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

T4rk1n avatar Aug 01 '22 15:08 T4rk1n

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

janosh avatar Aug 01 '22 15:08 janosh

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

kostyafarber avatar Oct 10 '22 11:10 kostyafarber