From dc47946f33a6a2c5f5dbdd552f75ea3014ea49a3 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 2 Oct 2024 00:14:31 +0100 Subject: [PATCH] tests: test new style list/tuple/optional annotations --- src/cachew/marshall/cachew.py | 14 ++++++++++---- src/cachew/pytest.py | 1 + src/cachew/tests/marshall.py | 1 + 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/cachew/marshall/cachew.py b/src/cachew/marshall/cachew.py index 9efb19e..22a54d0 100644 --- a/src/cachew/marshall/cachew.py +++ b/src/cachew/marshall/cachew.py @@ -116,7 +116,7 @@ def load(self, dct): @dataclass(**SLOTS) class SUnion(Schema): - # it's a bit faster to cache indixes here, gives about 15% speedup + # it's a bit faster to cache indices here, gives about 15% speedup args: tuple[tuple[int, Schema], ...] def dump(self, obj): @@ -442,18 +442,24 @@ def test_serialize_and_deserialize() -> None: helper('aaa', Optional[str]) helper('aaa', Union[str, None]) helper(None, Union[str, None]) + if sys.version_info[:2] >= (3, 10): + helper('aaa', str | None) - # lists - helper([1, 2, 3], List[int]) + # lists/tuples/sequences helper([1, 2, 3], List[int]) helper([1, 2, 3], Sequence[int], expected=(1, 2, 3)) helper((1, 2, 3), Sequence[int]) helper((1, 2, 3), Tuple[int, int, int]) - helper((1, 2, 3), Tuple[int, int, int]) + if sys.version_info[:2] >= (3, 9): + # TODO test with from __future__ import annotations.. + helper([1, 2, 3], list[int]) + helper((1, 2, 3), tuple[int, int, int]) # dicts helper({'a': 'aa', 'b': 'bb'}, Dict[str, str]) helper({'a': None, 'b': 'bb'}, Dict[str, Optional[str]]) + if sys.version_info[:2] >= (3, 9): + helper({'a': 'aa', 'b': 'bb'}, dict[str, str]) # compounds of simple types helper(['1', 2, '3'], List[Union[str, int]]) diff --git a/src/cachew/pytest.py b/src/cachew/pytest.py index c4c7190..ff83bae 100644 --- a/src/cachew/pytest.py +++ b/src/cachew/pytest.py @@ -1,6 +1,7 @@ """ Helpers to prevent depending on pytest in runtime """ + import sys import typing diff --git a/src/cachew/tests/marshall.py b/src/cachew/tests/marshall.py index 5d5ff9b..ab6db77 100644 --- a/src/cachew/tests/marshall.py +++ b/src/cachew/tests/marshall.py @@ -45,6 +45,7 @@ def do_test(*, test_name: str, Type, factory, count: int, impl: Impl = 'cachew') from_json = marshall.load elif impl == 'legacy': from ..legacy import NTBinder + # NOTE: legacy binder emits a tuple which can be inserted directly into the database # so 'json dump' and 'json load' should really be disregarded for this flavor # if you're comparing with implementation, you should compare