[issue24234] Should we define complex.__complex__ and bytes.__bytes__?

Mark Dickinson report at bugs.python.org
Sun Aug 22 07:16:34 EDT 2021


Mark Dickinson <dickinsm at gmail.com> added the comment:

> [...] we'll need `int.__complex__` and `float.__complex__` implementations as well as `complex.__complex__`.

The real problem here is that the "typing.SupportsComplex" protocol isn't a good match for code that needs to know that a given value `x` can be treated as though it were a complex number.

The test that best matches "usable as a complex number" seems to be that type(x) implements at least one of `__index__`, `__float__` or `__complex__`, or that `x` is a subclass of complex.

It looks to me as though the right thing to do here is to just implement complex.__complex__, but not int.__complex__ or float.__complex__.  Then at least we can remove the subclass test from the above and express the test purely in terms of special methods: __index__, __float__ and __complex__. And then perhaps it's for the typing module to find a more convenient way to express the union of typing.SupportsIndex, typing.SupportsFloat and typing.SupportsComplex.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue24234>
_______________________________________


More information about the Python-bugs-list mailing list