Repository navigation
Description
Describe the issue:
Python 3.14 will come with new complex C types in ctypes:
https://docs.python.org/3.14/library/ctypes.html#ctypes.c_double_complex
But it looks like their char codes differ from NumPy's F/D/G ones:
>>> import ctypes as ct
>>> ct.c_float_complex._type_
'E'
>>> ct.c_double_complex._type_
'C'
>>> ct.c_longdouble_complex._type_
'F'Unfortunately, the last F clashes with np.complex64 char code, which causes dtype(ct.c_longdouble_complex) to return a complex64 dtype.
Passing the other two (E and C) ctypes to dtype will raise a TypeError, which at least isn't incorrect (although it might be nice to add support for those as well).
Reproduce the code example:
>>> import ctypes as ct; import numpy as np
>>> np.dtype(ct.c_longdouble_complex)
dtype('complex64')Python and NumPy Versions:
>>> import sys, numpy; print(numpy.__version__); print(sys.version)
2.2.3
3.14.0a5 (main, Feb 20 2025, 02:53:28) [GCC 13.1.0]Runtime Environment:
>>> import numpy; numpy.show_runtime()
[{'numpy_version': '2.2.3',
'python': '3.14.0a5 (main, Feb 20 2025, 02:53:28) [GCC 13.1.0]',
'uname': uname_result(system='Linux', node='pop-os', release='6.9.3-76060903-generic', version='#202405300957~1738770968~22.04~d5f7c84 SMP PREEMPT_DYNAMIC Wed F', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Prescott',
'filepath': '/usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so',
'internal_api': 'openblas',
'num_threads': 32,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.20'}]Context for the issue:
I consider this a low-priority issue:
Python 3.14 isn't released yet, and is currently in alpha.
Also, I suppose that constructing a dtype this way is probably the 2nd most obscure (1st place goes to np.dtype('\x10') IMO).
Activity
hpkfft commentedon Apr 10, 2026
Using Python 3.15 alpha, I see the following:
jorenham commentedon Apr 10, 2026
Oh, huh, I see the same thing with Python 3.14. It was probably changed between 3.14 alpha and 3.14 final then.
hpkfft commentedon May 8, 2026
Python 3.15.0b1 has made changes to be better compatible with NumPy. See #31204 and the Python discussion for details.
In summary:
Python 3.14:
'F'|'D''F'|'D'|'G'Python 3.15:
'F'|'D'and'Zf'|'Zd''Zf'|'Zd''Zf'|'Zd''Zf'|'Zd'|'Zg'In particular, the following works with Python 3.15.0b1 and NumPy 2.4.4 :
However,
so
I think it would be nice for NumPy to support the PEP 3118 complex format strings in the same way it supports
np.dtype('D'). If you agree, you may with to reopen this issue.Currently, a Python standard library array can be made with
array.array('Zd', [1+2j, 3+4j])but not with'D'.Currently, a NumPy array can be made with
np.array([1+2j, 3+4j], dtype='D')but not withZd:Abhi210 commentedon May 9, 2026
I looked into this issue on Python 3.15.0b1 and found a potential fix.
The root cause seems to be that Python 3.15 switched
ctypescomplex scalar_type_codes to PEP 3118 format strings:However, NumPy currently does not recognize these format strings in dtype parsing, so calls like:
fail with:
I tested a fix by adding support for the PEP 3118 complex format strings (
Zf,Zd,Zg) in two places.First, in
numpy/_core/_internal.py, I extended_pep3118_standard_map:Then, in
numpy/_core/src/multiarray/descriptor.c, I added handling forZ*complex format strings inside_convert_from_str(PyObject *obj, int align):With these changes, the following now works correctly on Python 3.15.0b1:
Environment:
This approach also enables direct support for PEP 3118-compliant complex format strings in
np.dtype(...), improving interoperability withctypes,memoryview, andarray.array.PS: ChatGPT was used to format this comment structure into a readable form. Gemini was used as an assistant to understand certain parts of the code and to rewrite the logic in better way.
MAINT: mark numpy#28360 completed in issue-state