Skip to content

Commit

Permalink
refactor: Rename theme.themes -> theme._themes
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned committed Oct 18, 2024
1 parent 4fe4321 commit 2b3eb35
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion altair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ def __getattr__(name: str) -> _Any:
alternative="altair.theme",
stacklevel=3,
)
return theme.themes
return theme._themes
else:
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)
21 changes: 10 additions & 11 deletions altair/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
ViewBackgroundKwds,
ViewConfigKwds,
)
from altair.vegalite.v5.theme import themes
from altair.vegalite.v5.theme import themes as _themes

if TYPE_CHECKING:
import sys
Expand Down Expand Up @@ -174,7 +174,6 @@
"names",
"options",
"register",
"themes",
"unregister",
]

Expand Down Expand Up @@ -238,9 +237,9 @@ def custom_theme() -> theme.ThemeConfig:
# HACK: See for `LiteralString` requirement in `name`
# https://github.com/vega/altair/pull/3526#discussion_r1743350127
def decorate(func: Plugin[ThemeConfig], /) -> Plugin[ThemeConfig]:
themes.register(name, func)
_themes.register(name, func)
if enable:
themes.enable(name)
_themes.enable(name)

@_wraps(func)
def wrapper(*args: P.args, **kwargs: P.kwargs) -> ThemeConfig:
Expand All @@ -258,14 +257,14 @@ def unregister(name: LiteralString) -> Plugin[ThemeConfig]:
Parameters
----------
name
Unique name assigned in ``alt.theme.themes``.
Unique name assigned during ``alt.theme.register``.
Raises
------
TypeError
When ``name`` has not been registered.
"""
plugin = themes.register(name, None)
plugin = _themes.register(name, None)
if plugin is None:
msg = (
f"Found no theme named {name!r} in registry.\n"
Expand All @@ -277,9 +276,9 @@ def unregister(name: LiteralString) -> Plugin[ThemeConfig]:
return plugin


enable = themes.enable
get = themes.get
names = themes.names
enable = _themes.enable
get = _themes.get
names = _themes.names
active: str
"""Return the name of the currently active theme."""
options: dict[str, Any]
Expand All @@ -296,9 +295,9 @@ def __getattr__(name: Literal["active"]) -> str: ... # type: ignore[misc]
def __getattr__(name: Literal["options"]) -> dict[str, Any]: ... # type: ignore[misc]
def __getattr__(name: str) -> Any:
if name == "active":
return themes.active
return _themes.active
elif name == "options":
return themes.options
return _themes.options
else:
msg = f"module {__name__!r} has no attribute {name!r}"
raise AttributeError(msg)
1 change: 0 additions & 1 deletion doc/user_guide/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ Theme
names
options
register
themes
unregister
ThemeConfig
AreaConfigKwds
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ def test_deprecated_import():

with catch_warnings():
filterwarnings("ignore", category=AltairDeprecationWarning)
assert alt.themes == alt.theme.themes
assert alt.themes == alt.theme._themes
12 changes: 6 additions & 6 deletions tests/vegalite/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def basic_spec():


def make_final_spec(alt, basic_spec):
from altair.theme import themes
from altair.theme import _themes

theme = themes.get()
theme = _themes.get()
assert theme
spec = theme()
spec.update(basic_spec)
Expand Down Expand Up @@ -70,12 +70,12 @@ def test_basic_chart_from_dict(alt, basic_spec):

@pytest.mark.parametrize("alt", [v5])
def test_theme_enable(alt, basic_spec):
from altair.theme import themes
from altair.theme import _themes

active_theme = themes.active
active_theme = _themes.active

try:
themes.enable("none")
_themes.enable("none")

chart = alt.Chart.from_dict(basic_spec)
dct = chart.to_dict()
Expand All @@ -88,7 +88,7 @@ def test_theme_enable(alt, basic_spec):
assert dct == basic_spec
finally:
# reset the theme to its initial value
themes.enable(active_theme) # pyright: ignore[reportArgumentType]
_themes.enable(active_theme) # pyright: ignore[reportArgumentType]


@pytest.mark.parametrize("alt", [v5])
Expand Down
6 changes: 3 additions & 3 deletions tests/vegalite/v5/test_theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ def test_theme_register_decorator() -> None:
def custom_theme() -> ThemeConfig:
return {"height": 400, "width": 700}

assert theme.themes.active == "unique name" == theme.active
registered = theme.themes.get()
assert theme._themes.active == "unique name" == theme.active
registered = theme._themes.get()
assert registered is not None
assert registered == theme.get()
assert registered() == {"height": 400, "width": 700} == custom_theme()
Expand All @@ -93,7 +93,7 @@ def custom_theme() -> ThemeConfig:
assert theme.active == "big square"
fn = theme.unregister("big square")
assert fn() == custom_theme()
assert theme.active == theme.themes.active
assert theme.active == theme._themes.active
# BUG: https://github.com/vega/altair/issues/3619
# assert theme.active != "big square"

Expand Down

0 comments on commit 2b3eb35

Please sign in to comment.