◐ Shell
reader mode source ↗
Skip to content

gh-143715: Deprecate incomplete initialization of struct.Struct()#145580

Merged
serhiy-storchaka merged 38 commits into
python:mainfrom
serhiy-storchaka:deprecate-struct-init
Mar 12, 2026
Merged

gh-143715: Deprecate incomplete initialization of struct.Struct()#145580
serhiy-storchaka merged 38 commits into
python:mainfrom
serhiy-storchaka:deprecate-struct-init

Conversation

@serhiy-storchaka

@serhiy-storchaka serhiy-storchaka commented Mar 6, 2026

Copy link
Copy Markdown
Member
  • Struct.__new__() will require a mandatory argument (format)
  • Calls of __init__() method on initialized Struct are deprecated

This is an evolution of #143659, but since virtually all code and test were rewritten I created a new PR.


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

skirpichev and others added 29 commits January 10, 2026 16:37
* ``Struct.__new__()`` will require a mandatory argument (format)
* Calls of ``__init__()`` method on initialized Struct are deprecated
This make format argument in the __init__() - optional.  If it's
missing, the object must be already initialized in __new__().
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This catch current pattern for Struct's subclassing like

class MyStruct(Struct):
    def __init__(self):
        super().__init__('>h')
6 hidden items Load more…
@skirpichev skirpichev self-requested a review March 8, 2026 00:10

@skirpichev skirpichev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

This looks ok for me and have much better test coverage than my pr #143659.

Though, I worry that it's too complex: #143659 (comment)

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hide comment

The code is complex because the problem is complex.

We should minimize user inconvenience. If the code works in past and future versions, it should not emit warnings. There should be a way to write a warning-free code compatible with old versions (because you usually support more than one Python versions), and it should be valid in future versions.

If ignore this, we could just make a breaking change without deprecation period. And we do this if there is no other way (for example, the format attribute is now a string, not a bytes object like in older versions -- it was impossible to make this change not abrupt).

@skirpichev

Copy link
Copy Markdown
Member

There should be a way to write a warning-free code compatible with old versions (because you usually support more than one Python versions), and it should be valid in future versions.

As I said in #143659 (comment), this requirement looks too strong for me. To support several versions, people could also use if sys.version_info blocks.

CC @encukou, would you mind to review this pr?

@encukou encukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hide comment

I like this approach! Thank you for caring about backwards compatibility!

@serhiy-storchaka

Copy link
Copy Markdown
Member Author

About deprecation warning in the repeated __init__(). I suggested this as a simple intermediate step, but if in future it will be identical to object.__init__(), which is no-op, there should not be warning for calling it with the same format (which is also no-op currently), but calling it with different format should emit a FutureWarning instead of a DeprecationWarning.

Accidentally, this also makes the code a little bit simpler.

Hide details View details @serhiy-storchaka serhiy-storchaka merged commit 7245630 into python:main Mar 12, 2026
91 of 92 checks passed
@serhiy-storchaka serhiy-storchaka deleted the deprecate-struct-init branch March 12, 2026 07:44
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
…() (pythonGH-145580)

* Struct.__new__() will require a mandatory argument (format)
* Calls of __init__() method with a different format argument on initialized
  Struct are deprecated

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants