From 95cc53e09ecd665230f98df86eb5f52ab2795237 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 26 Jun 2024 15:31:57 -0400 Subject: [PATCH] refactor: Improve/simplify `.to_css(compile_args=)` Co-authored-by: Carson Sievert --- shiny/ui/_theme.py | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/shiny/ui/_theme.py b/shiny/ui/_theme.py index 5225d8b1c..692e5e53e 100644 --- a/shiny/ui/_theme.py +++ b/shiny/ui/_theme.py @@ -5,17 +5,7 @@ import re import tempfile import textwrap -from typing import ( - Any, - Callable, - Iterable, - Literal, - Optional, - Sequence, - TypeVar, - Union, - cast, -) +from typing import Any, Literal, Optional, Sequence, TypeVar from htmltools import HTMLDependency @@ -32,13 +22,6 @@ T = TypeVar("T", bound="Theme") -SassImporterReturnValue = Union["tuple[str]", "tuple[str, str]", "tuple[str, str, str]"] -SassImporterFunction = Union[ - Callable[[str], SassImporterReturnValue], - Callable[[str, str], SassImporterReturnValue], -] - - class SassCompileArgs(TypedDict): output_style: NotRequired[Literal["nested", "expanded", "compact", "compressed"]] source_comments: NotRequired[bool] @@ -50,7 +33,7 @@ class SassCompileArgs(TypedDict): precision: NotRequired[int] custom_functions: NotRequired[Any] # not worth the effort, it's a complicated type indented: NotRequired[bool] - importers: NotRequired[Iterable[tuple[int, SassImporterFunction]] | None] + importers: NotRequired[Any] # not worth the effort, it's a complicated type theme_temporary_directories: set[tempfile.TemporaryDirectory[str]] = set() @@ -416,17 +399,21 @@ def to_css( check_libsass_installed() import sass - if compile_args is None: - compile_args = {"output_style": "compressed"} + args: SassCompileArgs = {} if compile_args is None else compile_args - self._css = cast( - str, - sass.compile( - string=self.to_sass(), - include_paths=self._include_paths, - **compile_args, # type: ignore - ), - ) + if "include_paths" in args: + raise ValueError( + "The 'include_paths' argument is not allowed in 'compile_args'. " + "Use the 'include_paths' argument of the Theme constructor instead.", + ) + + args: SassCompileArgs = { + "output_style": "compressed", + "include_paths": self._include_paths, + **args, + } + + self._css = sass.compile(string=self.to_sass(), **args) return self._css