Skip to content

Commit

Permalink
tests: test new style list/tuple/optional annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
karlicoss committed Oct 2, 2024
1 parent 5c93e28 commit c39d812
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/cachew/marshall/cachew.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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]])
Expand Down
1 change: 1 addition & 0 deletions src/cachew/pytest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Helpers to prevent depending on pytest in runtime
"""

import sys
import typing

Expand Down
1 change: 1 addition & 0 deletions src/cachew/tests/marshall.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <other> implementation, you should compare
Expand Down

0 comments on commit c39d812

Please sign in to comment.