Skip to content

Commit

Permalink
Update tests with new error message from allow_only_kwargs decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens committed Jun 12, 2024
1 parent ff88dc9 commit e694b72
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/lcm/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@


def allow_only_kwargs(func: F) -> F:
"""Restrict a function to be called with *only* keyword arguments.
"""Restrict a function to be called with only keyword arguments.
Args:
func (Callable): The function to be wrapped.
Returns:
Callable: A Callable with the same arguments as func (but with the additional
restriction that it is *only* callable with keyword arguments).
restriction that it is only callable with keyword arguments).
"""
signature = inspect.signature(func)
Expand All @@ -41,7 +41,7 @@ def allow_only_kwargs(func: F) -> F:
new_signature = signature.replace(parameters=new_parameters)

@functools.wraps(func)
def allow_kwargs_wrapper(*args, **kwargs):
def func_with_only_kwargs(*args, **kwargs):
if args:
raise ValueError(
(
Expand Down Expand Up @@ -69,8 +69,8 @@ def allow_kwargs_wrapper(*args, **kwargs):

# This raises a mypy error but is perfectly fine to do. See
# https://github.com/python/mypy/issues/12472
allow_kwargs_wrapper.__signature__ = new_signature # type: ignore[attr-defined]
return allow_kwargs_wrapper
func_with_only_kwargs.__signature__ = new_signature # type: ignore[attr-defined]
return func_with_only_kwargs


def allow_kwargs(func):
Expand Down
6 changes: 5 additions & 1 deletion tests/test_dispatchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ def test_productmap_with_all_arguments_mapped(func, args, grids, expected, reque
calculated = decorated(**grids)
aaae(calculated, expected)

with pytest.raises(TypeError, match="takes 0 positional arguments but"):
match = (
"This function was decorated with allow_only_kwargs, but was called with "
"positional arguments."
)
with pytest.raises(ValueError, match=match):
decorated(*grids.values())


Expand Down

0 comments on commit e694b72

Please sign in to comment.