equinor / webviz-config

Make Dash applications from a user-friendly config file :book: :snake:
https://github.com/orgs/equinor/projects/24
MIT License
55 stars 39 forks source link

Improved yaml input conversion and generated json schema #357

Open anders-kiaer opened 3 years ago

anders-kiaer commented 3 years ago

webviz-config today supports the following:

Current solution however has some limitations/drawbacks:

Related issues:

Additional information:

⭑ Since webviz docs and webviz schema are user entrypoints, we are in control of import order of different packages. The following could be used in those subcommands to monkeypatch overload to do what we want. Prototype code:

class SomePlugin(WebvizPluginAbc):
    @typing.overload
    def __init__(response: None) -> None:
        ...
    @typing.overload
    def __init__(response: int) -> typing.Tuple[int, str]:
        ...
    @typing.overload
    def __init__(response: bytes) -> str:
        ...
    def __init__(response):
import inspect
import importlib
import typing
from webviz_config import WebvizPluginABC

overloaded_functions = []
def new_overload_func(func):
    overloaded_functions.append(func)

typing.overload = new_overload_func

from webviz_config import plugins

def check_overloaded_functions():
    for func in overloaded_functions:
        class_reference = getattr(inspect.getmodule(func), func.__qualname__.rsplit(".", 1)[0])
        if inspect.isclass(class_reference) and issubclass(class_reference, WebvizPluginABC):
            argspec = inspect.getfullargspec(func)
            print(argspec.annotations)
            print(class_reference)

check_overloaded_functions()

will give as output

{'return': None, 'response': None}
<class '[...].SomePlugin'>
{'return': typing.Tuple[int, str], 'response': <class 'int'>}
<class '[...].SomePlugin'>
{'return': <class 'str'>, 'response': <class 'bytes'>}
<class '[...].SomePlugin'>
anders-kiaer commented 3 years ago

Moving this to prioritized as it is important in order to prepare for #504.