Skip to content

Commit 1f13e89

Browse files
committed
Add theme color attrs for stronger typing; v0.5.1
1 parent 11c3d0a commit 1f13e89

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to `shinyswatch` will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [0.5.1] - 2024-03-07
9+
10+
* Add typed attributes in the theme's color class for stronger type checking.
11+
812
## [0.5.0] - 2024-03-07
913

1014
### Breaking changes

shinyswatch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Bootswatch + Bootstrap 5 themes for Shiny"""
22

3-
__version__ = "0.5.0"
3+
__version__ = "0.5.1"
44

55
from . import theme
66
from ._get_theme import get_theme

shinyswatch/_theme_utils.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,45 @@
88

99

1010
class ThemeColors:
11-
def __init__(self, name: BSW5_THEME_NAME) -> None:
12-
self._name = name
13-
self._colors: dict[str, str] = bsw5_theme_colors[name]
14-
15-
def __getattr__(self, key: str) -> str:
16-
return self._colors[key]
11+
# Leave type definitions for pyright type stubs; Do not set values here
12+
# Leave type definintions here for completions w/ static type checker
13+
primary: str
14+
secondary: str
15+
success: str
16+
danger: str
17+
warning: str
18+
info: str
19+
light: str
20+
dark: str
21+
body_color: str
22+
body_bg: str
1723

18-
def __getitem__(self, key: str) -> str:
19-
return self._colors[key]
24+
def __init__(self, name: BSW5_THEME_NAME) -> None:
25+
colors = bsw5_theme_colors[name]
26+
_color_names: list[str] = list(colors.keys())
27+
_color_name_len = max(len(x) for x in _color_names)
2028

21-
def __dir__(self) -> list[str]:
22-
return list(self._colors.keys())
29+
self._name = name
30+
self._color_names = _color_names
31+
self._color_name_len = _color_name_len
32+
for k, v in colors.items():
33+
setattr(self, k, v)
2334

2435
def __repr__(self) -> str:
25-
colors = [f"{k:11}: {v}" for k, v in self._colors.items()]
36+
colors = [
37+
f"{k:{self._color_name_len + 1}}: {getattr(self, k)}"
38+
for k in self._color_names
39+
]
2640
colors = "\n ".join(colors)
2741
ret = [f"<ThemeColors({self._name!r}):", " " + colors, ">"]
2842
return "\n".join(ret)
2943

3044

3145
class ShinyswatchTheme(Tagifiable):
46+
# Leave type definitions for pyright type stubs; Do not set values here
47+
name: BSW5_THEME_NAME
48+
colors: ThemeColors
49+
3250
def __init__(self, name: BSW5_THEME_NAME) -> None:
3351
assert_theme(name=name)
3452
self.name: BSW5_THEME_NAME = name

0 commit comments

Comments
 (0)