Skip to content

api!: Merge RendererBase class into Renderer #1032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jan 19, 2024
Merged
Prev Previous commit
Next Next commit
Use new and more TypeVars within @suspend_display.
  • Loading branch information
schloerke committed Jan 18, 2024
commit 6d3ff405f8d00fdde32bf7cdf5b5b7c39d900190
25 changes: 11 additions & 14 deletions shiny/express/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
import contextlib
import sys
from contextlib import AbstractContextManager
from typing import Callable, Generator, TypeVar, overload
from typing import Callable, Generator, TypeVar, cast, overload

from .. import ui
from .._typing_extensions import ParamSpec
from ..render.renderer import RendererBase, RendererBaseT
from ..render.renderer import Renderer, RendererT

__all__ = ("suspend_display",)

P = ParamSpec("P")
R = TypeVar("R")
CallableT = TypeVar("CallableT", bound=Callable[..., object])


# TODO-barret-future; quartodoc entry?
def output_args(
**kwargs: object,
) -> Callable[[RendererBaseT], RendererBaseT]:
) -> Callable[[RendererT], RendererT]:
"""
Sets default UI arguments for a Shiny rendering function.

Expand All @@ -41,20 +38,20 @@ def output_args(
A decorator that sets the default UI arguments for a Shiny rendering function.
"""

def wrapper(renderer: RendererBaseT) -> RendererBaseT:
def wrapper(renderer: RendererT) -> RendererT:
renderer._auto_output_ui_kwargs = kwargs
return renderer

return wrapper


@overload
def suspend_display(fn: CallableT) -> CallableT:
def suspend_display(fn: RendererT) -> RendererT:
...


@overload
def suspend_display(fn: RendererBaseT) -> RendererBaseT:
def suspend_display(fn: CallableT) -> CallableT:
...


Expand All @@ -64,8 +61,8 @@ def suspend_display() -> AbstractContextManager[None]:


def suspend_display(
fn: Callable[P, R] | RendererBaseT | None = None
) -> Callable[P, R] | RendererBaseT | AbstractContextManager[None]:
fn: RendererT | CallableT | None = None,
) -> RendererT | CallableT | AbstractContextManager[None]:
"""Suppresses the display of UI elements in various ways.

If used as a context manager (`with suspend_display():`), it suppresses the display
Expand Down Expand Up @@ -99,12 +96,12 @@ def suspend_display(
if fn is None:
return suspend_display_ctxmgr()

# Special case for RendererBase; when we decorate those, we just mean "don't
# Special case for Renderer; when we decorate those, we just mean "don't
# display yourself"
if isinstance(fn, RendererBase):
if isinstance(fn, Renderer):
# By setting the class value, the `self` arg will be auto added.
fn.auto_output_ui = null_ui
return fn
return cast(RendererT, fn)

return suspend_display_ctxmgr()(fn)

Expand Down