Skip to content

Deprecated F and D format in the struct module in Python 3.16 (use Zf and Zd instead) #352

@vstinner

Description

@vstinner

Hi,

Python 3.14 introduced F and D formats in the struct module for complex numbers (32-bit and 64-bit). F and D were chosen for compatibility with numpy, but sadly there was a misunderstanding:

  • F and D are NumPy's "dtype" typecodes, which are user-facing API, and single-character. Defined by NumPy.
  • Buffer protocol format strings (Zf and Zd), which are under-the-hood serialization details. Defined by PEP 3118.

Numpy uses Zf and Zd buffer formats, defined by PEP 3118, for 15 years (since numpy 1.6). PEP 3118 defines the Zd format for 19 years (commit).

Python 3.15 beta1 was modified to accept Zf and Zd formats in array, ctypes, struct and memoryview for better compatibility with numpy. This change was endorsed by the Steering Council.

So Python 3.16 supports "Python" F / D formats and Numpy/PEP 3118 Zd / Zf buffer formats in the struct module.

I propose deprecating the F and D formats in the struct module (emit DeprecationWarning warning) in Python 3.16, and soft deprecate them in Python 3.15 documentation (but not in Python 3.14 which doesn't support Zf / Zd). The deprecation will follow PEP 387 policy: keep the deprecated feature for 5 years before removing it (I don't think that there is an urgency to remove F and D formats).

Note: Only the struct module accepts F / D formats: array, ctypes and memoryview use Zf / Zd formats in Python 3.15 instead.

Note 2: Before the 3.14 final release, ctypes and struct used C (double complex) and E (float complex) formats. These formats were replaced with D and F for compatibility with numpy in Python 3.14 final release.

Victor

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions