This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Clarify documentation for binary arithmetic operation subclass __r*__ precedence
Type: Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: brett.cannon Nosy List: Eric Wieser, brett.cannon, docs@python, miss-islington, veky
Priority: normal Keywords: patch

Created on 2020-08-18 17:56 by brett.cannon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22505 merged brett.cannon, 2020-10-03 00:17
PR 22568 merged miss-islington, 2020-10-05 16:42
PR 22569 merged miss-islington, 2020-10-05 16:42
Messages (8)
msg375621 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-08-18 17:56
https://docs.python.org/3/reference/datamodel.html#object.__ror__ has a note saying:

"If the right operand's type is a subclass of the left operand's type and that subclass provides the reflected method for the operation, this method will be called before the left operand's non-reflected method." The slightly unclear part (at least to me) is the "provides the reflected method."

What this actually means according to https://bugs.python.org/issue30140 is that the subclass **implements** the `__r*__` method, not just that the method is reachable on the subclass via getattr(). That wasn't clear to me when I initially read this.
msg375622 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-08-18 17:59
Or to be more clear, the RHS subclass must provide a **different** implementation of the method than the LHS.
msg375645 - (view) Author: Vedran Čačić (veky) * Date: 2020-08-19 12:22
Yes, there is a corner case here. If A derives from B and B derives from C, A doesn't implement __rsub__ and B does, A()-C() will call B's __rsub__.
msg375646 - (view) Author: Vedran Čačić (veky) * Date: 2020-08-19 12:26
Sorry, of course I meant C()-A(). The point is that the class A _doesn't_ have to implement __rsub__.
msg375668 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-08-19 18:37
Right, there is a `lhs.__rsub__ is not rhs.__rsub__` check which needs to be clearly communicated in that note.
msg378063 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-10-05 16:42
New changeset d02d824e05e2cb86f4df381be18832e76e2c475f by Brett Cannon in branch 'master':
bpo-41584: clarify when the reflected method of a binary arithemtic operator is called (#22505)
https://github.com/python/cpython/commit/d02d824e05e2cb86f4df381be18832e76e2c475f
msg378065 - (view) Author: miss-islington (miss-islington) Date: 2020-10-05 16:52
New changeset 31ceccb2c77854893f3a754aca04bedd74bedb10 by Miss Skeleton (bot) in branch '3.8':
bpo-41584: clarify when the reflected method of a binary arithemtic operator is called (GH-22505)
https://github.com/python/cpython/commit/31ceccb2c77854893f3a754aca04bedd74bedb10
msg378067 - (view) Author: miss-islington (miss-islington) Date: 2020-10-05 17:05
New changeset 4aad1e5770fab72908f922a7876075d3d5a7c0d0 by Miss Skeleton (bot) in branch '3.9':
bpo-41584: clarify when the reflected method of a binary arithemtic operator is called (GH-22505)
https://github.com/python/cpython/commit/4aad1e5770fab72908f922a7876075d3d5a7c0d0
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85750
2020-10-05 18:34:03brett.cannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-10-05 17:05:58miss-islingtonsetmessages: + msg378067
2020-10-05 16:52:10miss-islingtonsetmessages: + msg378065
2020-10-05 16:42:48miss-islingtonsetpull_requests: + pull_request21565
2020-10-05 16:42:39brett.cannonsetmessages: + msg378063
2020-10-05 16:42:37miss-islingtonsetnosy: + miss-islington

pull_requests: + pull_request21564
stage: commit review -> patch review
2020-10-03 00:17:58brett.cannonsetstage: patch review -> commit review
2020-10-03 00:17:07brett.cannonsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request21516
2020-08-19 18:37:37brett.cannonsetmessages: + msg375668
2020-08-19 12:26:32vekysetmessages: + msg375646
2020-08-19 12:22:22vekysetnosy: + veky
messages: + msg375645
2020-08-19 08:32:09Eric Wiesersetnosy: + Eric Wieser
2020-08-18 17:59:41brett.cannonsetmessages: + msg375622
2020-08-18 17:56:56brett.cannonsetassignee: docs@python -> brett.cannon
2020-08-18 17:56:34brett.cannoncreate