add fallback test, make dumps factory unbounded

This commit is contained in:
seanbreckenridge 2021-03-19 17:18:30 -07:00
parent d4c5ccab42
commit 7d07b9601a
2 changed files with 22 additions and 2 deletions

View file

@ -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