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

Inplace dunders can return NotImplemented, and that's not documented #104219

Closed
Gouvernathor opened this issue May 6, 2023 · 2 comments
Closed
Labels
docs Documentation in the Doc dir

Comments

@Gouvernathor
Copy link
Contributor

Gouvernathor commented May 6, 2023

See this discussion.
Normal NotImplemented situation : A() + B() is resolved in A.__add__(A(), B()) which if returning NotImplemented, falls back to B.__radd__(B(), A()).
Now this behavior means there can be a double fallback : in a += b, if A.__iadd__ exists but returns NotImplemented, it will first fall back to A.__add__ and then to B.__radd__.
This is a great feature, but it's not currently documented.

Linked PRs

@Gouvernathor
Copy link
Contributor Author

For ye of little faith :

class A:
    def __init__(self, val):
        self.val = val
    def __str__(self):
        return f"A<{self.val}>"
    def __add__(self, other):
        print("add", self, other)
        return NotImplemented
    def __iadd__(self, other):
        print("iadd", self, other)
        return NotImplemented

class B:
    def __init__(self, val):
        self.val = val
    def __str__(self):
        return f"B<{self.val}>"
    def __radd__(self, other):
        print("radd", self, other)
        return NotImplemented

a, b = A("a"), B("b") then a += b calls the three methods in order (then fails).

erlend-aasland pushed a commit that referenced this issue Mar 1, 2024
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 1, 2024
…thonGH-104220)

(cherry picked from commit 2713c2a)

Co-authored-by: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 1, 2024
…thonGH-104220)

(cherry picked from commit 2713c2a)

Co-authored-by: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
erlend-aasland pushed a commit that referenced this issue Mar 1, 2024
…H-104220) (#116210)

(cherry picked from commit 2713c2a)

Co-authored-by: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
erlend-aasland pushed a commit that referenced this issue Mar 1, 2024
…H-104220) (#116211)

(cherry picked from commit 2713c2a)

Co-authored-by: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com>
Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
…thon#104220)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…thon#104220)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…thon#104220)

Co-authored-by: C.A.M. Gerlach <CAM.Gerlach@Gerlach.CAM>
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
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
Projects
None yet
Development

No branches or pull requests

2 participants