Skip to content

Commit 95c1f38

Browse files
Merge pull request #125 from kdheepak/kd/fix-test-failure
Fix test failures for Julia 1.8
2 parents 3f34931 + b916783 commit 95c1f38

File tree

4 files changed

+85
-26
lines changed

4 files changed

+85
-26
lines changed

src/abbreviations.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,20 @@ function format(::TypedMethodSignatures, buf, doc)
384384
# The following will find the tuple that matches the number of arguments in the function
385385
# ideally we would check that the method signature matches the Tuple{...} signature
386386
# but that is not straightforward because of how expressive Julia can be
387+
function f(t)
388+
if t isa DataType
389+
return t <: Tuple && length(t.types) == N
390+
elseif t isa UnionAll
391+
return f(t.body)
392+
else
393+
return false
394+
end
395+
end
396+
387397
if Sys.iswindows()
388-
t = tuples[findlast(t -> t isa DataType && t <: Tuple && length(t.types) == N, tuples)]
398+
t = tuples[findlast(f, tuples)]
389399
else
390-
t = tuples[findfirst(t -> t isa DataType && t <: Tuple && length(t.types) == N, tuples)]
400+
t = tuples[findfirst(f, tuples)]
391401
end
392402
printmethod(buf, binding, func, method, t)
393403
println(buf)

