GeezerVison is showing github.com — BUG: np.vectorize appears to pass c types in numpy2.4 and 2.5 where previously it passed Python types · Issue #31770 · numpy/numpy
BUG: np.vectorize appears to pass c types in numpy2.4 and 2.5 where previously it passed Python types #31770DescriptionDescribe the issue:
The test below passes under numpy 2.3 (and many previous versions of numpy) but fails under numpy 2.4 and 2.5. (I didn't use 2.4 till testing this.)
Previously, vectorize (in this case, at least), passed Python ints, whereas in 2.5 it is passing numpy raw values, (C types) that need .item() or whatever to convert to Python types. This broke my code.
There's nothing I can see in the 2.4 or 2.5 release notes, and this seems like a regression to me.
Here is the output under the three versions with the same Python 3.14 on MacOS 15.7.7 on an M1 Studio
(but also on ubuntu).
python3.14 test_np_24_25_vectorize.py
numpy version: 2.3.0
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Here it is on 2.4
$ python3.14 test_np_24_25_vectorize.py
numpy version: 2.4.0
FF
======================================================================
FAIL: testVectorizePassesPythonFloat (__main__.TestVectorizeTypes.testVectorizePassesPythonFloat)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/njr/python/artists/miro/test_np_24_25_vectorize.py", line 20, in testVectorizePassesPythonFloat
self.assertEqual(seen[0], float,
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
'np.vectorize passed %s, expected float '
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'(possible regression in numpy 2.4+)' % seen[0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'numpy.float64'> != <class 'float'> : np.vectorize passed <class 'numpy.float64'>, expected float (possible regression in numpy 2.4+)
======================================================================
FAIL: testVectorizePassesPythonInt (__main__.TestVectorizeTypes.testVectorizePassesPythonInt)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/njr/python/artists/miro/test_np_24_25_vectorize.py", line 11, in testVectorizePassesPythonInt
self.assertEqual(seen[0], int,
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
'np.vectorize passed %s, expected int '
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'(possible regression in numpy 2.4+)' % seen[0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'numpy.int32'> != <class 'int'> : np.vectorize passed <class 'numpy.int32'>, expected int (possible regression in numpy 2.4+)
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=2)
And here on 2.5
$ python3.14 test_np_24_25_vectorize.py
numpy version: 2.5.0
FF
======================================================================
FAIL: testVectorizePassesPythonFloat (__main__.TestVectorizeTypes.testVectorizePassesPythonFloat)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/njr/python/artists/miro/test_np_24_25_vectorize.py", line 20, in testVectorizePassesPythonFloat
self.assertEqual(seen[0], float,
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
'np.vectorize passed %s, expected float '
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'(possible regression in numpy 2.4+)' % seen[0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'numpy.float64'> != <class 'float'> : np.vectorize passed <class 'numpy.float64'>, expected float (possible regression in numpy 2.4+)
======================================================================
FAIL: testVectorizePassesPythonInt (__main__.TestVectorizeTypes.testVectorizePassesPythonInt)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/njr/python/artists/miro/test_np_24_25_vectorize.py", line 11, in testVectorizePassesPythonInt
self.assertEqual(seen[0], int,
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
'np.vectorize passed %s, expected int '
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
'(possible regression in numpy 2.4+)' % seen[0])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: <class 'numpy.int32'> != <class 'int'> : np.vectorize passed <class 'numpy.int32'>, expected int (possible regression in numpy 2.4+)
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=2)
Reproduce the code example:
import unittest
import numpy as np
class TestVectorizeTypes(unittest.TestCase):
def testVectorizePassesPythonInt(self):
seen = []
np.vectorize(lambda x: seen.append(type(x)) or x)(
np.array([1], dtype=np.int32)
)
self.assertEqual(seen[0], int,
'np.vectorize passed %s, expected int '
'(possible regression in numpy 2.4+)' % seen[0])
def testVectorizePassesPythonFloat(self):
seen = []
np.vectorize(lambda x: seen.append(type(x)) or x)(
np.array([1.0], dtype=np.float64)
)
self.assertEqual(seen[0], float,
'np.vectorize passed %s, expected float '
'(possible regression in numpy 2.4+)' % seen[0])
if __name__ == '__main__':
print(f'numpy version: {np.__version__}')
unittest.main()
Error message:
Python and NumPy Versions:
2.3.0
3.14.6 (main, Jun 10 2026, 15:24:40) [Clang 17.0.0 (clang-1700.0.13.5)]
2.4.0
3.14.6 (main, Jun 10 2026, 15:24:40) [Clang 17.0.0 (clang-1700.0.13.5)]
2.5.0
3.14.6 (main, Jun 10 2026, 15:24:40) [Clang 17.0.0 (clang-1700.0.13.5)]
Runtime Environment:
No response
How does this issue affect you or how did you find it:
Running tests for my software with a newer versions of numpy. Activitysahil-mangla commented on Jun 26, 2026 ngoldbaum commented on Jul 3, 2026 So the behavior you're seeing in NumPy 2.3 is fallout from #28624.
It was reverted in #29196, which was part of NumPy 2.3.1. I note that you are testing NumPy 2.3.0 and there were several 2.3.x bugfix releases you are missing in your test matrix. The NumPy 2.4 and 2.5 behavior is also identical to NumPy 1.26 through NumPy 2.2.
I wrote an incorrect comment before this that I deleted, sorry for the noise! njr0 commented on Jul 8, 2026 Sure. It is possible my claim that this behaviour was in many previous versions is wrong. I have been using numpy for at least 18 years, but (thinking about it more carefully) I haven't used vectorize much, so maybe I only started when I was on 2.3. Apologies if I was mistaken. Thanks for looking at it @ngoldbaum.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|