[pypy-issue] [issue944] Magic method exceptions

Armin Rigo tracker at bugs.pypy.org
Tue Nov 29 15:33:00 CET 2011

Armin Rigo <armin.rigo at gmail.com> added the comment:

Ah, to answer your last comment more directly: "both should produce identical
exceptions": no, that's not true, because the (simplified) definition of "a +=
b" is to try first a.__iadd__(b); if that returns NotImplemented, try
"a.__add__(b)"; if that still returns NotImplemented, try "b.__radd__(a)"; and
if that still returns NotImplemented, then it raises a TypeError.  That is why
the internal magic methods are supposed to return NotImplemented rather than
raise TypeError themselves.  For example, (2).__add__(2.5) returns
NotImplemented, but (2.5).__radd__(2) returns 4.5.

It can be regarded as wrong (and certainly a CPython implementation detail) that
occasionally the __add__ method raise TypeError itself directly.

PyPy bug tracker <tracker at bugs.pypy.org>

More information about the pypy-issue mailing list