From ea91e1b23eb6fdbbb563b80628993bf30ce03b40 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Thu, 14 Dec 2023 21:16:34 -0500 Subject: [PATCH 1/2] fix(layout_columns): Remove use of enum for breakpoints --- shiny/ui/_layout_columns.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/shiny/ui/_layout_columns.py b/shiny/ui/_layout_columns.py index 77e551a8d..8050838c8 100644 --- a/shiny/ui/_layout_columns.py +++ b/shiny/ui/_layout_columns.py @@ -1,7 +1,7 @@ from __future__ import annotations from enum import Enum -from typing import Dict, Iterable, Optional, TypeVar, Union, cast +from typing import Dict, Iterable, Literal, Optional, TypeVar, Union, cast from warnings import warn from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, css @@ -15,19 +15,16 @@ T = TypeVar("T") -class Breakpoints(Enum): - """ - References - ---------- - * [Available Bootstrap breakpoints](https://getbootstrap.com/docs/5.3/layout/breakpoints/#available-breakpoints) - """ +Breakpoints = Literal["xs", "sm", "md", "lg", "xl", "xxl"] +""" +References +---------- +* [Available Bootstrap breakpoints](https://getbootstrap.com/docs/5.3/layout/breakpoints/#available-breakpoints) +""" + - xs = "xs" - sm = "sm" - md = "md" - lg = "lg" - xl = "xl" - xxl = "xxl" +def bs_breakpoints() -> Iterable[Breakpoints]: + return ("xs", "sm", "md", "lg", "xl", "xxl") BreakpointsSoft = Dict[Breakpoints, Union[Iterable[T], T, None]] @@ -155,16 +152,15 @@ def as_col_spec( return None if not isinstance(col_widths, Dict): - return {Breakpoints.md: validate_col_width(col_widths, n_kids, Breakpoints.md)} + return {"md": validate_col_width(col_widths, n_kids, "md")} ret: BreakpointsOptional[int] = {} col_widths_items = cast(BreakpointsSoft[int], col_widths).items() for brk, value in col_widths_items: - bs_breakpoints = [str(bp.value) for bp in Breakpoints] - if str(brk) not in bs_breakpoints: + if brk not in bs_breakpoints(): raise ValueError( - f"Breakpoint '{brk}' is not valid. Valid breakpoints are: {', '.join(bs_breakpoints)}'." + f"Breakpoint '{brk}' is not valid. Valid breakpoints are: {', '.join(bs_breakpoints())}'." ) if value is None: @@ -266,9 +262,7 @@ def row_heights_attrs( # row height is derived from xs or defaults to auto in the CSS, so we don't need the # class to activate it classes = [ - f"bslib-grid--row-heights--{brk}" - for brk in x_complete.keys() - if brk != Breakpoints.xs + f"bslib-grid--row-heights--{brk}" for brk in x_complete.keys() if brk != "xs" ] # Create CSS variables, treating numeric values as fractional units, passing strings From 0ee68cdb13fa035a7788865273f5517b9311bbe3 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Fri, 15 Dec 2023 08:56:35 -0500 Subject: [PATCH 2/2] refactor: Singular `Breakpoint` and store `breakpoints` in a variable, not function --- shiny/ui/_layout_columns.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/shiny/ui/_layout_columns.py b/shiny/ui/_layout_columns.py index 8050838c8..7bd2394db 100644 --- a/shiny/ui/_layout_columns.py +++ b/shiny/ui/_layout_columns.py @@ -1,7 +1,7 @@ from __future__ import annotations from enum import Enum -from typing import Dict, Iterable, Literal, Optional, TypeVar, Union, cast +from typing import Dict, Iterable, Literal, Optional, Tuple, TypeVar, Union, cast from warnings import warn from htmltools import Tag, TagAttrs, TagAttrValue, TagChild, css @@ -15,7 +15,7 @@ T = TypeVar("T") -Breakpoints = Literal["xs", "sm", "md", "lg", "xl", "xxl"] +Breakpoint = Literal["xs", "sm", "md", "lg", "xl", "xxl"] """ References ---------- @@ -23,13 +23,12 @@ """ -def bs_breakpoints() -> Iterable[Breakpoints]: - return ("xs", "sm", "md", "lg", "xl", "xxl") +breakpoints: Tuple[Breakpoint, ...] = ("xs", "sm", "md", "lg", "xl", "xxl") -BreakpointsSoft = Dict[Breakpoints, Union[Iterable[T], T, None]] -BreakpointsOptional = Dict[Breakpoints, Union[Iterable[T], None]] -BreakpointsComplete = Dict[Breakpoints, Iterable[T]] +BreakpointsSoft = Dict[Breakpoint, Union[Iterable[T], T, None]] +BreakpointsOptional = Dict[Breakpoint, Union[Iterable[T], None]] +BreakpointsComplete = Dict[Breakpoint, Iterable[T]] BreakpointsUser = Union[BreakpointsSoft[T], Iterable[T], T, None] @@ -158,9 +157,9 @@ def as_col_spec( col_widths_items = cast(BreakpointsSoft[int], col_widths).items() for brk, value in col_widths_items: - if brk not in bs_breakpoints(): + if brk not in breakpoints: raise ValueError( - f"Breakpoint '{brk}' is not valid. Valid breakpoints are: {', '.join(bs_breakpoints())}'." + f"Breakpoint '{brk}' is not valid. Valid breakpoints are: {', '.join(breakpoints)}'." ) if value is None: @@ -176,7 +175,7 @@ def as_col_spec( def validate_col_width( - x: Iterable[int] | int, n_kids: int, break_name: Breakpoints + x: Iterable[int] | int, n_kids: int, break_name: Breakpoint ) -> Iterable[int]: if isinstance(x, int): y = [x]