[Python-Dev] return type of __complex__

Antonio Cuni anto.cuni at gmail.com
Fri Oct 19 15:08:40 CEST 2012


while fixing pypy to pass CPython 3.2 tests, I found what I think it's a
inconsistency in how CPython (both 2.7 and 3.2) handles __complex__:

>>> class Obj:
...     def __complex__(self):
...         return 2.0
>>> obj = Obj()
>>> complex(obj)
>>> import cmath
>>> cmath.acos(obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __complex__ should return a complex object

i.e., the complex constructor does not check that __complex__ returns an
actual complex, while the cmath functions do.
To me it looks like a bug in complex_new which should do the check as well;
however, there is a test in test_complex.test_constructor which checks that
returning a float actually works.

Is that the real intended behavior?


More information about the Python-Dev mailing list