← Back to Pull requests

Fix docstring search by signatures revisited #54324

Open
projekter opened this pull request with 54 lines diff across 6 files
View on GitHub
projekter projekter commented on May 01, 2024 17:51 UTC
Author

This is an extension of #53824 (therefore also fixes #52669), where details on the issue can be found (and, related to this, a different area where the issue shows, namely when you try to include documentation for a particular method of a function, which is similarly impossible when type parameters are involved).
The fix proposed there had two drawbacks:

  • with it, it is no longer possible to discriminate between two constructor functions that differ only in the type parameters. This is remedied with this PR by adding the parameters of the constructor as a unique type to the front of the method signature.

  • with it, constructors must specify type parameters explicitly. The previous behavior is restored with this PR, which allows to have the type parameters unspecified.

  • However, I completely agree with @matthias314's analysis that in this way, it is impossible to make the distinction between existing types and parametric types (which cannot be done well during macro deconstruction). This is also an issue in the code currently in Base; any type parameter will always be seen as parametric. For this reason, I'd rather see this whole behavior disappear; in the end, you cannot write Julia code like this. Removing the if isempty would prevent such a usage. A kind of middle ground would be to allow the current behavior, but only if the type is constrained (because this would not be possible for existing types).

    ---

    UPDATE: several more issues are fixed by this:
    Fixes #29437
    Fixes #41601

    Diff: 54 lines (+26 / -28) across 6 files Reviewer: LilithHafner Review duration: 1364902m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.41) × (1 − Review Confidence (0.91)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 54 lines diff across 6 files
    0.41
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.94 · 5 substantive comment(s)
    0.94
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    90%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.240
    Real PR Conversation Timeline
    13 items
    LilithHafner LilithHafner commented on code on May 10, 2024 11:29 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    These changes seem reasonable/good/bugfixes, but I'm worried how breaking they might be to docsystem building across the ecosystem.

    LilithHafner LilithHafner commented on May 10, 2024 11:36 UTC
    Reviewer

    Thanks for making this PR! I can't do a full review right now, but hopefully we can find someone who is willing to.

    matthias314 matthias314 commented on code on May 10, 2024 13:17 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    Is there a way to figure this out? From some JuliaCon talks for example I get the impression that it is possible to test many or all packages with a modified Julia version and see what happens. If the documentation of a package would require changes, then this would result in an error during precompilation or when building the docs.

    LilithHafner LilithHafner commented on code on May 10, 2024 13:58 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    There is nanosoldier, but that only runs testsuites which often do not include building docs.

    matthias314 matthias314 commented on code on May 10, 2024 14:17 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    In Base, most changes occur in docstrings (as opposed to separate documentation files), and a missing change gives an error during precompilation. So with nanosoldier we might still get an idea of how big the problem really is.

    projekter projekter commented on code on May 12, 2024 15:44 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    I would also have thought that this issue should be detected by Nanosoldier.
    The only way to circumvent this possible incompatibility is if we decide to formally allow this syntax for functions. (But then, it should be documented somewhere.)
    Then, it should also formally be defined show this syntax is interpreted. If we just say that in the documentation, fun{X}(args) is _exactly_ equivalent to fun(args) where {X}, then it is clear that everything in X is a type variable (with possible subtyping). Apart from the case in which fun is the name of a type/constructor, as then it could also be a concrete type (or some other bitstype). I previously thought that it is not easily possible for the macro-called function determining which is the case, but as I now think of it, I'm no longer so sure. For outer constructors, for sure when the macro is called, the type must already exist. And in order for the Julia compiler to accept fun{X} without X being in the where clause, X must also be well defined (in the context of the calling module, which is available to the macro). I don't know how it is when the docstring is attached to an inner constructor, though.
    Then, in the specification of what fun{X}(args) means when fun is a constructor, should we allow mixing actual generic types (coming first?) with parametric types from the arguments (pretty clumsy IMHO)? Or just disallow this shorthand notation?

    matthias314 matthias314 commented on May 12, 2024 22:59 UTC
    Contributor

    [I'm moving the discussion out of the code review because it has a wider scope]

    In my opinion, we should not allow the syntax f{T}(x::T) for docstrings if f is a function (as opposed to a constructor). For constructors, I also find it bad syntax because it cannot distinguish between actual types and type parameters. If T is a parameter, then one should write f{T}(x::T) where T. However, the currently allowed syntax might be so widespread in the existing documentation that it might be better not to change it. After all, replacing a type parameter by an actual type should not happen often. I still think that it would be helpful to run nanosoldier (or whatever tool is appropriate) over a large collection of packages to see how much would break with disallowing this syntax.

    fingolfin fingolfin commented on code on Oct 22, 2025 13:29 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    I am also concerned about breakage in the ecosystem, on the other hand, there is a also real bug here that keep coming up again and again, and surely we must be able to deal with it somehow?

    What are the possible failure modes here? I am a bit confused by the discussion here. Which of these can occur here:
    1. The changes here could lead to makedocs invocations that previously worked now erroring out?
    - I think we could live with that; package authors would be annoyed, but if we give a clear guidance about this change and how to deal with it in the release notes, and why it was necessary, it seems acceptable.
    2. The changes here could lead to Julia code not (pre)compiling anymore?
    - Similar conclusion as in 1, but we can at least get a rough idea about it via NanoSoldier
    3. The changes here could lead to silent breakage, e.g. manuals suddenly don't include certain docstrings
    - that's the annoying kind, it could even lead to projects making several release with broken documentation without realizing.

    Any other relevant failure modes I am forgetting here?

    fingolfin fingolfin commented on code on Oct 22, 2025 13:30 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    In Base, most changes occur in docstrings (as opposed to separate documentation files), and a missing change gives an error during precompilation.

    This line confuses me: looking at this PR, it's the other way around: most changes here are in the .md files, and one is in a docstring. What gives?

    fingolfin fingolfin commented on code on Oct 22, 2025 13:34 UTC
    Code Review
    base/docs/basedocs.jl:1
    @@ -3579,18 +3579,14 @@ Tuple
     
     """
         NamedTuple{names}(args::Tuple)
    -
    -Construct a named tuple with the given `names` (a tuple of Symbols) from a tuple of values.
    -"""
    -NamedTuple{names}(args::Tuple)
    -
    -"""
         NamedTuple{names,T}(args::Tuple)
     
    -Construct a named tuple with the given `names` (a tuple of Symbols) and field types `T`
    +In the first form, construct a named tuple with the given `names` (a tuple of Symbols) from a tuple of values.
    +In the second form, construct a named tuple with the given `names` (a tuple of Symbols) and field types `T`
     (a `Tuple` type) from a tuple of values.
     """
    -NamedTuple{names,T}(args::Tuple)
    +NamedTuple{names}(args::Tuple)
    +# NamedTuple{names,T}(args::Tuple)   # redundant because of the same signature

    With this, PR, ?NamedTuple{names,T}, ?NamedTuple{names} and `?NamedTuple all give the same output, which includes four docstrings,

      NamedTuple(itr)
    
      NamedTuple{names}(nt::NamedTuple)
    
      NamedTuple{names}(args::Tuple)
      NamedTuple{names,T}(args::Tuple)
    
      NamedTuple
    

    Is that intended?

    projekter projekter commented on code on Oct 22, 2025 18:48 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    It has been a while, but here's what I think I remember:

  • The issue is that in the current implementation, generic methods are not identified properly.

  • Therefore it is not always possible to refer to them uniquely. So if you currently want to include a specific method in the documentation (which would be a @docs block) or you want to [...](@ref something(concrete)) a particular method, this might not always work out.

  • By changing how the methods are identified, we fix this problem. However, as a consequence, now Documenter will only understand references that use proper syntax as Julia does and will reject things that would not compile in the normal code.

  • So what will break: Explicit references to methods in a @docs block that used wrong syntax and links within docstrings. I don't think that this will lead to an error in precompilation: The markdown files are irrelevant for precompilation anyways, they are "external" documentation. And if you have links within docstrings, well, they are just strings until you actually want to generate some hypertext out of it. So the links may be broken in the documentation, which might give a warning or an error depending on your makedocs configuration, but it should not affect precompilation, same for markdown.

  • What it will also break: If you explicitly use the Docs module to find the documentation based on a method type signature which uses the previously-used Union structure. Which should be pretty rare. Why would you ever care to programmatically get the docstring of a particular method? Probably only if you want to copy this docstring for another method (which is how I arrived at this bug, as far as I remember). And if you used the previous internal representation, I'm pretty sure that this was an implementation detail and therefore can change without notice. And REPL of course also uses the Docs system, since I have no idea how REPL internally does this, maybe it needs some adjustments.
  • So indeed, I also don't understand @matthias314's statement about the changes in docstrings in Base.

    matthias314 matthias314 commented on code on Oct 23, 2025 01:09 UTC
    Code Review
    doc/src/base/c.md:1
    @@ -13,11 +13,11 @@ Base.unsafe_store!
     Base.unsafe_modify!
     Base.unsafe_replace!
     Base.unsafe_swap!
    -Base.unsafe_copyto!{T}(::Ptr{T}, ::Ptr{T}, ::Any)
    +Base.unsafe_copyto!(::Ptr{T}, ::Ptr{T}, ::Any) where T

    I have to admit that don't understand that comment myself anymore!

    Regarding breakages: If I understand correctly, @projekter would also prefer to disallow type parameters that are not explicitly specified. I think it would be instructive to see how many packages would break if we did this. We could either take this temporarily out of this PR or by use my original PR and then run nanosoldier.

    Reviewers
    Assignees
    projekter projekter
    Labels
    risk: low confidence: 90.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.3% (MEDIUM) · Higher = larger blast radius
    41.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 90.6% · Lower = rushed or shallow review
    90.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 3.9% · Safe to merge
    3.9%