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 loses keywords #343

Open
brl0 opened this issue May 27, 2023 · 0 comments
Open

wraps loses keywords #343

brl0 opened this issue May 27, 2023 · 0 comments

Comments

@brl0
Copy link

brl0 commented May 27, 2023

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=""):
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

No branches or pull requests

1 participant