Skip to content

wraps loses keywords #343

@brl0

Description

@brl0

Thanks for the great project!

The decorator boltons.funcutils.wraps seems to lose keywords passed to the wrapped function. This does not happen with functools.wraps.
Here is a silly example:

from boltons.funcutils import wraps

def flip(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        return f(*reversed(args), **kwargs)
    return wrapper

def pow(x, y, msg=""):
    """Print msg and raise x to the power of y."""
    print(f"{x=}, {y=}")
    if msg:
        print(f"{msg=}")
    result = x ** y
    print(f"{result=}")
    return result

flipped_pow = flip(pow)
flipped_pow(3, 2, msg="abc")

Expected:

x=2, y=3
msg='abc'
result=8


Actual:

x='abc', y=2
msg=3

TypeError
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[22], line 18
     15     return x ** y
     17 flipped_pow = flip(pow)
---> 18 flipped_pow(3, 2, msg="abc")

File :2, in pow(x, y, msg)

Cell In[22], line 7, in flip..wrapper(*args, **kwargs)
      5 @wraps(f)
      6 def wrapper(*args, **kwargs):
----> 7     return f(*reversed(args), **kwargs)

Cell In[22], line 15, in pow(x, y, msg)
     13 if msg:
     14     print(f"{msg=}")
---> 15 return x ** y

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

I also noticed that if I make msg keyword only, as below, it works:

def pow(x, y, *, msg=""):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions