GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-146151: support for complex arrays in the array module by skirpichev · Pull Request #146237 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Mar 21, 2026 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • Issue: Support complex (float/double) types in array.array and memoryview #146151

📚 Documentation preview 📚: https://cpython-previews--146237.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@skirpichev
pythongh-146151: support for complex arrays in the array module
1afb3b9
@skirpichev
skirpichev requested a review from AA-Turner as a code owner March 21, 2026 03:47
@bedevere-app bedevere-app Bot mentioned this pull request Mar 21, 2026
Support complex (float/double) types in array.array and memoryview #146151
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Mar 21, 2026
@skirpichev
+make clinic
5d52ad6
@skirpichev

This comment was marked as resolved.

Sign in to view
@skirpichev
+ cleanup news entry
f5dfb79
@skirpichev skirpichev mentioned this pull request Mar 21, 2026
gh-146238: support half-floats in the array module #146242
Merged
@skirpichev
Merge branch 'main' into support-complex-arrays/146151
2a9a1b4
vstinner
vstinner reviewed Mar 25, 2026
View reviewed changes
Comment thread Lib/test/test_array.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/arraymodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/arraymodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/arraymodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_array.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/whatsnew/3.15.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/arraymodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev added 2 commits March 26, 2026 02:24
@skirpichev
Merge branch 'master' into support-complex-arrays/146151
2635360
@skirpichev
address review: test + warning
7c6ac57
skirpichev
skirpichev commented Mar 25, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-03-21-06-21-38.gh-issue-146151.yNpgml.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev @vstinner
Apply suggestions from code review
c3489f2
Co-authored-by: Victor Stinner <[email protected]>
Co-authored-by: Sergey B Kirpichev <[email protected]>
@skirpichev
skirpichev requested a review from vstinner March 25, 2026 23:35
vstinner
vstinner reviewed Mar 25, 2026
View reviewed changes
Comment thread Modules/arraymodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

skirpichev added 2 commits March 26, 2026 03:07
@skirpichev
address review: +tests for array_reconstructor
f8a7c8c
@skirpichev
+clinic
defb1bb
vstinner
vstinner reviewed Mar 26, 2026
View reviewed changes
Comment thread Lib/test/test_array.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev @vstinner
Apply suggestion from @vstinner
ffc5745
Co-authored-by: Victor Stinner <[email protected]>
vstinner
vstinner approved these changes Mar 26, 2026
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

skirpichev reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Mar 26, 2026
@skirpichev

skirpichev commented Mar 26, 2026

Copy link
Copy Markdown
Member Author

Thanks for reviews!

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner
vstinner merged commit 0e3b3b8 into python:main Mar 26, 2026
55 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 26, 2026
@skirpichev
skirpichev deleted the support-complex-arrays/146151 branch March 26, 2026 18:32
@pablogsal pablogsal mentioned this pull request Apr 4, 2026
gh-130273: Fix traceback color output with unicode characters #142529
Merged
@seberg

seberg commented Apr 12, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This came up in NumPy: numpy/numpy#31204 and I am wondering whether you discussed if the buffer protocol should use D and F vs. Zd and Zf here?
On the one hand, sure, this follows the struct protocol, which is documented. But on the other hand the struct module didn't include it at the time, and the PEP says it should be Zd. And because of that this will be what NumPy (and everyone else) uses.

Now, understanding D as well is unproblematic, but I dunno if it is great for us to transition to D.

EDIT: sorry forgot the buffer protocol link: https://peps.python.org/pep-3118/#additions-to-the-struct-string-syntax (see the Z entry in the table)

EDIT2: And now I find #146241 which is the more interesting PR... but since everyone is the same person involved...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Apr 13, 2026

Copy link
Copy Markdown
Member Author

This came up in NumPy: numpy/numpy#31204 and I am wondering whether you discussed if the buffer protocol should use D and F vs. Zd and Zf here?

Yes, I think so. That was briefly discussed while adding support for complex types in the ctypes module, e.g.: #120894 (comment). Note that later type codes for ctypes/struct were changed to NumPy-style: #121249 (comment)

Currently, struct/array/ctypes modules use single-letter codes for fundamental types. So, adding support for complex types in PEP 3118-style requires rather complex changes in internal structures, interpreting of the format string and so on. Not sure if this feasible just for complex types alone.

BTW, note we are trying to be consistent across the stdlib modules (struct/array/ctypes), while NumPy uses different "type codes", e.g. 'D' with ndarray, instead of 'Zd'.

