Skip to content

Commit 5207768

Browse files
authored
refactor: Remove channels parameter in infer_encoding_types (#3564)
* test: Monkeypatch channels global Removes the dependency in `test_infer_encoding_types` * refactor: Remove `channels` parameter in `infer_encoding_types` Was kept, but only needed for tests since #3444. As `infer_encoding_types` is not public API - this is a safe remove, no need for deprecation
1 parent db97a0b commit 5207768

File tree

2 files changed

+31
-33
lines changed

2 files changed

+31
-33
lines changed

altair/utils/core.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
if TYPE_CHECKING:
3535
import typing as t
36-
from types import ModuleType
3736

3837
import pandas as pd
3938
from narwhals.typing import IntoExpr
@@ -812,22 +811,6 @@ class _ChannelCache:
812811
channel_to_name: dict[type[SchemaBase], str]
813812
name_to_channel: dict[str, dict[_ChannelType, type[SchemaBase]]]
814813

815-
@classmethod
816-
def from_channels(cls, channels: ModuleType, /) -> _ChannelCache:
817-
# - This branch is only kept for tests that depend on mocking `channels`.
818-
# - No longer needs to pass around `channels` reference and rebuild every call.
819-
c_to_n = {
820-
c: c._encoding_name
821-
for c in channels.__dict__.values()
822-
if isinstance(c, type)
823-
and issubclass(c, SchemaBase)
824-
and hasattr(c, "_encoding_name")
825-
}
826-
self = cls.__new__(cls)
827-
self.channel_to_name = c_to_n
828-
self.name_to_channel = _invert_group_channels(c_to_n)
829-
return self
830-
831814
@classmethod
832815
def from_cache(cls) -> _ChannelCache:
833816
global _CHANNEL_CACHE
@@ -925,9 +908,7 @@ def _reduce(it: Iterator[tuple[type[Any], str]]) -> Any:
925908
return {k: _reduce(chans) for k, chans in grouper}
926909

927910

928-
def infer_encoding_types(
929-
args: tuple[Any, ...], kwargs: dict[str, Any], channels: ModuleType | None = None
930-
):
911+
def infer_encoding_types(args: tuple[Any, ...], kwargs: dict[str, Any]):
931912
"""
932913
Infer typed keyword arguments for args and kwargs.
933914
@@ -937,20 +918,14 @@ def infer_encoding_types(
937918
Sequence of function args
938919
kwargs : MutableMapping
939920
Dict of function kwargs
940-
channels : ModuleType
941-
The module containing all altair encoding channel classes.
942921
943922
Returns
944923
-------
945924
kwargs : dict
946925
All args and kwargs in a single dict, with keys and types
947926
based on the channels mapping.
948927
"""
949-
cache = (
950-
_ChannelCache.from_channels(channels)
951-
if channels
952-
else _ChannelCache.from_cache()
953-
)
928+
cache = _ChannelCache.from_cache()
954929
# First use the mapping to convert args to kwargs based on their types.
955930
for arg in args:
956931
el = next(iter(arg), None) if isinstance(arg, (list, tuple)) else arg

tests/utils/test_core.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pandas.api.types import infer_dtype
1212

1313
import altair as alt
14+
from altair.utils import core
1415
from altair.utils.core import infer_encoding_types, parse_shorthand, update_nested
1516
from tests import skip_requires_pyarrow
1617

@@ -267,18 +268,40 @@ def test_update_nested():
267268

268269

269270
@pytest.fixture
270-
def channels():
271+
def channels() -> types.ModuleType:
271272
channels = types.ModuleType("channels")
272273
exec(FAKE_CHANNELS_MODULE, channels.__dict__)
273274
return channels
274275

275276

277+
@pytest.fixture
278+
def channels_cached(channels) -> core._ChannelCache:
279+
"""Previously ``_ChannelCache.from_channels``."""
280+
cached = core._ChannelCache.__new__(core._ChannelCache)
281+
cached.channel_to_name = {
282+
c: c._encoding_name
283+
for c in channels.__dict__.values()
284+
if isinstance(c, type)
285+
and issubclass(c, alt.SchemaBase)
286+
and hasattr(c, "_encoding_name")
287+
}
288+
cached.name_to_channel = core._invert_group_channels(cached.channel_to_name)
289+
return cached
290+
291+
276292
def _getargs(*args, **kwargs):
277293
return args, kwargs
278294

279295

280-
# NOTE: Dependent on a no longer needed implementation detail
281-
def test_infer_encoding_types(channels):
296+
def test_infer_encoding_types(
297+
monkeypatch: pytest.MonkeyPatch, channels, channels_cached
298+
):
299+
# Indirectly initialize `_CHANNEL_CACHE`
300+
infer_encoding_types((), {})
301+
# Replace with contents of `FAKE_CHANNELS_MODULE`
302+
# Scoped to only this test
303+
monkeypatch.setattr(core, "_CHANNEL_CACHE", channels_cached)
304+
282305
expected = {
283306
"x": channels.X("xval"),
284307
"y": channels.YValue("yval"),
@@ -289,17 +312,17 @@ def test_infer_encoding_types(channels):
289312
args, kwds = _getargs(
290313
channels.X("xval"), channels.YValue("yval"), channels.StrokeWidthValue(4)
291314
)
292-
assert infer_encoding_types(args, kwds, channels) == expected
315+
assert infer_encoding_types(args, kwds) == expected
293316

294317
# All keyword args
295318
args, kwds = _getargs(x="xval", y=alt.value("yval"), strokeWidth=alt.value(4))
296-
assert infer_encoding_types(args, kwds, channels) == expected
319+
assert infer_encoding_types(args, kwds) == expected
297320

298321
# Mixed positional & keyword
299322
args, kwds = _getargs(
300323
channels.X("xval"), channels.YValue("yval"), strokeWidth=alt.value(4)
301324
)
302-
assert infer_encoding_types(args, kwds, channels) == expected
325+
assert infer_encoding_types(args, kwds) == expected
303326

304327

305328
def test_infer_encoding_types_with_condition():

0 commit comments

Comments
 (0)