Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wraps does not correctly handle keyword-only arguments #1724

Closed
FMeinicke opened this issue Mar 14, 2023 · 2 comments · Fixed by #1866
Closed

wraps does not correctly handle keyword-only arguments #1724

FMeinicke opened this issue Mar 14, 2023 · 2 comments · Fixed by #1866

Comments

@FMeinicke
Copy link

I have a function that takes some positional arguments and a keyword-only argument that I'd like to wrap with @ureg.wraps as follows:

@ureg.wraps(None, (None, ureg.kilogram, ureg.meter, ureg.meter_per_second, None))
def func(arg1: str, arg2: float, arg3: np.float, arg4: float, *, kwarg1: str = ""):
    ...

However, this results in the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/florian/.local/lib/python3.10/site-packages/pint/registry_helpers.py", line 289, in wrapper
    result = func(*new_values, **kw)
TypeError: func() takes 4 positional arguments but 5 were given

I've traced the problem down to this function:

def _apply_defaults(func, args, kwargs):
"""Apply default keyword arguments.
Named keywords may have been left blank. This function applies the default
values so that every argument is defined.
"""
sig = signature(func)
bound_arguments = sig.bind(*args, **kwargs)
for param in sig.parameters.values():
if param.name not in bound_arguments.arguments:
bound_arguments.arguments[param.name] = param.default
args = [bound_arguments.arguments[key] for key in sig.parameters.keys()]
return args, {}

Here, kwargs are "converted" to positional arguments which in my case causes the error because kwarg1 cannot be used as a positional argument but only a keyword argument.

A possible fix would be to check the kind of the parameter and construct a kwargs dictionary with all keyword-only arguments. This, for example, works in my case:

def _apply_defaults(func, args, kwargs):
    """Apply default keyword arguments.

    Named keywords may have been left blank. This function applies the default
    values so that every argument is defined.
    """

    sig = signature(func)
    bound_arguments = sig.bind(*args, **kwargs)
    for param in sig.parameters.values():
        if param.name not in bound_arguments.arguments:
            bound_arguments.arguments[param.name] = param.default
    args = []
    kwargs = {}
    for key, param in sig.parameters.items():
        if param.kind == Parameter.KEYWORD_ONLY:
            kwargs[key] = bound_arguments.arguments[key]
        else:
            args.append(bound_arguments.arguments[key])
    return args, kwargs
@FMeinicke FMeinicke changed the title wraps does not correctly handle keyword only arguments wraps does not correctly handle keyword-only arguments Mar 14, 2023
@hgrecco
Copy link
Owner

hgrecco commented Apr 27, 2023

Yes. We need to improve wraps and check. But we should make a wish list in a separate issue.

@hgrecco hgrecco closed this as completed Apr 27, 2023
@hgrecco
Copy link
Owner

hgrecco commented Apr 27, 2023

See #1753

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants