Preserve keyword-only defaults in namespaced_function and alias_function - #70009
Preserve keyword-only defaults in namespaced_function and alias_function#70009eeshsaxena wants to merge 2 commits into
Conversation
twangboy
left a comment
There was a problem hiding this comment.
Please make this against the earliest branch where the bug exists. Most likely 3006.x
dad776a to
e88479c
Compare
|
Retargeted to 3006.x and rebased. I confirmed the bug is present there too: |
types.FunctionType does not carry __kwdefaults__, so copying a function via namespaced_function or alias_function dropped keyword-only argument defaults. Calling the copy without those keyword arguments then raised TypeError even though the original had defaults. Copy __kwdefaults__ across explicitly and cover both helpers with unit tests.
e88479c to
89be5e9
Compare
|
Hi! Gentle nudge on this one whenever you have some bandwidth. It's a small, self-contained fix ( |
|
Looks like it's failing pre-commit and lint. |
|
Thanks for the heads up. I'll run pre-commit locally, clear the lint failures, and push a clean commit. |
black wanted the alias_function() call on one line, and pylint's E1102 fires on the dynamically-built clone/alias, so the two call sites carry a targeted not-callable disable.
|
This is a duplicate of my PR btw #69902 |
|
Ah you're right, #69902 is older and covers the same ground. I'll close this in favour of yours. If the keyword-only defaults regression test here is useful feel free to lift it, otherwise no worries. |
What does this PR do?
Fixes #69901.
salt.utils.functools.namespaced_functionandalias_functionboth rebuild a function withtypes.FunctionType, passingargdefs=fn.__defaults__but nothing for__kwdefaults__. The constructor has no parameter for keyword-only defaults, so the copy loses them. Calling the copy without those keyword arguments then raisesTypeErroreven though the original had defaults:Setting
__kwdefaults__on the new function object in both helpers restores the defaults.Tests
Added
tests/pytests/unit/utils/test_functools.pycovering both helpers: each asserts__kwdefaults__is carried over and that the copy is callable without the keyword-only argument. Both assertions fail against the current code and pass with the change.I could not run the full salt suite on my machine (Windows, no salt runtime), so I verified by executing the patched
salt/utils/functools.pydirectly with the two salt-internal imports stubbed; both copies keep__kwdefaults__and are callable. The new unit test is standard and should run cleanly in CI.Changelog
Added
changelog/69901.fixed.md.