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.

classification
Title: The __complex__ method is missing from the complex, float, and int built-in types
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: corona10, maggyero, mark.dickinson, rhettinger
Priority: normal Keywords:

Created on 2022-03-21 17:09 by maggyero, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg415689 - (view) Author: Géry (maggyero) * Date: 2022-03-21 17:09
## Expected outcome

```
>>> hasattr(complex(), '__complex__')
True
>>> hasattr(float(), '__complex__')
True
>>> hasattr(int(), '__complex__')
True
```

## Actual outcome

```
>>> hasattr(complex(), '__complex__')
False
>>> hasattr(float(), '__complex__')
False
>>> hasattr(int(), '__complex__')
False
```

## Rationale

The `numbers.Complex` abstract base class has a `__complex__` abstract method and the `numbers.Real` and `numbers.Integral` abstract base classes inherit from `numbers.Complex` (https://github.com/python/cpython/blob/v3.10.3/Lib/numbers.py#L45-L47):

```
    @abstractmethod
    def __complex__(self):
        """Return a builtin complex instance. Called for complex(self)."""
```

The `complex` built-in type is a virtual subclass (nominal subtype) of `numbers.Complex` (https://github.com/python/cpython/blob/v3.10.3/Lib/numbers.py#L144):

```
Complex.register(complex)
```

The `float` built-in type is a virtual subclass (nominal subtype) of `numbers.Real` (https://github.com/python/cpython/blob/v3.10.3/Lib/numbers.py#L264):

```
Real.register(float)
```

The `int` built-in type is a virtual subclass (nominal subtype) of `numbers.Integral` (https://github.com/python/cpython/blob/v3.10.3/Lib/numbers.py#L393):

```
Integral.register(int)
```

`__complex__` is the only method of the abstract base classes of `numbers` missing from `complex`, `float`, and `int`:

```
>>> import numbers
>>> for attr in dir(numbers.Complex):
...     if not hasattr(complex(), attr): print(attr)
... 
__abstractmethods__
__complex__
__module__
__slots__
_abc_impl
>>> for attr in dir(numbers.Real):
...     if not hasattr(float(), attr): print(attr)
... 
__abstractmethods__
__complex__
__module__
__slots__
_abc_impl
>>> for attr in dir(numbers.Integral):
...     if not hasattr(int(), attr): print(attr)
... 
__abstractmethods__
__complex__
__module__
__slots__
_abc_impl
```
msg416141 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-03-28 02:53
I think this may fine as-is.  In general, virtiual classes only promise that an operation will work rather than promsing the presence of a particular method.

An object can be Iterable without defining __iter__ because iter() can use __getitem__ and __len__ to build a sequence iterator.  Likewise, an object can be a Container without defining __contains__ because the in-operator will work on any iterable.  An object can be Reversible without defining __reversed__ because reversed() will fall back to an implementation based on __getitem__ and __len__.

The docstring in numbers.Complex promises, "Complex defines the operations that work on the builtin complex type.  In short, those are: a conversion to complex, .real, .imag, +, -, *, /, **, abs(), .conjugate, ==, and !=."

In other words, the promise is that these work (which they do):

   >>> complex(0.0)
   0j
   >>> int(0.0)
   0
msg416273 - (view) Author: Géry (maggyero) * Date: 2022-03-29 16:35
> I think this may fine as-is.  In general, virtiual classes only promise that an operation will work rather than promsing the presence of a particular method.

Okay, I just wanted to make sure that the absence of the `__complex__` method was intended and not an oversight, since the built-in numeric types define *all* the other methods of the numbers ABCs.
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91239
2022-03-31 14:37:42rhettingersetstatus: open -> closed
resolution: not a bug
stage: resolved
2022-03-29 16:35:35maggyerosetmessages: + msg416273
2022-03-28 02:53:48rhettingersetnosy: + rhettinger
messages: + msg416141
2022-03-24 14:07:04corona10setnosy: + mark.dickinson, corona10
2022-03-21 17:09:40maggyerocreate