From f6f478bddbbe467efa41ea861695e184a2d26c0b Mon Sep 17 00:00:00 2001 From: KamenDimitrov97 Date: Mon, 2 Sep 2024 10:55:10 +0300 Subject: [PATCH] chore: Refactored render.py:get_render_method for better readability, added explanitory comments --- docs/renderer_tutorial.md | 3 --- src/dewret/annotations.py | 13 ++++++------- src/dewret/backends/backend_dask.py | 20 ++------------------ src/dewret/core.py | 5 ++++- src/dewret/render.py | 21 +++++++++++---------- 5 files changed, 23 insertions(+), 39 deletions(-) diff --git a/docs/renderer_tutorial.md b/docs/renderer_tutorial.md index f046b6ea..f4d0f702 100644 --- a/docs/renderer_tutorial.md +++ b/docs/renderer_tutorial.md @@ -416,6 +416,3 @@ RawType = BasicType | list[str] | list["RawType"] | dict[str, "RawType"] ```shell python snakemake_tasks.py ``` - -### Q: Should I add a brief description of dewret in step 1? Should link dewret types/docs etc here? -### A: Get details on how that happens and probably yes. diff --git a/src/dewret/annotations.py b/src/dewret/annotations.py index 62a9ee36..a541ec0d 100644 --- a/src/dewret/annotations.py +++ b/src/dewret/annotations.py @@ -46,13 +46,12 @@ def __init__(self, fn: Callable[..., Any]): If `fn` is a class, it takes the constructor, and if it is a method, it takes the `__func__` attribute. """ - self.fn = ( - fn.__init__ - if inspect.isclass(fn) else - fn.__func__ - if inspect.ismethod(fn) else - fn - ) + if inspect.isclass(fn): + self.fn = fn.__init__ + elif inspect.ismethod(fn): + self.fn = fn.__func__ + else: + self.fn = fn @property def return_type(self) -> Any: diff --git a/src/dewret/backends/backend_dask.py b/src/dewret/backends/backend_dask.py index cc22ae34..06ae47f0 100644 --- a/src/dewret/backends/backend_dask.py +++ b/src/dewret/backends/backend_dask.py @@ -105,26 +105,10 @@ def run(workflow: Workflow | None, task: Lazy | list[Lazy] | tuple[Lazy], thread Args: workflow: `Workflow` in which to record the execution. task: `dask.delayed` function, wrapped by dewret, that we wish to compute. - thread_pool: thread pool for executing the workflows, to allow initialization of configuration contextvars. + thread_pool: custom thread pool for executing workflows, copies in correct values for contextvars to each thread before they are accessed by a dask worker. **kwargs: any configuration arguments for this backend. """ - # def _check_delayed(task: Lazy | list[Lazy] | tuple[Lazy]) -> Delayed: - # # We need isinstance to reassure type-checker. - # if isinstance(task, list) or isinstance(task, tuple): - # lst: list[Delayed] | tuple[Delayed, ...] = [_check_delayed(elt) for elt in task] - # if isinstance(task, tuple): - # lst = tuple(lst) - # return delayed(lst) - # elif not isinstance(task, Delayed) or not is_lazy(task): - # raise RuntimeError( - # f"{task} is not a dask delayed, perhaps you tried to mix backends?" - # ) - # return task - # computable = _check_delayed(task) - # if not is_lazy(task): - # raise RuntimeError( - # f"{task} is not a dask delayed, perhaps you tried to mix backends?" - # ) + # def _check_delayed was here, but we decided to delegate this to dask if isinstance(task, Delayed) and is_lazy(task): computable = task diff --git a/src/dewret/core.py b/src/dewret/core.py index 72be23ae..fbc575f2 100644 --- a/src/dewret/core.py +++ b/src/dewret/core.py @@ -48,7 +48,9 @@ BasicType = str | float | bool | bytes | int | None RawType = BasicType | list["RawType"] | dict[str, "RawType"] FirmType = RawType | list["FirmType"] | dict[str, "FirmType"] | tuple["FirmType", ...] -ExprType = (FirmType | Basic | list["ExprType"] | dict[str, "ExprType"] | tuple["ExprType", ...]) # type: ignore +ExprType = ( + FirmType | Basic | list["ExprType"] | dict[str, "ExprType"] | tuple["ExprType", ...] +) # type: ignore U = TypeVar("U") T = TypeVar("T") @@ -71,6 +73,7 @@ def strip_annotations(parent_type: type) -> tuple[type, tuple[str]]: return parent_type, tuple(metadata) +# Generic type for configuration settings for the renderer RenderConfiguration = dict[str, RawType] diff --git a/src/dewret/render.py b/src/dewret/render.py index 31b9e3da..f2fc3e3d 100644 --- a/src/dewret/render.py +++ b/src/dewret/render.py @@ -66,19 +66,20 @@ def get_render_method(renderer: Path | RawRenderModule | StructuredRenderModule, render_module = cast(BaseRenderModule, module) else: render_module = renderer - if not isinstance(render_module, StructuredRenderModule): - if not isinstance(render_module, RawRenderModule): - raise NotImplementedError("This render module neither seems to be a structured nor a raw render module.") + + if isinstance(render_module, RawRenderModule): return render_module.render_raw + elif isinstance(render_module, (StructuredRenderModule)): + def _render(workflow: Workflow, render_module: StructuredRenderModule, pretty: bool=False, **kwargs: RenderConfiguration) -> dict[str, str]: + rendered = render_module.render(workflow, **kwargs) + return { + key: structured_to_raw(value, pretty=pretty) + for key, value in rendered.items() + } - def _render(workflow: Workflow, render_module: StructuredRenderModule, pretty: bool=False, **kwargs: RenderConfiguration) -> dict[str, str]: - rendered = render_module.render(workflow, **kwargs) - return { - key: structured_to_raw(value, pretty=pretty) - for key, value in rendered.items() - } + return cast(RenderCall, partial(_render, render_module=render_module, pretty=pretty)) - return cast(RenderCall, partial(_render, render_module=render_module, pretty=pretty)) + raise NotImplementedError("This render module neither seems to be a structured nor a raw render module.") def base_render( workflow: Workflow, build_cb: Callable[[Workflow], T]