src/utilities.jl

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ julia> DocStringExtensions.find_tuples(s)
246246
"""
247247
function find_tuples(typesig)
248248
if typesig isa UnionAll
249-
return find_tuples(typesig.body)
249+
return [UnionAll(typesig.var, x) for x in find_tuples(typesig.body)]
250250
elseif typesig isa Union
251251
return [typesig.a, find_tuples(typesig.b)...]
252252
else
@@ -278,11 +278,49 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
278278
local args = arguments(method)
279279
local where_syntax = []
280280

281+
# find inner tuple type
282+
function f(t)
283+
# t is always either a UnionAll which represents a generic type or a Tuple where each parameter is the argument
284+
if t isa DataType && t <: Tuple
285+
t
286+
elseif t isa UnionAll
287+
f(t.body)
288+
else
289+
error("Expected `typeof($t)` to be `Tuple` or `UnionAll` but found `$typeof(t)`")
290+
end
291+
end
292+
293+
function get_typesig(t::Union, org::Union)
294+
if t.a isa TypeVar
295+
UnionAll(t.a, get_typesig(t.b, org))
296+
elseif t.b isa TypeVar
297+
UnionAll(t.b, t)
298+
else
299+
t
300+
end
301+
end
302+
303+
function get_typesig(typ::TypeVar, org)
304+
UnionAll(typ, org)
305+
end
306+
307+
function get_typesig(typ, org)
308+
typ
309+
end
310+
281311
for (i, sym) in enumerate(args)
282-
t = typesig.types[i]
312+
if typesig isa UnionAll
313+
# e.g. Tuple{Vector{T}} where T<:Number
314+
# or Tuple{String, T, T} where T<:Number
315+
# or Tuple{Type{T}, String, Union{Nothing, Function}} where T<:Number
316+
t = [x for x in f(typesig).types]
317+
t = [get_typesig(x, x) for x in t][i]
318+
else
319+
# e.g. Tuple{Vector{Int}}
320+
t = typesig.types[i]
321+
end
283322
if isvarargtype(t)
284323
elt = vararg_eltype(t)
285-
286324
if elt === Any
287325
print(buffer, "$sym...")
288326
else
@@ -308,7 +346,7 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
308346
if length(rt) >= 1 && rt[1] !== Nothing && rt[1] !== Union{}
309347
print(buffer, " -> $(rt[1])")
310348
end
311-
return buffer
349+
buffer
312350
end
313351

314352
printmethod(b, f, m) = String(take!(printmethod(IOBuffer(), b, f, m)))

test/TestModule/M.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ i_4(x; y::T = zero(T), z::U = zero(U)) where {T, U} = x + y + z
2323
j_1(x, y) = x * y # two arguments, no keyword arguments
2424
j_1(x; y = x) = x * y # one argument, one keyword argument
2525

26+
k_0(x::T) where T = x
2627
k_1(x::String, y::T = 0, z::T = zero(T)) where T <: Number = x
2728
k_2(x::String, y::U, z::T) where T <: Number where U <: Complex = x
2829
k_3(x, y::T, z::U) where {T, U} = x + y + z
@@ -32,7 +33,6 @@ k_6(x::Vector{T}) where T <: Number = x
3233
k_7(x::Union{T,Nothing}, y::T = zero(T)) where {T <: Integer} = x
3334
k_8(x) = x
3435
k_9(x::T where T<:Any) = x
35-
k_10(x::T) where T = x
3636
k_11(x::Int, xs...) = x
3737
k_12(x::Int, xs::Real...) = x
3838

test/tests.jl

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ end
196196
end
197197
@test occursin("\n```\n", str)
198198

199+
199200
doc.data = Dict(
200201
:binding => Docs.Binding(M, :h),
201202
:typesig => Tuple{Int, Int, Int},
@@ -226,6 +227,17 @@ end
226227
end
227228
@test occursin("\n```\n", str)
228229

230+
doc.data = Dict(
231+
:binding => Docs.Binding(M, :k_0),
232+
:typesig => Tuple{T} where T,
233+
:module => M,
234+
)
235+
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
236+
str = String(take!(buf))
237+
@test occursin("\n```julia\n", str)
238+
@test occursin("\nk_0(x) -> Any\n", str)
239+
@test occursin("\n```\n", str)
240+
229241
doc.data = Dict(
230242
:binding => Docs.Binding(M, :k_1),
231243
:typesig => Union{Tuple{String}, Tuple{String, T}, Tuple{String, T, T}, Tuple{T}} where T <: Number,
@@ -235,8 +247,8 @@ end
235247
str = String(take!(buf))
236248
@test occursin("\n```julia\n", str)
237249
@test occursin("\nk_1(x::String) -> String\n", str)
238-
@test occursin("\nk_1(x::String, y::T<:Number) -> String\n", str)
239-
@test occursin("\nk_1(x::String, y::T<:Number, z::T<:Number) -> String\n", str)
250+
@test occursin("\nk_1(x::String, y::Number) -> String\n", str)
251+
@test occursin("\nk_1(x::String, y::Number, z::Number) -> String\n", str)
240252
@test occursin("\n```\n", str)
241253

242254
doc.data = Dict(
@@ -248,7 +260,7 @@ end
248260
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
249261
str = String(take!(buf))
250262
@test occursin("\n```julia\n", str)
251-
@test occursin("k_2(x::String, y::U<:Complex, z::T<:Number) -> String", str)
263+
@test occursin("k_2(x::String, y::Complex, z::Number) -> String", str)
252264
@test occursin("\n```\n", str)
253265

254266
doc.data = Dict(
@@ -259,7 +271,7 @@ end
259271
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
260272
str = String(take!(buf))
261273
@test occursin("\n```julia\n", str)
262-
@test occursin("\nk_3(x, y::T, z::U) -> Any\n", str)
274+
@test occursin("\nk_3(x, y, z) -> Any\n", str)
263275
@test occursin("\n```\n", str)
264276

265277
doc.data = Dict(
@@ -285,7 +297,6 @@ end
285297
end
286298
@test occursin("\n```\n", str)
287299

288-
289300
doc.data = Dict(
290301
:binding => Docs.Binding(M, :k_5),
291302
:typesig => Union{Tuple{Type{T}, String}, Tuple{Type{T}, String, Union{Nothing, Function}}, Tuple{T}} where T <: Number,
@@ -315,19 +326,28 @@ end
315326
str = String(take!(buf))
316327
str = f(str)
317328
@test occursin("\n```julia\n", str)
318-
@test occursin(f("\nk_6(x::Array{T<:Number,1}) -> Array{T<:Number,1}\n"), str)
329+
if VERSION >= v"1.6.0"
330+
@test occursin(f("\nk_6(x::Array{T<:Number, 1}) -> Vector{T} where T<:Number\n"), str)
331+
else
332+
# TODO: remove this test when julia 1.0.0 support is dropped.
333+
@test occursin(f("\nk_6(x::Array{T<:Number,1}) -> Array{T,1} where T<:Number\n"), str)
334+
end
319335
@test occursin("\n```\n", str)
320336

321337
doc.data = Dict(
322338
:binding => Docs.Binding(M, :k_7),
323-
:typesig => Union{Tuple{Union{T, Nothing}}, Tuple{Union{T, Nothing}, T}, Tuple{T}} where T <: Integer,
339+
:typesig => Union{Tuple{Union{Nothing, T}}, Tuple{T}, Tuple{Union{Nothing, T}, T}} where T<:Integer,
324340
:module => M,
325341
)
326342
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
327343
str = String(take!(buf))
328344
@test occursin("\n```julia\n", str)
329-
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}) -> Union{Nothing, Integer}\n", str)
330-
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}, y::T<:Integer) -> Union{Nothing, T<:Integer}\n", str)
345+
if VERSION > v"1.7" || VERSION < v"1.1"
346+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer) -> Union{Nothing, T} where T<:Integer\n", str)
347+
else
348+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer) -> Union{Nothing, Integer}\n", str)
349+
end
350+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer, y::Integer) -> Union{Nothing, T} where T<:Integer\n", str)
331351
@test occursin("\n```\n", str)
332352

333353
doc.data = Dict(
@@ -378,16 +398,7 @@ end
378398

379399
end
380400

381-
doc.data = Dict(
382-
:binding => Docs.Binding(M, :k_10),
383-
:typesig => Union{Tuple{T}, Tuple{T}} where T,
384-
:module => M,
385-
)
386-
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
387-
str = String(take!(buf))
388-
@test_broken occursin("\n```julia\n", str)
389-
@test_broken occursin("\nk_10(x::T) -> Any\n", str)
390-
@test_broken occursin("\n```\n", str)
401+
391402
end
392403

393404
@testset "function names" begin

0 commit comments

Comments
 (0)