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

bpo-6761: Fix __call__ documentation #7987

Merged
merged 8 commits into from
Oct 27, 2020

Conversation

andresdelfino
Copy link
Contributor

@andresdelfino andresdelfino commented Jun 28, 2018

@vstinner
Copy link
Member

I removed the " needs backport to 3.6" label, the 3.6 branch no longer accept bugfixes (only security fixes are accepted): https://devguide.python.org/#status-of-python-branches

@andresdelfino
Copy link
Contributor Author

@bitdancer could you take a look at this?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer (arg1, arg2, ...) syntax rather than (arguments) syntax: IMHO (arg1, arg2, ...) is more explicit.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@andresdelfino
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@vstinner: please review the changes made to this pull request.

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vstinner
Copy link
Member

@csabella @JulienPalard: Would you mind to double check and/or merge this PR?

@vstinner
Copy link
Member

"needs backport 2.7"

I'm not super excited by continuing to enhance the 2.7 doc. IMHO it's good as it is. I mean, we are close to Python 2 end of life, and I prefer to read Python 3 doc to write Python 2 code. Well, it's just a comment, I'm also fine with backport to 2.7 if you want (but I expect a conflict).

@andresdelfino
Copy link
Contributor Author

This patch isn't incorrect, however, I'm wondering if something else is needed.

Looking at the original bug ticket, @bitdancer makes the point:

FYI, all special methods are (now) looked up on the type for new style classes.

Taking this a step further, I tried the original example from the tracker with __str__ instead of __call__ and it produced the same result (of proving David's point about special methods being looked up on type(x) instead of x.

This is already defined in the intro of this entire 3.3 section:

For instance, if a class defines a method named __getitem__(), and x is an instance of this class, then x[i] is roughly equivalent to type(x).__getitem__(x, i).

I'm wondering if the object.__call__ section should define what it does without referencing the x. The other sections (like __str__) avoid using an example instance.

I think you are right. Could you take a look at the new wording?

@csabella
Copy link
Contributor

csabella commented Mar 6, 2019

I'm definitely not the one who would be able to have the final say on the wording for this, but I wonder if something like this would work:

Called by ``object(arg1, arg2, ...)`` or an equivalent (such as a decorator) to invoke the instance as a function call.

@andresdelfino
Copy link
Contributor Author

@vstinner could you take a look on the new wording I wrote based on Cheryl's comments on #7987 (comment) and also Cheryl's proposed wording on #7987 (comment) and tell us what you think?

@vstinner
Copy link
Member

vstinner commented Mar 7, 2019

For me, obj(arg1, arg2, ...) is equivalant to (obj.__class__).__call__(obj, arg1, arg2, ...).

Usually, it's also equivalent to type(obj).__call__(arg1, arg2, ...), but I would prefer to use type() in the doc, since technically the builtin function can be overriden or a global variable with the same name can be defined.

@@ -2108,7 +2108,7 @@ Emulating callable objects
.. index:: pair: call; instance

Called when the instance is "called" as a function; if this method is defined,
``x(arg1, arg2, ...)`` is a shorthand for ``x.__call__(arg1, arg2, ...)``.
the class receives the instance and all arguments of the call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know what is supposed to be conveyed here, and I find this more confusing than the original. Diving into the internals of how calls to instance methods work is detracting from the discussion of what call does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your insight! Before doing any changes, I'll wait for @bitdancer to give his input.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This current version don't work for me, "if this method is defined, the class receives the instance and all arguments of the call." looks wrong, It's not the class that receives the instance and all arguments.

I'd propose something like:

Called when the instance is "called" as a function. Receives the instance and the function parameters.

If you want to almost keep the example you could add something like:

So ``obj(arg1, arg2, ...)`` calls ``__call__(arg1, arg2, ...)`` on ``obj``.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think that last suggestion (from Julien) works (call on obj?).

type(x).__call__(x, arg1, ...) is closer to what happens, and the call matches the way the method is defined (with a self parameter).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merwok I agree. Made a change based on your suggestion, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vstinner You said «I would prefer to [not] use type() in the doc, since technically the builtin function can be overriden» → that’s what we say «roughly»! Is the latest version good for you or you would prefer obj.__class__.__call__(obj, etc)?

@csabella I’m not sure I understand your review; could you comment on the latest version? Thanks!

@andresdelfino
Copy link
Contributor Author

andresdelfino commented Jun 15, 2019

@bitdancer Could you check the proposed wording out and comment? :)

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@merwok merwok added needs backport to 3.9 only security fixes and removed needs backport to 3.7 labels Oct 27, 2020
@merwok
Copy link
Member

