Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/69901.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed `salt.utils.functools` `namespaced_function`/`alias_function` dropping keyword-only argument defaults in copied function
6 changes: 6 additions & 0 deletions salt/utils/functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def namespaced_function(function, global_dict, defaults=None, preserve_context=N
closure=function.__closure__,
)
new_namespaced_function.__dict__.update(function.__dict__)
if function.__kwdefaults__ is not None:
# Only Py 3.13+ accept this in FunctionType.__new__
new_namespaced_function.__kwdefaults__ = function.__kwdefaults__.copy()
return new_namespaced_function


Expand All @@ -76,6 +79,9 @@ def alias_function(fun, name, doc=None):
fun.__closure__,
)
alias_fun.__dict__.update(fun.__dict__)
if fun.__kwdefaults__ is not None:
# Only Py 3.13+ accept this in FunctionType.__new__
alias_fun.__kwdefaults__ = fun.__kwdefaults__.copy()

if doc and isinstance(doc, str):
alias_fun.__doc__ = doc
Expand Down
11 changes: 11 additions & 0 deletions tests/pytests/functional/utils/functools/test_alias_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from salt.utils.functools import alias_function


def test_kwarg_defaults_preserved():
def func(_arg, *, default="foo"):
return default

func2 = alias_function(func, "func2")

assert func(None) == "foo"
assert func2(None) == "foo" # pylint: disable=not-callable
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,13 @@ def foo():
"for removal in 3008.0 (Argon) and no longer does anything for the function "
"being namespaced."
)


def test_kwarg_defaults_preserved():
def func(_arg, *, default="foo"):
return default

func2 = namespaced_function(func, globals())

assert func(None) == "foo"
assert func2(None) == "foo" # pylint: disable=not-callable
Loading