1111from pandas .api .types import infer_dtype
1212
1313import altair as alt
14+ from altair .utils import core
1415from altair .utils .core import infer_encoding_types , parse_shorthand , update_nested
1516from 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+
276292def _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
305328def test_infer_encoding_types_with_condition ():
0 commit comments