-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-125897: Fix range function doc referring to step as a kwarg #125945
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
Open
kbaikov
wants to merge
1
commit into
python:main
Choose a base branch
from
kbaikov:fix-issue-125897
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
8000
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
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 have an issue with this syntax because, in this signature,
stop
is actuallystart
and is only treated asstop
because 1 arg is passed. While I understand that, in the second signature, it is intended to convey thatstep
is a positional arg with a default value, you cannot treat the first signature the same. At least I think not. I'm happy to be proven wrong and learn something new. How would you writestart=0
in the first signature?IMO, this is completely irrelevant. Yes, that is how
range
functions internally, but, for the average reader (myself included), I think it would be less confusing to just have 3 signatures if the intent is to avoid the[, step]
syntax.range(stop)
range(start, stop)
range(start, stop, step)
It may also be pertinent to rephrase the description as well.
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.
No. Stop is stop and start is start. The point is that this function has multiple signatures. Each signature is a valid pure-python function signature. Closest pure-python version, I think, is the typing.overload.
There is no start argument in the first signature.
The problem is that this signature means you can do
range(stop=123)
, which is wrong. The/
syntax might be boring, but this is a part of the language, long time ago.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.
Sure, but I am describing how it works internally. To me, it seems inconsistent that the description explains the behavior under-the-hood but then makes a "special case" for the second signature. Why not just have 3 and explain that in the first signature you start from 0? Why confuse the reader with "If the start argument is omitted..." as if it has a single signature?
Well, you can, but you'll get a
TypeError
. Nothing is stopping you from doing it with/
, either. Are we expecting folks to not understand thatstop
is a positional arg without the/
?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.
Could you propose alternative version for the documentation of range()?
That means you can't.
Language syntax. It tells me, that function doesn't support this.
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.
IMO, the signature(s) should be consistent with the behavior of the function and with each other.
Internally,
range
is overloaded:range(stop, /)
range(start, stop, /)
range(start, stop, step, /)
I have never read a signature like
range(stop)
and thought to myself to call it likerange(stop=123)
. I have always understood, at-a-glance, that this is a positional arg and should be called likerange(123)
(even though it can be called likestop=123
, just not forrange
).The same is true of
range(start, stop, step=1)
. That reads, to me, at-a-glance, thatstep
is a keyword arg, not a positional arg with a default value.In fact,
range
agrees with me!The only time I would read
step
as a positional arg with a default value is the signaturerange(start, stop, step=1, /)
; however, you cannot represent the other signature with default values likerange(start=0, stop, step=1, /)
. This is inconsistent.If the goal is to write signatures in such a way that language like "If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0." is not necessary then I don't think
range(stop, /)
accomplishes that. Why does one get to be "fully described" but not the other? Unfortunately, I don't think this is possible forrange
as-is.P.S.
You linked to 4.9. More on Defining Functions in your comment and after reading it I'm left wondering: What the hell is the difference between a positional arg with a default value and a keyword arg?
According to this, the answer is: "positional argument: an argument that is not a keyword argument."
😅
I guess we should all be writing code and documentation using
/
and*
.Anyway, I'm sure you are all much smarter than me and will choose the best option.
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.
Thanks @JelleZijlstra. @travis-pds Hmm... I tend to take the approach of user first. Undesirable is too strong a word. It was not ideal when 3.8 was released. Time has passed and @JelleZijlstra has captured well the current thinking. I tend to read most of the decisions as in most cases... yet, it may make sense in some cases to do something else if it benefits readability and comprehension.
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.
Hence I am asking why only
range
should get the slashes again and not all other functions that lost them in #99476? Like e.g.bool(x=False, /)
?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.
@chris-eibl, this pr address concrete issue with range, not about mechanical reversion of #99476.
Please open issue.
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.
Just for
bool
? This does not seem consistent to me, but the mechanical reversion does not seem to be liked either.Therefore, I leave it to you core dev(s) to decide which and how many issues to create, since there are more canditates than
bool
.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.
CC @willingc @JelleZijlstra
Now sphinx docs shows tooltips for syntax characters in signatures:


Is there something controversial in the pr now?