Skip to content

Commit 91b1db5

Browse files
committed
Fix test failures for Julia 1.8
1 parent 3f34931 commit 91b1db5

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
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: 24 additions & 3 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,32 @@ 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+
return t
286+
elseif t isa UnionAll
287+
return f(t.body)
288+
end
289+
end
290+
281291
for (i, sym) in enumerate(args)
282-
t = typesig.types[i]
292+
if typesig isa UnionAll
293+
# e.g. Tuple{Vector{T}} where T<:Number
294+
# or Tuple{String, T, T} where T<:Number
295+
# or Tuple{Type{T}, String, Union{Nothing, Function}} where T<:Number
296+
t = [x for x in f(typesig).types]
297+
v = [x for x in t if x isa TypeVar]
298+
# TODO: this prints `Union{Nothing, T<:Integer}` instead of `Union{Nothing, T} where T<:Number`
299+
# To do this correctly, we need to extract the information out of types like this: `Type{TypeVar(:T, Number)}`
300+
t = [x isa TypeVar ? UnionAll(popfirst!(v), x) : x for x in t][i]
301+
else
302+
# e.g. Tuple{Vector{Int}}
303+
t = typesig.types[i]
304+
end
283305
if isvarargtype(t)
284306
elt = vararg_eltype(t)
285-
286307
if elt === Any
287308
print(buffer, "$sym...")
288309
else

test/tests.jl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ end
235235
str = String(take!(buf))
236236
@test occursin("\n```julia\n", str)
237237
@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)
238+
@test occursin("\nk_1(x::String, y::Number) -> String\n", str)
239+
@test occursin("\nk_1(x::String, y::Number, z::Number) -> String\n", str)
240240
@test occursin("\n```\n", str)
241241

242242
doc.data = Dict(
@@ -248,7 +248,7 @@ end
248248
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
249249
str = String(take!(buf))
250250
@test occursin("\n```julia\n", str)
251-
@test occursin("k_2(x::String, y::U<:Complex, z::T<:Number) -> String", str)
251+
@test occursin("k_2(x::String, y::Complex, z::Number) -> String", str)
252252
@test occursin("\n```\n", str)
253253

254254
doc.data = Dict(
@@ -259,7 +259,7 @@ end
259259
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
260260
str = String(take!(buf))
261261
@test occursin("\n```julia\n", str)
262-
@test occursin("\nk_3(x, y::T, z::U) -> Any\n", str)
262+
@test occursin("\nk_3(x, y, z) -> Any\n", str)
263263
@test occursin("\n```\n", str)
264264

265265
doc.data = Dict(
@@ -315,7 +315,12 @@ end
315315
str = String(take!(buf))
316316
str = f(str)
317317
@test occursin("\n```julia\n", str)
318-
@test occursin(f("\nk_6(x::Array{T<:Number,1}) -> Array{T<:Number,1}\n"), str)
318+
if VERSION >= v"1.6.0"
319+
@test occursin(f("\nk_6(x::Array{T<:Number, 1}) -> Vector{T} where T<:Number\n"), str)
320+
else
321+
# TODO: remove this test when julia 1.0.0 support is dropped.
322+
@test occursin(f("\nk_6(x::Array{T<:Number,1}) -> Array{T,1} where T<:Number\n"), str)
323+
end
319324
@test occursin("\n```\n", str)
320325

321326
doc.data = Dict(
@@ -326,8 +331,13 @@ end
326331
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
327332
str = String(take!(buf))
328333
@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)
334+
# TODO: print `Union{Nothing, T<:Integer}` instead of `Union{Nothing, T} where T<:Number`
335+
if VERSION > v"1.7" || VERSION < v"1.1"
336+
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}) -> Union{Nothing, T} where T<:Integer\n", str)
337+
else
338+
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}) -> Union{Nothing, Integer}\n", str)
339+
end
340+
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}, y::Integer) -> Union{Nothing, T} where T<:Integer\n", str)
331341
@test occursin("\n```\n", str)
332342

333343
doc.data = Dict(

0 commit comments

Comments
 (0)