But on the other hand the struct module didn't include it at the time, and the PEP says it should be Zd.

I'm not sure that PEP actually says about this. It's not a part of the Specification. Note that nothing from this section was actually implemented (except '?' format type) during 20+ years.

I quickly read linked discussion thread and then did some search on PEP 3118 in mail lists. Sorry, I wasn't able to find where that part of the proposal was discussed, where a draft implementation was proposed. And it looks that some parts of that proposal are too vague (see this and this), and that includes support for complex types! IMO, it's not a part of the actual specification and the warning at top of the PEP clearly says that:

This PEP is a historical document. The up-to-date, canonical documentation can now be found at Buffer Protocol, PyBufferProcs, PyMemoryView_FromObject.

Not all features proposed here were implemented. Specifically:

PyObject_CopyToObject was not added.
Additions to the struct string-syntax were not added, except for ? (_Bool).
PyObject_GetMemoryView is named PyMemoryView_FromObject.

This PEP targets Python 3.0, which was released more than a decade ago. Any proposals to add missing functionality should be discussed as new features, not treated as finishing the implementation of this PEP.

See PEP 1 for how to propose changes.

This looks as a good advice.

Maybe you can open a discussion thread on https://discuss.python.org/c/core-dev/23 ? PR thread is not a best place for this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev skirpichev mentioned this pull request Apr 13, 2026
ENH: Support importing complex arrays from standard library numpy/numpy#31204
Closed
@seberg

seberg commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

functionality should be discussed as new features, not treated as finishing the implementation of this PEP.

Sure, that sounds like sound advice, with the caveat that on this one it isn't true: It was implemented in a vast ecosystem, just not Python itself.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Apr 13, 2026

Copy link
Copy Markdown
Member Author

It was implemented in a vast ecosystem, just not Python itself.

The NumPy uses also 'D' type code. Which convention we should adopt for the array module?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@seberg

seberg commented Apr 13, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

The struct module already uses D rather than Zd now. And sure, NumPy uses mainly D as well from a user perspective. So yeah, D is a clear reasonable choice.

But, just because I agree that D seems like the better choice…. And I would agree that the buffer protocol might have been better off just using D as well…. (Also because for future dtypes, I don't believe in single character type codes anyway.)

None of that changes the situation that NumPy cannot just change (export, import can support both easily).
And as with NumPy the whole ecosystem won't be able to export D for years.

Now, Python can be the odd one out and one could document that one should/must implement both D and Zd for import as most users use Zd for export for the time being, but Python uses D and hopes that in a some years we can slowly transition to D (i.e. in a few years, Python and NumPy could chose to give a deprecation warning when importing Zd and then a few years later we could only support D -- but even then it might not be a great experience).

But as much as I wouldn't mind that outcome... The inconvenience of just using Zd in Python honestly seems smaller to me right now?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@skirpichev

skirpichev commented Apr 13, 2026

Copy link
Copy Markdown
Member Author

Now d.p.o thread opened, lets continue here: https://discuss.python.org/t/106949

The inconvenience of just using Zd in Python honestly seems smaller to me right now?

We can't just switch to two-letters code for compatibility reasons, as 'D' type was introduced in v3.14. Though, we could add an alias. What next? Deprecate former type codes?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

clin1234 pushed a commit to clin1234/cpython that referenced this pull request Apr 16, 2026
@skirpichev @vstinner @clin1234
pythongh-146151: Add support for complex arrays in the array module (p…
1ce9d54
…ython#146237)

Co-authored-by: Victor Stinner <[email protected]>
@skirpichev skirpichev mentioned this pull request Apr 17, 2026
Decide buffer protocol format string for complex numbers python/steering-council#345
Closed
skirpichev added a commit to skirpichev/cpython that referenced this pull request Apr 17, 2026
@skirpichev
Revert "pythongh-146151: Add support for complex arrays in the array …
5c606ee
…module (python#146237)"

This reverts commit 0e3b3b8.
@skirpichev

skirpichev commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

I prepared a reversion in #148674.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
@skirpichev @vstinner @ljfp
pythongh-146151: Add support for complex arrays in the array module (p…
0e7bf22
…ython#146237)

Co-authored-by: Victor Stinner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@AA-Turner AA-Turner Awaiting requested review from AA-Turner AA-Turner is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@skirpichev @seberg @vstinner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.