-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Replace -> with :: where appropriate in docstrings #57012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At some point I made a branch switching to -->
instead (but never made a PR). One of the goals was things like this, where what is returned seems more important than the type:
lmul!(a::Number, B::AbstractArray) --> B # mutates & returns 2nd arg
eigvals!(A, B; sortby) --> values::Vector # returns neither arg
I see the PR would make these eigvals!(A, B; sortby) -> values::Vector
. Perhaps that seems awkwardly almost-code (same complaint as the present ->
)? My idea was to make a smaller visual change, and move in the direction of clearly-not-syntax.
I also had these... which do parse as capturing :(s --> Bool)
but at least -->
has no definition, while s::Bool
does mean something which isn't true:
@isdefined s --> Bool
@__LINE__ --> Int
|
||
""" | ||
resize!(a::Vector, n::Integer) -> Vector | ||
resize!(a::Vector, n::Integer) -> a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or switched ... to
ret_name::RetT
.
Took a while to find an example of what exactly this meant.
If the reason to use ::
is that this is valid syntax, does this want to use ->
when it's not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to use -> a
because while it is not valid syntax, if it were, the only plausible literal interpretation would be correct up to omission of the implementation. ::Vector
would also be fine, though less helpful. -> Vector
, if interpreted literally, is wrong, hence the change.
I'm not as concerned with weather a docstring uses literal syntax as with weather, when interpreted as syntax, its semantics are correct.
What about using assignment syntax to name variables instead? B = lmul!(a::Number, B::AbstractArray)
values = eigvals!(A, B; sortby)::Vector
maxval, index = findmax(A; dims) |
Previous discussion on this at #22804 |
Reading the previous discussion and existing docstrings, I propose that
|
I like the idea from @mbauman to use → instead of -> to distinguish it from anonymous functions. If this is used, I'd also prefer having it consistently, even if only the type is specified:
|
I like the unicode arrow idea aside from the first case. The line at the end in |
Small nit: This is a bad idea B = lmul!(a::Number, B::AbstractArray) as one can see from the next example values = eigvals!(A, B; sortby)::Vector which could be rewritten as A = eigvals!(A, B; sortby)::Vector That is, in cases where memory is mutated, but the returned object is something different, one could use the same symbol and just redirect the variable binding. We should make clear that it's the very same |
Nice job taking this on @LilithHafner! I must say though that I really think |
From triage: write down a guide on what style is appropriate here (basically what this PR does) in a separate PR in the style guide. Then implement that style here. |
Sorry to be late to the party here. I wanted to put in a vote against using because although this matches well the meaning of callsite annotation ( And my fear is that by making the syntax """
foo(x::Int, y::AbstractBar)::Integer
Return a lovely integer
"""
function foo(x::Int, y::AbstractBar)::Integer
...
end which could have unintended perf consequences. I say this as someone who has had to go through a large codebase and remove many of these unintended type conversions that sometimes had perf costs (the original authors thought this syntax was a type assert). |
genuinely interested in your 👎 emoji @MasonProtter -- can you say more about it? Do you think the worry about the unintended consequence (i.e. declarations annotated with conversions becoming more prevalent) just isn't realistic? |
I'm sure it's possible for it to have unintended consequences, but I don't really believe that the cross-section for negative outcomes is worth having a confusing / inconsistent documentation style. I don't really see why someone reading a docstring that says """
foo(x::Int, y::AbstractBar)::Integer
Return a lovely integer
""" would cause them to write function my_function(x::T)::U
[...]
end as their implementation, and even if they did, it would take some pretty specific circumstances for that to actually have a noticable performance impact (i.e. the Ultimately, I just think it's useful for us to document expected return types, and I think that documenting them as """
foo(x::Int, y::AbstractBar) -> Integer
Return a lovely integer
""" is worse. |
If one is concerned that users would copy parts of the documentation, I'd say it is just as likely they may copy parts to the call site (as opposed to the definition site). Consequently, the suggested documentation style may just as well help users find broken edge cases:
|
The problem is that The main alternative I can think of is |
@JeffBezanson, I'm not sure if you're arguing against using When I use
This indicates that the thing that's returned is the tarball path, which may be passed in or a tempname that's generated for you. If the information that's conveyed can be expressed in actual Julia syntax, it should be expressed using actual syntax. If the information is something else, then I think it's fine to ues non-syntax, especially if we standardize a convention for it. I do think there's an undercurrent here that some people thing that the function return type with implicit conversion is a misfeature and don't want to encourage people to use it. While I think it may have been better for return type annotation to do assertion, I don't think what we have now is so bad. There's nothing wrong with using this feature in the language and there's nothing wrong with using it in the documentation or encouraging people to use it. |
FWIW I was talking with Jeff when he wrote that comment and AFAICT it is an argument against |
IMO this is ready to merge except for @mcabbott's #57012 (comment) |
In cases where a function is documented as ``` function(arg::ArgT, arg2::Arg2T) -> RetT ... ``` I either switched ` -> ` to `::` or switched `RetT` to `ret_name::RetT`. From the recommendation and justification from @nsajko here: JuliaLang#56978 (comment) and applied throughout the repo. As documented here JuliaLang#57583 Also includes some minor changes to touched lines (e.g. removing annotations that are just clutter)
In cases where a function is documented as
I either switched
->
to::
or switchedRetT
toret_name::RetT
.From the recommendation and justification from @nsajko here: #56978 (comment) and applied throughout the repo.