merwok commented Oct 27, 2020

I updated the backports labels to get 3.9 and 3.8, and I think this is ready to merge unless another reviewer weighs in.

@vstinner vstinner merged commit 95f710c into python:master Oct 27, 2020
@miss-islington
Copy link
Contributor

Thanks @andresdelfino for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8, 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 27, 2020
(cherry picked from commit 95f710c)

Co-authored-by: Andre Delfino <[email protected]>
@bedevere-bot
Copy link

GH-23004 is a backport of this pull request to the 3.9 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 27, 2020
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 27, 2020
(cherry picked from commit 95f710c)

Co-authored-by: Andre Delfino <[email protected]>
@bedevere-bot
Copy link

GH-23005 is a backport of this pull request to the 3.8 branch.

@vstinner
Copy link
Member

Thanks @andresdelfino! It's a nice documentation enhancement. I'm fine with "roughly" ;-)

miss-islington added a commit that referenced this pull request Oct 27, 2020
(cherry picked from commit 95f710c)

Co-authored-by: Andre Delfino <[email protected]>
miss-islington added a commit that referenced this pull request Oct 27, 2020
(cherry picked from commit 95f710c)

Co-authored-by: Andre Delfino <[email protected]>
@andresdelfino andresdelfino deleted the bpo-6761-call-method branch October 27, 2020 16:51
shihai1991 added a commit to shihai1991/cpython that referenced this pull request Oct 29, 2020
…lots1

* origin/master: (365 commits)
  bpo-42029: Remove IRIX code (pythonGH-23023)
  bpo-42143: Ensure PyFunction_NewWithQualName() can't fail after creating the func object (pythonGH-22953)
  bpo-34204: Use pickle.DEFAULT_PROTOCOL in shelve (pythonGH-19639)
  bpo-41805: Documentation for PEP 585 (pythonGH-22615)
  bpo-42161: Micro-optimize _collections._count_elements() (pythonGH-23008)
  bpo-42161: Remove private _PyLong_Zero and _PyLong_One (pythonGH-23003)
  bpo-42099: Fix reference to ob_type in unionobject.c and ceval (pythonGH-22829)
  bpo-41659: Disallow curly brace directly after primary (pythonGH-22996)
  bpo-6761: Enhance __call__ documentation (pythonGH-7987)
  bpo-42161: Modules/ uses _PyLong_GetZero() and _PyLong_GetOne() (pythonGH-22998)
  bpo-41474, Makefile: Add dependency on cpython/frameobject.h (pythonGH-22999)
  bpo-42157: Rename unicodedata.ucnhash_CAPI (pythonGH-22994)
  bpo-42161: Use _PyLong_GetZero() and _PyLong_GetOne() (pythonGH-22995)
  bpo-30681: Support invalid date format or value in email Date header (pythonGH-22090)
  bpo-42161: Add _PyLong_GetZero() and _PyLong_GetOne() (pythonGH-22993)
  bpo-42123: Run the parser two times and only enable invalid rules on the second run (pythonGH-22111)
  bpo-42157: Convert unicodedata.UCD to heap type (pythonGH-22991)
  bpo-42157: unicodedata avoids references to UCD_Type (pythonGH-22990)
  bpo-39101: Fixes BaseException hang in IsolatedAsyncioTestCase. (pythonGH-22654)
  bpo-1635741: _PyUnicode_Name_CAPI moves to internal C API (pythonGH-22713)
  ...
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
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.