Skip to content

Commit 0fb7e6c

Browse files
tkoolenmortenpi
authored andcommitted
Fix SIGNATURES for methods with UnionAll arguments (#38)
* Fix #32. * Add tests. * Fix templates for `where` expression header, add test. * Cleanup.
1 parent 2735596 commit 0fb7e6c

4 files changed

Lines changed: 28 additions & 6 deletions

File tree

src/templates.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function expression_type(ex::Expr)
127127
:TYPES
128128
elseif Meta.isexpr(ex, :macro)
129129
:MACROS
130-
elseif Meta.isexpr(ex, [:function, :(=)]) && Meta.isexpr(ex.args[1], :call)
130+
elseif Meta.isexpr(ex, [:function, :(=)]) && Meta.isexpr(ex.args[1], :call) || (Meta.isexpr(ex.args[1], :where) && Meta.isexpr(ex.args[1].args[1], :call))
131131
:METHODS
132132
elseif Meta.isexpr(ex, :function)
133133
:FUNCTIONS

src/utilities.jl

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ groups = methodgroups(f, Union{Tuple{Any}, Tuple{Any, Integer}}, Main; exact = f
2121
"""
2222
function methodgroups(func, typesig, modname; exact = true)
2323
# Group methods by file and line number.
24-
local methods = Set{Method}(getmethods(func, typesig))
24+
local methods = getmethods(func, typesig)
2525
local groups = groupby(Tuple{Symbol, Int}, Vector{Method}, methods) do m
2626
(m.file, m.line), m
2727
end
@@ -32,7 +32,7 @@ function methodgroups(func, typesig, modname; exact = true)
3232
for (key, group) in groups
3333
filter!(group) do m
3434
local ismod = m.module == modname
35-
exact ? (ismod && Base.tuple_type_tail(m.sig) in typesigs) : ismod
35+
exact ? (ismod && Base.rewrap_unionall(Base.tuple_type_tail(m.sig), m.sig) in typesigs) : ismod
3636
end
3737
isempty(group) || push!(results, group)
3838
end
@@ -80,8 +80,10 @@ function getmethods!(results, f, sig)
8080
append!(results, methods(f))
8181
elseif isa(sig, Union)
8282
for each in uniontypes(sig)
83-
append!(results, getmethods(f, each))
83+
getmethods!(results, f, each)
8484
end
85+
elseif isa(sig, UnionAll)
86+
getmethods!(results, f, Base.unwrap_unionall(sig))
8587
else
8688
append!(results, methods(f, sig))
8789
end
@@ -95,7 +97,7 @@ Collect and return all methods of function `f` matching signature `sig`.
9597
This is similar to `methods(f, sig)`, but handles type signatures found in `DocStr` objects
9698
more consistently that `methods`.
9799
"""
98-
getmethods(f, sig) = getmethods!(Method[], f, sig)
100+
getmethods(f, sig) = unique(getmethods!(Method[], f, sig))
99101

100102

101103
"""
@@ -118,7 +120,17 @@ $(:SIGNATURES)
118120
119121
Returns a `Vector` of the `Tuple` types contained in `sig`.
120122
"""
121-
alltypesigs(sig) = sig == Union{} ? Any[] : isa(sig, Union) ? uniontypes(sig) : Any[sig]
123+
function alltypesigs(sig)::Vector{Any}
124+
if sig == Union{}
125+
Any[]
126+
elseif isa(sig, Union)
127+
uniontypes(sig)
128+
elseif isa(sig, UnionAll)
129+
Base.rewrap_unionall.(uniontypes(Base.unwrap_unionall(sig)), sig)
130+
else
131+
Any[sig]
132+
end
133+
end
122134

123135
"""
124136
$(:SIGNATURES)

test/templates.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ mutable struct T end
3838
"method `f`"
3939
f(x) = x
4040

41+
"method `g`"
42+
g(::Type{T}) where {T} = T # Issue 32
43+
4144
"macro `@m`"
4245
macro m(x) end
4346

test/tests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const A{T} = Union{Vector{T}, Matrix{T}}
1717

1818
h_1(x::A) = x
1919
h_2(x::A{Int}) = x
20+
h_3(x::A{T}) where {T} = x
2021

2122
mutable struct T
2223
a
@@ -186,6 +187,7 @@ end
186187
@test contains(fmt(:(TemplateTests.K)), "(DEFAULT)")
187188
@test contains(fmt(:(TemplateTests.T)), "(TYPES)")
188189
@test contains(fmt(:(TemplateTests.f)), "(METHODS, MACROS)")
190+
@test contains(fmt(:(TemplateTests.g)), "(METHODS, MACROS)")
189191
@test contains(fmt(:(TemplateTests.@m)), "(METHODS, MACROS)")
190192

191193
@test contains(fmt(:(TemplateTests.InnerModule.K)), "(DEFAULT)")
@@ -297,6 +299,9 @@ end
297299
@test length(DSE.getmethods(M.f, Union{})) == 1
298300
@test length(DSE.getmethods(M.f, Tuple{})) == 0
299301
@test length(DSE.getmethods(M.f, Union{Tuple{}, Tuple{Any}})) == 1
302+
@test length(DSE.getmethods(M.h_3, Tuple{M.A{Int}})) == 1
303+
@test length(DSE.getmethods(M.h_3, Tuple{Vector{Int}})) == 1
304+
@test length(DSE.getmethods(M.h_3, Tuple{Array{Int, 3}})) == 0
300305
end
301306
@testset "methodgroups" begin
302307
@test length(DSE.methodgroups(M.f, Tuple{Any}, M)) == 1
@@ -305,11 +310,13 @@ end
305310
@test length(DSE.methodgroups(M.h_1, Tuple{M.A}, M)[1]) == 1
306311
@test length(DSE.methodgroups(M.h_2, Tuple{M.A{Int}}, M)) == 1
307312
@test length(DSE.methodgroups(M.h_2, Tuple{M.A{Int}}, M)[1]) == 1
313+
@test length(DSE.methodgroups(M.h_3, Tuple{M.A}, M)[1]) == 1
308314
end
309315
@testset "alltypesigs" begin
310316
@test DSE.alltypesigs(Union{}) == Any[]
311317
@test DSE.alltypesigs(Union{Tuple{}}) == Any[Tuple{}]
312318
@test DSE.alltypesigs(Tuple{}) == Any[Tuple{}]
319+
@test DSE.alltypesigs(Type{T} where {T}) == Any[Type{T} where T]
313320
end
314321
@testset "groupby" begin
315322
let groups = DSE.groupby(Int, Vector{Int}, collect(1:10)) do each

0 commit comments

Comments
 (0)