diff --git a/my/core/serialize.py b/my/core/serialize.py index b6ef232..93dba36 100644 --- a/my/core/serialize.py +++ b/my/core/serialize.py @@ -37,8 +37,8 @@ def _default_encode(obj: Any) -> Any: # could possibly run multiple times/raise warning if you provide different 'default' # functions or change the kwargs? The alternative is to maintain all of this at the module -# level, which is just as annoying. Maybe should be increased to lru_cache(16) or something...? -@lru_cache(1) +# level, which is just as annoying +@lru_cache(maxsize=None) def _dumps_factory(**kwargs) -> Callable[[Any], str]: use_default: DefaultEncoder = _default_encode # if the user passed an additional 'default' parameter, @@ -118,6 +118,25 @@ def dumps( return _dumps_factory(default=default, **kwargs)(obj) +def test_serialize_fallback() -> None: + import json as jsn # dont cause possible conflicts with module code + + import pytest + + # cant use a namedtuple here, since the default json.dump serializer + # serializes namedtuples as tuples, which become arrays + # just test with an array of mixed objects + X = [5, 5.0] + + # ignore warnings. depending on test order, + # the lru_cache'd warning may have already been sent, + # so checking may be nondeterministic? + with pytest.warns(None): + res = jsn.loads(dumps(X)) + assert res == X + + + def test_nt_serialize() -> None: import json as jsn # dont cause possible conflicts with module code import orjson # import to make sure this is installed diff --git a/tests/core.py b/tests/core.py index 52ff688..95d30b7 100644 --- a/tests/core.py +++ b/tests/core.py @@ -18,3 +18,4 @@ from my.core.util import * from my.core.discovery_pure import * from my.core.types import * from my.core.stats import * +from my.core.serialize import test_serialize_fallback