Skip to content
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

gh-110383: Update dict get, fromkeys, and setdefault signatures in docs #119330

Merged

Conversation

johnlandonwood
Copy link
Contributor

@johnlandonwood johnlandonwood commented May 21, 2024

Small update to address "list of errata: 2" in #110383, by updating the signature of dict methods get, pop, fromkeys, and setdefault in the docs.


📚 Documentation preview 📚: https://cpython-previews--119330.org.readthedocs.build/

Copy link

cpython-cla-bot bot commented May 21, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news awaiting review labels May 21, 2024
@encukou
Copy link
Member

encukou commented May 21, 2024

Thank you!

Please also remove the square brackets: those are another way of saying that the argument is optional, they aren't needed if the default value is given.

This change makes the get docs inconsistent with fromkeys, pop, and setdefault. Do you want to update those as well?

@erlend-aasland
Copy link
Contributor

Thanks for the PR! Changes like this are ... controversial. There's a group of core devs who prefer the (a[, b]) signatures, and there's a groupe of core devs who prefer the (a, b=None) signatures. The problem with the former is that they do not work well with inspect, so a lot of people want to change inspect instead.

Moreover, this PR introduces an inconsistency when we compare get() to the surrounding documented methods. The "bracket-without-default" notation is predominantly used, so iff this should be changed, one would have to take this whole section into account.

So, sorry, I think we have to reject this particular change for now. IMO, it should not be in the TODO list in the issue; changes like this are controversial, and thus no good match as a beginner PR/issue :(

@johnlandonwood
Copy link
Contributor Author

@encukou and @erlend-aasland - thank you for the feedback! No worries, I wasn't aware of the controversy around those notations. I just saw it in that issue and it looked like a layup, but things are often more complex than they seem. I appreciate the context :)

@erlend-aasland
Copy link
Contributor

I just had a talk with Petr, and I see now that this particular change is not in the controversial camp :) It does make sense to align the docs with the docstring; so let's land this!

@erlend-aasland erlend-aasland added needs backport to 3.12 bug and security fixes needs backport to 3.13 bugs and security fixes labels May 21, 2024
@johnlandonwood
Copy link
Contributor Author

johnlandonwood commented May 21, 2024

I just had a talk with Petr, and I see now that this particular change is not in the controversial camp :) It does make sense to align the docs with the docstring; so let's land this!

Great to hear! I just updated get and the other surrounding methods (fromkeys, pop, and setdefault) for consistency. Let me know if anything looks off.

@johnlandonwood
Copy link
Contributor Author

johnlandonwood commented May 21, 2024

Wait, one point of confusion - should the dosctring for pop be updated too, to mention that None is the default? Or does the raising of the KeyError cover that?

@johnlandonwood johnlandonwood changed the title gh-110383: Update default to default=None in dict .get() doc gh-110383: Update dict get, pop, fromkeys, and setdefault signatures in docs May 21, 2024
@erlend-aasland
Copy link
Contributor

Wait, one point of confusion - should the dosctring for pop be updated too, to mention that None is the default? Or does the raising of the KeyError cover that?

Well, it is not that easy; you'd have to change the Argument Clinic input and regenerate the parsing code:

cpython/Objects/dictobject.c

Lines 4377 to 4395 in b7f45a9

/*[clinic input]
dict.pop
key: object
default: object = NULL
/
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If the key is not found, return the default if given; otherwise,
raise a KeyError.
[clinic start generated code]*/
static PyObject *
dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
/*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
{
return _PyDict_Pop((PyObject*)self, key, default_value);
}

So, that change would look like this1:

diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index 985a326a176..c976a0c868f 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -4378,7 +4378,7 @@ dict_clear_impl(PyDictObject *self)
 dict.pop
 
     key: object
-    default: object = NULL
+    default: object = None
     /
 
 D.pop(k[,d]) -> v, remove specified key and return the corresponding value.

Footnotes

  1. combined with make clinic

@erlend-aasland
Copy link
Contributor

I think we should consider leaving the pop() change out; what do you think, @encukou? Contrary to the other methods, pop() does not actually default to None; in the implementation it defaults to NULL.

@rhettinger
Copy link
Contributor

Except for pop, these look fine to me.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
@erlend-aasland erlend-aasland enabled auto-merge (squash) May 21, 2024 20:47
@erlend-aasland erlend-aasland changed the title gh-110383: Update dict get, pop, fromkeys, and setdefault signatures in docs gh-110383: Update dict get, fromkeys, and setdefault signatures in docs May 21, 2024
@erlend-aasland
Copy link
Contributor

Thank you, @johnlandonwood, and congrats on landing your first PR :)

@johnlandonwood
Copy link
Contributor Author

Thank you, @johnlandonwood, and congrats on landing your first PR :)

No, thanks to you and the others for what you do! Feels great!

@rhettinger
Copy link
Contributor

rhettinger commented May 21, 2024

Hmm, I think I remember why these got skipped in all of the past sweeps. The issue was that the bracketless form implied that keyword arguments could be used. Also, there was a decision to avoid use / in signatures in the docs because it tends to confuse users.

Also, it looks like the actual help signatures sometimes look terrible and we don't want that to spill to the main docs:

>>> help(dict.pop)
Help on method_descriptor:

pop(self, key, default=<unrepresentable>, /) unbound builtins.dict method
    D.pop(k[,d]) -> v, remove specified key and return the corresponding value.

    If the key is not found, return the default if given; otherwise,
    raise a KeyError.

Go ahead and do want you want. I don't have an opinion. I just know that these were intentionally not changed during past sweeps.

@erlend-aasland erlend-aasland merged commit 0e3c8cd into python:main May 22, 2024
38 checks passed
@miss-islington-app

This comment has been minimized.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 22, 2024
…s with docstrings (pythonGH-119330)

(cherry picked from commit 0e3c8cd)

Co-authored-by: Landon Wood <landon@elkrange.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 22, 2024
…s with docstrings (pythonGH-119330)

(cherry picked from commit 0e3c8cd)

Co-authored-by: Landon Wood <landon@elkrange.com>
@bedevere-app
Copy link

bedevere-app bot commented May 22, 2024

GH-119370 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label May 22, 2024
@bedevere-app
Copy link

bedevere-app bot commented May 22, 2024

GH-119371 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label May 22, 2024
erlend-aasland pushed a commit that referenced this pull request May 22, 2024
…cs with docstrings (GH-119330) (#119371)

(cherry picked from commit 0e3c8cd)

Co-authored-by: Landon Wood <landon@elkrange.com>
erlend-aasland pushed a commit that referenced this pull request May 22, 2024
…cs with docstrings (GH-119330) (#119370)

(cherry picked from commit 0e3c8cd)

Co-authored-by: Landon Wood <landon@elkrange.com>
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants