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: [3.10] __pow__ and __rpow__ are not reached when __ipow__ returns NotImplemented for **=
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: DeepSpace, ammar2, brett.cannon, hongweipeng, pablogsal, serhiy.storchaka
Priority: release blocker Keywords: patch

Created on 2019-09-27 21:55 by DeepSpace, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16459 merged ashkop, 2019-09-28 11:57
PR 24587 closed brett.cannon, 2021-02-19 23:25
Messages (10)
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 committer) 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.
msg386108 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2021-02-01 20:50
Friendly reminder that this issue is currently blocking the 3.10a5 release. If you are ok with waiting for the next release to include the fix, please say so here or drop me an email/
msg386124 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2021-02-02 00:30
I'm totally fine with pushing this until b1 since this has been broken for  ages.
msg387741 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2021-02-26 19:58
New changeset cc02b4f2e810ab524d845daa18bc94df5b092dd8 by Alex in branch 'master':
bpo-38302: __pow__/__rpow__ now called when __ipow__ returns NotImplemented (#16459)
https://github.com/python/cpython/commit/cc02b4f2e810ab524d845daa18bc94df5b092dd8
History
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82483
2021-02-26 19:59:01brett.cannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-02-26 19:58:49brett.cannonsetmessages: + msg387741
2021-02-19 23:25:31brett.cannonsetpull_requests: + pull_request23367
2021-02-02 00:30:52brett.cannonsetmessages: + msg386124
2021-02-01 20:50:33pablogsalsetnosy: + pablogsal
messages: + msg386108
2020-10-04 16:24:13lukasz.langasettitle: __pow__ and __rpow__ are not reached when __ipow__ returns NotImplemented for **= -> [3.10] __pow__ and __rpow__ are not reached when __ipow__ returns NotImplemented for **=
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