Skip to content

Commit

Permalink
Default as a function (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTung97 committed Jan 8, 2024
1 parent 3661a65 commit fde7ff8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions memproxy/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import logging
from dataclasses import dataclass
from typing import Generic, TypeVar, Callable, Any, List, Optional, Dict, Type
from typing import Generic, TypeVar, Callable, List, Optional, Dict, Type

from .memproxy import LeaseGetResult
from .memproxy import Promise, Pipeline, Session
Expand Down Expand Up @@ -250,13 +250,13 @@ class _MultiGetFunc(Generic[T, K]):
_state: Optional[_MultiGetState[T, K]]
_fill_func: MultiGetFillFunc
_get_key_func: GetKeyFunc
_default: T
_default: Callable[[], T]

def __init__(
self,
fill_func: Callable[[List[K]], List[T]], # List[K] -> List[T]
key_func: Callable[[T], K], # T -> K
default: T,
default: Callable[[], T],
):
self._state = None
self._fill_func = fill_func
Expand All @@ -283,7 +283,7 @@ def resp_func() -> T:
state.completed = True
self._state = None

return state.result.get(key, self._default)
return state.result.get(key, self._default())

return resp_func

Expand All @@ -292,7 +292,7 @@ def resp_func() -> T:
def new_multi_get_filler(
fill_func: Callable[[List[K]], List[T]], # List[K] -> List[T]
get_key_func: Callable[[T], K], # T -> K
default: T,
default: Callable[[], T], # () -> T
) -> Callable[[K], Promise[T]]: # K -> () -> T
fn = _MultiGetFunc(fill_func=fill_func, key_func=get_key_func, default=default)
return fn.result_func
4 changes: 2 additions & 2 deletions test/proxy/test_proxy_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def new_user_item(self) -> Item[UserTest, int]:
filler = new_multi_get_filler(
fill_func=self.get_users,
get_key_func=UserTest.get_id,
default=UserTest(user_id=0, username='', counter=0),
default=lambda: UserTest(user_id=0, username='', counter=0),
)

return Item(
Expand Down Expand Up @@ -123,7 +123,7 @@ def key_fn(k: RoleKey) -> str:
filler = new_multi_get_filler(
fill_func=self.get_roles,
get_key_func=RoleTest.get_key,
default=RoleTest(tenant='', code='', name=''),
default=lambda: RoleTest(tenant='', code='', name=''),
)

return Item(
Expand Down
8 changes: 4 additions & 4 deletions test/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import unittest
from dataclasses import dataclass
from typing import List, Union
from typing import List, Union, Callable

import redis

Expand Down Expand Up @@ -397,11 +397,11 @@ def test_run_with_profiler(self) -> None:
class TestMultiGetFiller(unittest.TestCase):
fill_keys: List[List[int]]
return_users: List[UserTest]
default: UserTest
default: Callable[[], UserTest]

def setUp(self) -> None:
self.fill_keys = []
self.default = UserTest(id=0, name='', age=0)
self.default = lambda: UserTest(id=0, name='', age=0)

def new_filler(self) -> FillerFunc[int, UserTest]:
return new_multi_get_filler(
Expand Down Expand Up @@ -529,7 +529,7 @@ def setUp(self) -> None:
filler=new_multi_get_filler(
fill_func=self.fill_multi,
get_key_func=UserTest.get_key,
default=UserTest(id=0, name='', age=0),
default=lambda: UserTest(id=0, name='', age=0),
),
codec=new_json_codec(UserTest),
)
Expand Down

0 comments on commit fde7ff8

Please sign in to comment.