Skip to content

Commit

Permalink
feat: assert_min_num_of_args
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Feb 21, 2025
1 parent 4eadd85 commit 5b9943d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion dol/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -3232,6 +3232,20 @@ def ingress(self, x):
def egress(self, y):
return self.egress_map[y]

def assert_min_num_of_args(func: Callable, num_of_args: int):
"""Assert that a function can be a store method.
That is, it should have a signature that takes the store as the first argument
"""
try:
assert len(sig = Sig(func).parameters) >= assert_min_num_of_args, (
f"Function {func} doesn't have at least {num_of_args} arguments"
)
except Exception as e:
warn(
f"Encountered error checking if {func} can be a store method. "
"Will return True, to not disrupt process, but you may want to check on "
f"this : {e}"
)

@store_decorator
def add_missing_key_handling(store=None, *, missing_key_callback: Callable):
Expand Down Expand Up @@ -3261,7 +3275,9 @@ def add_missing_key_handling(store=None, *, missing_key_callback: Callable):
>>> v = s['a/']
>>> assert dict(v) == {'a/b': 1, 'a/c': 2}
"""


assert_min_num_of_args(missing_key_callback, 2)

@wraps(store, updated=())
class StoreWithMissingKeyCallaback(store):
pass
Expand Down

0 comments on commit 5b9943d

Please sign in to comment.