Skip to content

Commit

Permalink
Merge 79e35ee into af69e82
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Jul 24, 2023
2 parents af69e82 + 79e35ee commit 6786714
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/DeprecateKeywords.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ function _depkws(def)

# Update new symbols to use deprecated kws if passed:
for (i, kw) in enumerate(sdef[:kwargs])
isa(kw, Symbol) && continue

new_kw = kw.args[1]
default = kw.args[2]
no_default = isa(kw, Symbol)
new_kw, default = if no_default
(kw, DeprecatedDefault)
else
(kw.args[1], kw.args[2])
end
_get_symbol(new_kw) in deprecated_symbols && continue
!(_get_symbol(new_kw) in new_symbols) && continue
deprecated_symbol = symbol_mapping[_get_symbol(new_kw)]
Expand All @@ -82,6 +84,19 @@ function _depkws(def)
end
end
sdef[:kwargs][i] = Expr(:kw, new_kw, new_kwcall)

if no_default
# Propagate UndefKeywordError
pushfirst!(
sdef[:body].args,
Expr(:if,
Expr(:call, :(==), _get_symbol(new_kw), DeprecatedDefault),
Expr(:call, :throw,
Expr(:call, :UndefKeywordError, QuoteNode(_get_symbol(new_kw)))
)
)
)
end
end

return combinedef(sdef)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,10 @@ end
@testset "Error catching" begin
VERSION >= v"1.8" && @test_throws LoadError (@eval @depkws k(; @deprecate a b, b = 10) = b)
end

@testset "No default set" begin
@depkws k(x; (@deprecate a b), b) = x + b
@test_throws UndefKeywordError k(1.0)
VERSION >= v"1.8" && @test_warn "Keyword argument" (@test k(1.0; a=2.0) == 3.0)
@test k(1.0; b=2.0) == 3.0
end

0 comments on commit 6786714

Please sign in to comment.