Skip to content

BUG: isinstance(array, SupportsFloat) is True but float(array) raises #29264

Open
@d-perl

Description

@d-perl

Describe the issue:

Even when an array cannot be converted to a float, it still passes an isinstance(_, SupportsFloat) check.

>>> a = np.array([1.0,2.0,3.0])
>>> float(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: only length-1 arrays can be converted to Python scalars
>>> a = np.array([1.0])
>>> float(a)
<stdin>:1: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
1.0

Reproduce the code example:

import numpy as np
from typing import SupportsFloat

a = np.array([1.0, 2.0, 3.0])
isinstance(a, SupportsFloat) # True
float(a) # raises

Error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: only length-1 arrays can be converted to Python scalars

Python and NumPy Versions:

2.2.6
3.12.10 (main, May 21 2025, 10:38:42) [GCC 15.1.1 20250425]

Runtime Environment:

No response

Context for the issue:

Just affects tidiness, easy enough to work around, not high priority. I just thought it should be documented. I suppose this will be resolved anyway whenever this deprecated feature is removed and array doesn't have __float__ any longer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    00 - Bug57 - Close?Issues which may be closable unless discussion continued

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions