fix docstring search by signatures #53824
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The problem
I think the signatures generated for docstrings create problems when type parameters are involved. This can be seen when searching docstrings based on signatures. My understanding of the intended behavior is that only docstrings matching the given signature should be displayed or, if none exists, all docstrings for the given function. This works well without type parameters, but not with them. I reported this already in #52669, but I didn’t get any response. The present PR intends to fix this issue. The underlying problem is somewhat subtle, so I try to explain it in detail.
EDIT: The builds fail because a change in
doc/src/stdlib/SparseArrays.md
is needed, see below.Examples
displays the two docstrings
although the first one doesn't match the signature.
displays
although only the last one matches.
What is going wrong?
Here are the signatures used by the help system for the examples above:
The third entry for
filter
comes fromjulia/base/array.jl
Line 2875 in d68a04e
as can be seen via
The type parameters
T
andN
have been added as signatures, which leads to wrong search results.The first entry for
NamedTuple
comes fromjulia/base/docs/basedocs.jl
Line 3306 in d68a04e
Here the same happens with the parameters
names
andT
, even in the absence of awhere
clause:The problematic code
The signatures attached to docstrings are generated by the function
Base.Docs.signature!
. PR #21036 added among other things the following statements:julia/base/docs/Docs.jl
Lines 91 to 96 in f65b8ab
The
for
loop adds type parameters fromwhere
clauses as additional signatures, which was the problem forfilter
. In the absence ofwhere
clauses, theif
statement does the same with:curly
expressions. We have seen this forNamedTuple
.Proposed solution
The PR deletes these two loops. As far as I can tell, searching docstrings by signatures then works as expected. However, docstrings attached to constructors like
cannot be distinguished anymore because the signatures of the arguments are the same. Attaching a docstring to the second call replaces the first one. At present they can be distinguished, and this may have been the motivation for #21036.
That having different docstrings for constructors differing only by a type parameter could have been the original motivation is also indicated by two tests in
test/docs.jl
. One checks that the signatures have the (now problematic) form, and the other one checks that different docstrings can be attached to constructors like theNamedTuple
example above. I’ve modified the former test and marked the other one as@test_broken
.NamedTuple
is the only case where I have seen this problem. (I've compiled Julia and loaded all standard libs.) I've merged the corresponding docstrings into one. I have also changed two lines in the documentation.Also, docstrings attached to constructor calls with parameters must now use a
where
clause if the parameter is used for the arguments. For example,is currently allowed, but would have to be written
For this reason, one needs the following additional change to build the documentation. The file is from a different git repository, see here.
With this change the documentation builds without problems.
Changing less?
(ADDED) One could avoid the changes just mentioned if one kept part of the code that I delete, namely the lines that treat
:curly
expressions likewhere
clauses. This is problematic, however, because on the level of expressions free parameters cannot be distinguished from constant parameters. Consider the following example:The signature for the first docstring would be converted to
Tuple{Int} where Int
, which is the same asTuple{Any}
. Hencehelp?> A{Int}(1.0)
would display both docstrings although the first one doesn't match. (This is also the current behavior.)Conclusion
I think that the current situation is not desirable and should be changed. Whether the proposed PR is the right way to go is up for debate. That docstrings can be attached to function calls (instead of method definitions) is not documented, I believe. In this sense one could say that the change made by this PR would be non-breaking. In any case, let me know what you think.