classification
Title: __pow__ and __rpow__ are not reached when __ipow__ returns NotImplemented for **=
Type: behavior Stage: patch review
Components: Interpreter Core Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: DeepSpace, ammar2, brett.cannon, hongweipeng, serhiy.storchaka
Priority: release blocker Keywords: patch

Created on 2019-09-27 21:55 by DeepSpace, last changed 2020-09-01 20:09 by brett.cannon.

Pull Requests
URL Status Linked Edit
PR 16459 open ashkop, 2019-09-28 11:57
Messages (7)
msg353419 - (view) Author: Adi (DeepSpace) Date: 2019-09-27 21:55
Due to shared code between the 2 and 3 forms of pow, the following code causes a TypeError:

class A:
    def __init__(self, val):
        self.val = val
    
    def __ipow__(self, other):
        return NotImplemented

class B:
    def __init__(self, val):
        self.val = val
    
    def __rpow__(self, other):
        return A(other.val ** self.val)

a = A(2)
b = B(2)
a **= b


(https://stackoverflow.com/questions/58141475/ipow-raising-typeerror-when-left-hand-side-object-returns-notimplemented)
msg353422 - (view) Author: Adi (DeepSpace) Date: 2019-09-27 22:00
Meant to say "... 2 and 3 arguments forms of pow, ..."
msg353503 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-09-29 16:13
The document says(https://docs.python.org/3.9/reference/datamodel.html?highlight=__rpow__#object.__rpow__):
>Note that ternary pow() will not try calling __rpow__() (the coercion rules would become too complicated).
msg353505 - (view) Author: Ammar Askar (ammar2) * (Python triager) Date: 2019-09-29 16:55
This isn't the ternary form of pow(), the documentation there is referring to the `pow(base, exp, modulus)`.
msg353595 - (view) Author: hongweipeng (hongweipeng) * Date: 2019-09-30 16:51
Oh, I see. Thank you.
msg375825 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-08-23 19:37
It turns out **= ONLY calls __ipow__ and neither __pow__ or  __rpow__ as the data model says should be called.

- Data Model: https://docs.python.org/3/reference/datamodel.html#object.__ipow__
- PyNumber_InPlacePower(): https://github.com/python/cpython/blob/802726acf6048338394a6a4750835c2cdd6a947b/Objects/abstract.c#L1159
- ternary_op (which is what is used to implement PyNumber_InPlacePower(): https://github.com/python/cpython/blob/802726acf6048338394a6a4750835c2cdd6a947b/Objects/abstract.c#L849
msg376208 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-09-01 20:09
I have opened https://bugs.python.org/issue41688 to track the documentation fixes so that this can become a release blocker for Python 3.10.
History
Date User Action Args
2020-09-01 20:09:07brett.cannonsetpriority: high -> release blocker

messages: + msg376208
versions: - Python 3.8, Python 3.9
2020-08-23 19:37:28brett.cannonsetpriority: normal -> high
title: __rpow__ not reached when __ipow__ returns NotImplemented -> __pow__ and __rpow__ are not reached when __ipow__ returns NotImplemented for **=
nosy: + brett.cannon

messages: + msg375825

versions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.6, Python 3.7
2019-09-30 16:51:08hongweipengsetmessages: + msg353595
2019-09-29 16:55:33ammar2setnosy: + ammar2
messages: + msg353505
2019-09-29 16:13:12hongweipengsetnosy: + hongweipeng
messages: + msg353503
2019-09-28 11:57:01ashkopsetkeywords: + patch
stage: patch review
pull_requests: + pull_request16041
2019-09-28 07:27:18serhiy.storchakasetnosy: + serhiy.storchaka
2019-09-27 22:00:16DeepSpacesetmessages: + msg353422
2019-09-27 21:58:57DeepSpacesettype: behavior
2019-09-27 21:55:15DeepSpacecreate