Deprecate keyword argument

I want to deprecate a renamed keyword argument e.g. like this

@deprecate foo(;a1=1, a2=2) foo(;b1=1, a2=2)
foo(a1=2, a2=3)

Warning: foo(; a1=1, a2=2) is deprecated, use foo(; b1=1, a2=2) instead.

But I dont want to specify the default values.

@deprecate foo(;a1, a2) foo(;b1, a2)

ERROR: syntax: invalid keyword argument syntax “b1”

but it seems to not work. Is there an error?

Also, I want to not state all keyword arguments. Only those that did change. Is this possible?

3 Likes

I think this is not really possible. You probably need to do something cumbersome like:

function foo(;a2=2, kw...)
    if haskey(kw, :a1)
        Base.depwarn("keyword argument a1 is now b1", :foo)
        b1 = kw[:a1]
    else
        b1 = get(kw, :b1, 1)
    end
    # the new foo
end
2 Likes

I just made a package for doing this here: GitHub - MilesCranmer/DeprecateKeywords.jl: Macro for deprecating keyword parameters

Described in this thread: Standard way to deprecate a keyword argument

using DeprecateKeywords

@deprecate_kws function foo(;
    new_kw1=2,
    new_kw2=3,
    @deprecate(old_kw1, new_kw1),
    @deprecate(old_kw2, new_kw2)
)
    new_kw1 + new_kw2
end
2 Likes