Skip to content

Commit 9b556ec

Browse files
Merge pull request #179 from ReubenJ/feat/typed-signature-no-return
Implement `TYPEDSIGNATURENORETURN`
2 parents 3f6543c + 649f56d commit 9b556ec

3 files changed

Lines changed: 107 additions & 10 deletions

File tree

src/abbreviations.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,17 +336,25 @@ end
336336
#
337337

338338
"""
339-
The singleton type for [`TYPEDSIGNATURES`](@ref) abbreviations.
339+
The type for [`TYPEDSIGNATURES`](@ref) abbreviations.
340340
341341
$(:FIELDS)
342342
"""
343-
struct TypedMethodSignatures <: Abbreviation end
343+
struct TypedMethodSignatures <: Abbreviation
344+
return_types::Bool
345+
end
344346

345347
"""
346348
An [`Abbreviation`](@ref) for including a simplified representation of all the method
347349
signatures with types that match the given docstring. See [`printmethod`](@ref) for details on
348350
the simplifications that are applied.
349351
352+
!!! tip "Disabling the Return Type"
353+
In many codebases the return types are not annotated meaning the return
354+
type is printed as `Any`. To reduce clutter, the return type may be omitted by
355+
calling [`TypedMethodSignatures`](@ref) and passing `false` to its constructor:
356+
`\$(DocStringExtensions.TypedMethodSignatures(false))`.
357+
350358
# Examples
351359
352360
The generated markdown text will look similar to the following example where a function `f`
@@ -358,9 +366,9 @@ f(x::Int, y::Int; a, b...)
358366
```
359367
````
360368
"""
361-
const TYPEDSIGNATURES = TypedMethodSignatures()
369+
const TYPEDSIGNATURES = TypedMethodSignatures(true)
362370

363-
function format(::TypedMethodSignatures, buf, doc)
371+
function format(tms::TypedMethodSignatures, buf, doc)
364372
local binding = doc.data[:binding]
365373
local typesig = doc.data[:typesig]
366374
local modname = doc.data[:module]
@@ -395,7 +403,8 @@ function format(::TypedMethodSignatures, buf, doc)
395403
else
396404
t = tuples[findfirst(f, tuples)]
397405
end
398-
printmethod(buf, binding, func, method, t)
406+
printmethod(buf, binding, func, method, t;
407+
print_return_types=tms.return_types)
399408
println(buf)
400409
end
401410
println(buf, "\n```\n")

src/utilities.jl

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ f(x::Int; a = 1, b...) = x
338338
sig = printmethod(Docs.Binding(Main, :f), f, first(methods(f)))
339339
```
340340
"""
341-
function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Method, typesig)
341+
function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Method, typesig; print_return_types=true)
342342
# TODO: print qualified?
343343
local args = string.(arguments(method))
344344
local kws = string.(keywords(func, method))
@@ -397,11 +397,24 @@ function printmethod(buffer::IOBuffer, binding::Docs.Binding, func, method::Meth
397397
end
398398

399399
rt = Base.return_types(func, typesig)
400+
return_type_string = if (
401+
print_return_types &&
402+
length(rt) >= 1 &&
403+
rt[1] !== Nothing &&
404+
rt[1] !== Union{}
405+
)
406+
" -> $(rt[1])"
407+
else
408+
""
409+
end
400410

401-
return printmethod_format(buffer, string(binding.var), args, string.(kws);
402-
return_type =
403-
length(rt) >= 1 && rt[1] !== Nothing && rt[1] !== Union{} ?
404-
" -> $(rt[1])" : "")
411+
return printmethod_format(
412+
buffer,
413+
string(binding.var),
414+
args,
415+
string.(kws);
416+
return_type=return_type_string
417+
)
405418
end
406419

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

test/tests.jl

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,81 @@ end
455455

456456
end
457457

458+
@testset "method signatures with types (no return type)" begin
459+
doc.data = Dict(
460+
:binding => Docs.Binding(M, :h_1),
461+
:typesig => Tuple{M.A},
462+
:module => M,
463+
)
464+
DSE.format(DSE.TypedMethodSignatures(false), buf, doc)
465+
str = String(take!(buf))
466+
@test occursin("\n```julia\n", str)
467+
f = str -> replace(str, " " => "")
468+
str = f(str)
469+
if Sys.iswindows() && VERSION < v"1.8"
470+
@test occursin(f("h_1(x::Union{Array{T,4}, Array{T,3}} where T)"), str)
471+
else
472+
@test occursin(f("h_1(x::Union{Array{T,3}, Array{T,4}} where T)"), str)
473+
end
474+
@test !occursin("->", str)
475+
@test occursin("\n```\n", str)
476+
477+
478+
doc.data = Dict(
479+
:binding => Docs.Binding(M, :g_2),
480+
:typesig => Tuple{String},
481+
:module => M,
482+
)
483+
DSE.format(DSE.TypedMethodSignatures(false), buf, doc)
484+
str = String(take!(buf))
485+
@test occursin("\n```julia\n", str)
486+
@test occursin("\ng_2(x::String)", str)
487+
@test occursin("\n```\n", str)
488+
489+
doc.data = Dict(
490+
:binding => Docs.Binding(M, :h),
491+
:typesig => Tuple{Int,Int,Int},
492+
:module => M,
493+
)
494+
DSE.format(DSE.TypedMethodSignatures(false), buf, doc)
495+
str = String(take!(buf))
496+
@test occursin("\n```julia\n", str)
497+
if typeof(1) === Int64
498+
@test occursin("\nh(x::Int64, y::Int64, z::Int64; kwargs...)\n", str)
499+
else
500+
@test occursin("\nh(x::Int32, y::Int32, z::Int32; kwargs...)\n", str)
501+
end
502+
@test occursin("\n```\n", str)
503+
504+
doc.data = Dict(
505+
:binding => Docs.Binding(M, :h),
506+
:typesig => Tuple{Int},
507+
:module => M,
508+
)
509+
DSE.format(DSE.TypedMethodSignatures(false), buf, doc)
510+
str = String(take!(buf))
511+
@test occursin("\n```julia\n", str)
512+
if typeof(1) === Int64
513+
# On 1.10+, automatically generated methods have keywords in the metadata,
514+
# hence the display difference between Julia versions.
515+
if VERSION >= v"1.10"
516+
@test occursin("\nh(x::Int64; ...)\n", str)
517+
else
518+
@test occursin("\nh(x::Int64)\n", str)
519+
end
520+
else
521+
# On 1.10+, automatically generated methods have keywords in the metadata,
522+
# hence the display difference between Julia versions.
523+
if VERSION >= v"1.10"
524+
@test occursin("\nh(x::Int32; ...)\n", str)
525+
else
526+
@test occursin("\nh(x::Int32)\n", str)
527+
end
528+
end
529+
@test occursin("\n```\n", str)
530+
531+
end
532+
458533
@testset "function names" begin
459534
doc.data = Dict(
460535
:binding => Docs.Binding(M, :f),

0 commit comments

Comments
 (0)