Giter VIP home page Giter VIP logo

Comments (6)

mortenpi avatar mortenpi commented on July 22, 2024

It's probably due to the assumption that typesig will always have a field called :a:

for (i, method) in enumerate(group)
if i == length(group)
t = typesig
else
t = typesig.a
typesig = typesig.b
end
printmethod(buf, binding, func, method, t)
println(buf)
end

I won't have time to dig into this deeper though, but would happily take a PR.

from docstringextensions.jl.

kdheepak avatar kdheepak commented on July 22, 2024

@mortenpi I'm looking into this and will be happy to submit a PR.

I do have a question though. I have the following file:

cat test.jl
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.jl
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ using DocStringExtensions
   2   │
   3   │ """
   4   │ $TYPEDSIGNATURES
   5   │
   6   │ hello_world is a function
   7   │ """
   8   │ function hello_world(::Type{T}, s::AbstractString) where {T <: Number}
   9   │     @show T, s
  10   │     nothing
  11   │ end
  12   │
  13   │ println(@doc hello_world(Number, "hi"))
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

And I have this minimal example, and when I run it in DocStringExtensions, I get the following

binding = hello_world
typesig = Union{Tuple{T}, Tuple{Type{T},AbstractString}} where T<:Number
modname = Main
func = hello_world

```julia
hello_world(?::T<:Number, s::T<:Number)

```

hello_world is a function

I put @show calls in the following function for the above output:

local binding = doc.data[:binding]
local typesig = doc.data[:typesig]
local modname = doc.data[:module]
local func = Docs.resolve(binding)
local groups = methodgroups(func, typesig, modname)

Why does the typesig have Tuple{Type{T},AbstractString}? Shouldn't it just be AbstractString? Is this a bug? Is DocStringExtensions generating typesig or is that happening in base julia?

If I don't use a generic typeof as the first argument, this works as expected.

───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: test.jl
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ using DocStringExtensions
   2   │
   3   │ """
   4   │ $TYPEDSIGNATURES
   5   │
   6   │ hello_world is a function
   7   │ """
   8   │ function hello_world(::Type{Number}, s::AbstractString)
   9   │     @show Number, s
  10   │     nothing
  11   │ end
  12   │
  13   │ println(@doc hello_world(Number, "hi"))
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
binding = hello_world
typesig = Tuple{Type{Number},AbstractString}
modname = Main
func = hello_world

```julia
hello_world(?::Type{Number}, s::AbstractString)

```

hello_world is a function

from docstringextensions.jl.

kdheepak avatar kdheepak commented on July 22, 2024

I think I figured out the answer to part of my question

struct __CUSTOM__ end

function Docs.formatdoc(buffer, docstr, ::Type{__CUSTOM__})
    @show docstr.data[:typesig]
end


"""
hello_world is a function

$__CUSTOM__
"""
function hello_world(::Type{T}, s::AbstractString) where T <: Number
    @show Number, s
    nothing
end

println(@doc hello_world(Number, "hi"))

This returns

docstr.data[:typesig] = Union{Tuple{T}, Tuple{Type{T},AbstractString}} where T<:Number
hello_world is a function

So this is generated by Julia. I'm still not sure if Tuple{Type{T},AbstractString} is a bug or not.

from docstringextensions.jl.

kdheepak avatar kdheepak commented on July 22, 2024

Okay, it's seems that it is not a bug, but intended. Sorry for spamming this thread. I'm still trying to figure out why the typesig is the way it is, and I have more questions than answers.

from docstringextensions.jl.

mortenpi avatar mortenpi commented on July 22, 2024

Thanks for tackling this @kdheepak!

To answer your question about the type signature: yep, it's something that comes from Julia itself. It's created by the Base.Docs.signature function:

julia> Base.Docs.signature(:(function foo end))
:(Union{})

julia> Base.Docs.signature(:(function foo(::Int) end))
:(Union{Tuple{Int}})

julia> Base.Docs.signature(:(function foo(::Vector{T}) where T <: Number end))
:(Union{Tuple{Vector{T}}, Tuple{T}} where T <: Number)

The weirdness about the parametric signature, I believe, is a bug in Docs.signature (JuliaLang/julia#29437). The second Tuple{T} shouldn't be there, which you can see if you look up the signature for the method in the method table:

julia> function foo(::Int) end
foo (generic function with 1 method)

julia> function foo(::Vector{T}) where T <: Number end
foo (generic function with 2 methods)

julia> methods(foo)
# 2 methods for generic function "foo":
[1] foo(::Int64) in Main at REPL[4]:1
[2] foo(::Array{T,1}) where T<:Number in Main at REPL[10]:1

julia> methods(foo).ms[1].sig
Tuple{typeof(foo),Int64}

julia> methods(foo).ms[2].sig
Tuple{typeof(foo),Array{T,1}} where T<:Number

So, coming back to the original example, the typesig for function hello_world(::Type{Number}, s::AbstractString) end should be the following UnionAll: Tuple{Type{T},AbstractString} where T<:Number. However, that's not what the docsystem stores, so in DocStringExtensions we'd need some sort of a workaround for that bug.

from docstringextensions.jl.

kdheepak avatar kdheepak commented on July 22, 2024

Thanks for the comment! This is exactly what I wanted to know. 1) it is a bug with the signature function, and 2) how to generate the signature with the Base.Docs module and what the Julia type system thinks the signature is. I'll post more information in the PR.

from docstringextensions.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.