Skip to content

Commit 0fd456f

Browse files
committed
Make print better for parameteric types
1 parent 91b1db5 commit 0fd456f

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/utilities.jl

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,31 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
288288
end
289289
end
290290

291+
function get_typesig(t::Union, org::Union)
292+
if t.a isa TypeVar
293+
UnionAll(t.a, get_typesig(t.b, org))
294+
elseif t.b isa TypeVar
295+
UnionAll(t.b, t)
296+
else
297+
t
298+
end
299+
end
300+
301+
function get_typesig(typ::TypeVar, org)
302+
UnionAll(typ, org)
303+
end
304+
305+
function get_typesig(typ, org)
306+
return typ
307+
end
308+
291309
for (i, sym) in enumerate(args)
292310
if typesig isa UnionAll
293311
# e.g. Tuple{Vector{T}} where T<:Number
294312
# or Tuple{String, T, T} where T<:Number
295313
# or Tuple{Type{T}, String, Union{Nothing, Function}} where T<:Number
296314
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]
315+
t = [get_typesig(x, x) for x in t][i]
301316
else
302317
# e.g. Tuple{Vector{Int}}
303318
t = typesig.types[i]

test/tests.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,18 @@ end
325325

326326
doc.data = Dict(
327327
:binding => Docs.Binding(M, :k_7),
328-
:typesig => Union{Tuple{Union{T, Nothing}}, Tuple{Union{T, Nothing}, T}, Tuple{T}} where T <: Integer,
328+
:typesig => Union{Tuple{Union{Nothing, T}}, Tuple{T}, Tuple{Union{Nothing, T}, T}} where T<:Integer,
329329
:module => M,
330330
)
331331
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
332332
str = String(take!(buf))
333333
@test occursin("\n```julia\n", str)
334-
# TODO: print `Union{Nothing, T<:Integer}` instead of `Union{Nothing, T} where T<:Number`
335334
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)
335+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer) -> Union{Nothing, T} where T<:Integer\n", str)
337336
else
338-
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}) -> Union{Nothing, Integer}\n", str)
337+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer) -> Union{Nothing, Integer}\n", str)
339338
end
340-
@test occursin("\nk_7(x::Union{Nothing, T<:Integer}, y::Integer) -> Union{Nothing, T} where T<:Integer\n", str)
339+
@test occursin("\nk_7(x::Union{Nothing, T} where T<:Integer, y::Integer) -> Union{Nothing, T} where T<:Integer\n", str)
341340
@test occursin("\n```\n", str)
342341

343342
doc.data = Dict(
@@ -390,13 +389,13 @@ end
390389

391390
doc.data = Dict(
392391
:binding => Docs.Binding(M, :k_10),
393-
:typesig => Union{Tuple{T}, Tuple{T}} where T,
392+
:typesig => Tuple{T} where T,
394393
:module => M,
395394
)
396395
DSE.format(DSE.TYPEDSIGNATURES, buf, doc)
397396
str = String(take!(buf))
398397
@test_broken occursin("\n```julia\n", str)
399-
@test_broken occursin("\nk_10(x::T) -> Any\n", str)
398+
@test_broken occursin("\nk_10(x) -> Any\n", str)
400399
@test_broken occursin("\n```\n", str)
401400
end
402401

0 commit comments

Comments
 (0)