add fallback test, make dumps factory unbounded
This commit is contained in:
parent
d4c5ccab42
commit
7d07b9601a
2 changed files with 22 additions and 2 deletions
|
@ -37,8 +37,8 @@ def _default_encode(obj: Any) -> Any:
|
||||||
|
|
||||||
# could possibly run multiple times/raise warning if you provide different 'default'
|
# 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
|
# 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...?
|
# level, which is just as annoying
|
||||||
@lru_cache(1)
|
@lru_cache(maxsize=None)
|
||||||
def _dumps_factory(**kwargs) -> Callable[[Any], str]:
|
def _dumps_factory(**kwargs) -> Callable[[Any], str]:
|
||||||
use_default: DefaultEncoder = _default_encode
|
use_default: DefaultEncoder = _default_encode
|
||||||
# if the user passed an additional 'default' parameter,
|
# if the user passed an additional 'default' parameter,
|
||||||
|
@ -118,6 +118,25 @@ def dumps(
|
||||||
return _dumps_factory(default=default, **kwargs)(obj)
|
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:
|
def test_nt_serialize() -> None:
|
||||||
import json as jsn # dont cause possible conflicts with module code
|
import json as jsn # dont cause possible conflicts with module code
|
||||||
import orjson # import to make sure this is installed
|
import orjson # import to make sure this is installed
|
||||||
|
|
|
@ -18,3 +18,4 @@ from my.core.util import *
|
||||||
from my.core.discovery_pure import *
|
from my.core.discovery_pure import *
|
||||||
from my.core.types import *
|
from my.core.types import *
|
||||||
from my.core.stats import *
|
from my.core.stats import *
|
||||||
|
from my.core.serialize import test_serialize_fallback
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue