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.

Title: __rrshift__ for same class obj will raise TypeError
Type: behavior Stage: resolved
Components: Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, zen-xu
Priority: normal Keywords:

Created on 2022-03-05 14:33 by zen-xu, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg414578 - (view) Author: ZhengYu, Xu (zen-xu) Date: 2022-03-05 14:33
class C:
    def __rrshift__(self, v):
        return 1

C() >> C()   # raise TypeError: unsupported operand types(s) for >>: 'C' and 'C'
msg414579 - (view) Author: ZhengYu, Xu (zen-xu) Date: 2022-03-05 14:35
But with different classed will be right

class C:
    def __rrshift__(self, v):
        return 1

class D:
    def __rrshift__(self, v):
        return 2

C() >> D() # 2
msg414580 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2022-03-05 14:59
This is the intended behaviour. See the docs here:

> These functions are only called if the left operand does not support the corresponding operation and the operands are of different types.

There's a further explanation in a footnote:

> For operands of the same type, it is assumed that if the non-reflected method – such as __add__() – fails then the overall operation is not supported, which is why the reflected method is not called.
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91085
2022-03-05 14:59:39mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg414580

resolution: not a bug
stage: resolved
2022-03-05 14:35:27zen-xusetmessages: + msg414579
2022-03-05 14:33:30zen-xucreate