# Check if a function has a docstring

**URL:** <https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489>\
**Category:** General Usage\
**Created:** [September 3, 2023, 6:41pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489 "2023-09-03T18:41:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 3, 2023, 6:41pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/1 "2023-09-03T18:41:19Z")

</div>

How can I check programmatically if a function has a docstring?

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [September 3, 2023, 7:40pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/2 "2023-09-03T19:40:51Z")

</div>

Use the `@doc` macro or the `doc` function:

[https://docs.julialang.org/en/v1/manual/documentation/#Advanced-Usage](https://docs.julialang.org/en/v1/manual/documentation/#Advanced-Usage)

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 3, 2023, 7:47pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/3 "2023-09-03T19:47:18Z")

</div>

How can I tell if a name has a docstring using your proposed technique? For example

```julia
using Markdown
julia> dump(@doc md"")
Markdown.MD
  content: Array{Any}((3,))
    1: Markdown.Paragraph
      content: Array{Any}((1,))
        1: String "No documentation found."
    2: Markdown.Paragraph
      content: Array{Any}((2,))
        1: Markdown.Code
          language: String ""
          code: String "Markdown.@md_str"
        2: String " is a macro."
    3: Markdown.Code
      language: String ""
```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 3, 2023, 9:00pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/4 "2023-09-03T21:00:46Z")

</div>

> [@jar1](#):
>
> How can I tell if a name has a docstring using your proposed technique? For example

You could look for `"No documentation found."` as the first paragraph, I guess, though that is a bit ugly.

To check whether there is documentation for a particular symbol in a particular module, this seems to work, but relies on undocumented internals of the `Docs` module:

```julia
julia> hasdoc(mod::Module, sym::Symbol) = haskey(Base.Docs.meta(mod), Base.Docs.Binding(mod, sym));

julia> hasdoc(Base, :sum)
true

julia> hasdoc(Base, :foo)
false

```

It would be nice to export some kind of documented functionality here, but first it would be helpful to have an example of a practical application that needs this, in order to determine the appropriate functionality.

For example, it might be useful for `Base.Test` to provide a way to check that all exported symbols in a module have docstrings.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 3, 2023, 9:08pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/5 "2023-09-03T21:08:59Z")

</div>

> [@stevengj](#):
>
> For example, it might be useful for `Base.Test` to provide a way to check that all exported symbols in a module have docstrings.

Yeah that’s what I’m going for. Though I think it’s even better practice to give _all_ functions docstrings to help contributors.

---

<div class="post-metadata">

**Author:** ![rdavis120](https://avatars.discourse-cdn.com/v4/letter/r/b5a626/32.png) [@rdavis120](https://discourse.julialang.org/u/rdavis120)\
**Post date:** [September 4, 2023, 1:29am UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/6 "2023-09-04T01:29:23Z")

</div>

> [@stevengj](#):
>
> For example, it might be useful for `Base.Test` to provide a way to check that all exported symbols in a module have docstrings.

I think this would be a very significant change to help R developers who are used to a dynamic language, but expecting the ‘check’ process of cran where packages have complete documentation on exported functions.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2023, 12:47pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/7 "2023-09-04T12:47:08Z")

</div>

> [@stevengj](#):
>
> For example, it might be useful for `Base.Test` to provide a way to check that all exported symbols in a module have docstrings.

I filed an issue with an example implementation: [add a way to test whether exported symbols are documented · Issue #51174 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/51174)

Anyone want to take a crack at it?

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [September 4, 2023, 2:00pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/8 "2023-09-04T14:00:25Z")

</div>

> [@stevengj](#):
>
> Anyone want to take a crack at it?

Do you mean make a pull request addressing the issue? (I don’t have time for that, but I hope that someone does)

If you just meant test your code, I did the following which works in 1.9 & 1.10:

```julia
using Test

@testset "hasdoc" begin
    @eval module Aa
        export good, bad

        """
        Better to do nothing than to do something bad
        """
        good() = nothing

        bad() = Inf
    end

    @test hasdoc(Aa, :good)
    @test hasdoc(Aa, :bad) == false
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 4, 2023, 2:01pm UTC](https://discourse.julialang.org/t/check-if-a-function-has-a-docstring/103489/9 "2023-09-04T14:01:35Z")

</div>

> [@GHTaarn](#):
>
> Do you mean make a pull request addressing the issue?

Yes, I mean making a PR. This involves adding tests, documentation, going through the review process, etcetera.
