Skip to content

add a way to test whether exported symbols are documented #51174

@stevengj

Description

@stevengj

As discussed on discourse, it would be nice to have a (documented) way to test whether all exported symbols in a module have docstrings. Currently, this seems to require undocumented internals?

Something like:

# hasdoc should go in Base.Docs; might also be accomplished by refactoring Base.Docs.doc somehow
using  Base.Docs: Binding, defined, getdoc, resolve, meta, modules, aliasof
hasdoc(mod::Module, sym::Symbol) = hasdoc(Base.Docs.Binding(mod, sym))
function hasdoc(binding::Binding, sig::Type = Union{})
    defined(binding) && !isnothing(getdoc(resolve(binding), sig)) && return true
    for mod in modules
        dict = meta(mod; autoinit=false)
        !isnothing(dict) && haskey(dict, binding) && return true
    end
    alias = aliasof(binding)
    return alias == binding ? false : hasdoc(alias, sig)
end

# possible function for the Test module
function check_documented(mod::Module)
    nodocs = filter!(sym -> !hasdoc(mod, sym), names(mod))
    isempty(nodocs) || error("missing docstrings in $mod: $nodocs")
end

For example, this catches some missing docstrings in Base and several standard libraries:

julia> check_documented(Base)
ERROR: missing docstrings in Base: [:CanonicalIndexError, :CapturedException, :InvalidStateException]

julia> check_documented(LinearAlgebra)
ERROR: missing docstrings in LinearAlgebra: [:ColumnNorm, :LAPACKException, :NoPivot, :RankDeficientException, :RowMaximum, :RowNonZero, :copy_transpose!]

julia> check_documented(Markdown)
ERROR: missing docstrings in Markdown: [Symbol("@doc_str"), Symbol("@md_str"), :html, :latex]

julia> check_documented(Pkg)
ERROR: missing docstrings in Pkg: [Symbol("@pkg_str"), :PKGMODE_MANIFEST, :PKGMODE_PROJECT, :PRESERVE_ALL, :PRESERVE_ALL_INSTALLED, :PRESERVE_DIRECT, :PRESERVE_NONE, :PRESERVE_SEMVER, :PRESERVE_TIERED, :PRESERVE_TIERED_INSTALLED, :Pkg, :PreserveLevel, :Registry, :UPLEVEL_MAJOR, :UPLEVEL_MINOR, :UPLEVEL_PATCH]

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsystemThe documentation building systemfeatureIndicates new feature / enhancement requestsgood first issueIndicates a good issue for first-time contributors to JuliatestsystemThe unit testing framework and Test stdlib

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions