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

fix: make copy_function work with keyword-only defaults #336

Merged

Conversation

thorwhalen
Copy link
Contributor

At this point, we have:

from boltons.funcutils import copy_function

f = lambda x, *, y=2: x * y

f_copy = copy_function(f)

assert f(21) == 42  # all goes well, but...

import pytest, re

with pytest.raises(TypeError):
    f_copy(21)  # missing 1 required keyword-only argument: 'y'
    

So f_copy isn't really a copy.

By adding

...
    if hasattr(orig, "__kwdefaults__"):
        ret.__kwdefaults__ = orig.__kwdefaults__
...

to the function, we get the desired behavior:

f = lambda x, *, y=2: x * y
f_copy = copy_function(f)
assert f(21) == f_copy(21) == 42 

I also extended the test_copy_function test function to protect this behavior in future evolutions.

@mahmoud
Copy link
Owner

mahmoud commented Apr 21, 2023

This is great Thor, thanks for the great report and succinct test + fix. I moved the test into the py3-only funcutils tests and kicked off discussions for dropping py2 support via #339. Will get this out in the next release, hopefully in the next week or so.

@mahmoud mahmoud merged commit c2b04b4 into mahmoud:master Apr 21, 2023
9 checks passed
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 this pull request may close these issues.

None yet

2 participants