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-143715: deprecate incomplete initialization of struct.Struct() by skirpichev · Pull Request #143659 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Jan 10, 2026 •
edited by serhiy-storchaka
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
  • Struct.__new__() will require a mandatory argument (format)
  • Calls of __init__() method on initialized Struct are deprecated
  • Issue: Make struct.Struct() really immutable #143715

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

  • Issue: Make struct.Struct() really immutable #143715

Sorry, something went wrong.

Uh oh!

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

All reactions
@skirpichev
pythongh-78724: deprecate incomplete initialization of struct.Struct()
fff147f
* ``Struct.__new__()`` will require a mandatory argument (format)
* Calls of ``__init__()`` method on initialized Struct are deprecated
@bedevere-app bedevere-app Bot mentioned this pull request Jan 10, 2026
_struct.Struct: calling functions without calling __init__ results in SystemError #78724
Closed
@skirpichev
skirpichev marked this pull request as ready for review January 10, 2026 14:18
@skirpichev
skirpichev requested a review from AA-Turner as a code owner January 10, 2026 14:18
@bedevere-app bedevere-app Bot added the awaiting review label Jan 10, 2026
@skirpichev skirpichev mentioned this pull request Jan 10, 2026
gh-78724: raise RuntimeError's when calling methods on non-ready Struct()'s #143643
Merged
@skirpichev

skirpichev commented Jan 10, 2026

Copy link
Copy Markdown
Member Author

The evil plan is to remove custom Struct.__init__() method and move all initialization logic to the Struct.__new__(). Something, that was done by #94532 before.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev
skirpichev requested a review from serhiy-storchaka January 10, 2026 23:05
serhiy-storchaka
serhiy-storchaka reviewed Jan 11, 2026
View reviewed changes

@serhiy-storchaka serhiy-storchaka 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.

I think this needs a separate issue for discussion. Please open a new issue, and refer #78724 and #112358 (maybe even with citations).

We need to discuss what should be the end goal after the end of the deprecation period, and how deprecations canl help to transit the user code.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_struct.py Resolved

Uh oh!

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

Comment thread Lib/test/test_struct.py Resolved

Uh oh!

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

@skirpichev
skirpichev marked this pull request as draft January 12, 2026 00:23
@bedevere-app bedevere-app Bot removed the awaiting review label Jan 12, 2026
skirpichev added 4 commits January 12, 2026 03:28
@skirpichev
Merge branch 'master' into deprecate-struct-init-2/78724
d2a0345
@skirpichev
address review: test_Struct_reinitialization()
589fbc7
@skirpichev
catch new warning in test_operations_on_half_initialized_Struct()
5e91e87
@skirpichev
add init_called flag
babb274
@skirpichev skirpichev changed the title gh-78724: deprecate incomplete initialization of struct.Struct() gh-143715: deprecate incomplete initialization of struct.Struct() Jan 12, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jan 12, 2026
Make struct.Struct() really immutable #143715
Closed
@skirpichev
skirpichev marked this pull request as ready for review January 12, 2026 06:09
@bedevere-app bedevere-app Bot added the awaiting review label Jan 12, 2026
@skirpichev
skirpichev requested a review from serhiy-storchaka January 12, 2026 06:09
skirpichev added 2 commits January 13, 2026 03:12
@skirpichev
Merge branch 'master' into deprecate-struct-init-2/78724
0e4e84b
@skirpichev
a hack to support new idiom for subclassing
3f36635
This make format argument in the __init__() - optional.  If it's
missing, the object must be already initialized in __new__().
@skirpichev
skirpichev requested a review from meadori January 13, 2026 01:35
@skirpichev

skirpichev commented Jan 13, 2026

Copy link
Copy Markdown
Member Author

CC @meadori per experts index.

All reactions

Sorry, something went wrong.

Uh oh!

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

skirpichev added 2 commits January 17, 2026 03:09
@skirpichev
Merge branch 'master' into deprecate-struct-init-2/78724
e576475
@skirpichev
+ filter out Struct signature test
628aadd
vstinner
vstinner reviewed Jan 19, 2026
View reviewed changes
Comment thread Modules/_struct.c Resolved

Uh oh!

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

Comment thread Modules/_struct.c Resolved

Uh oh!

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

Comment thread Modules/_struct.c Resolved

Uh oh!

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

skirpichev and others added 3 commits January 19, 2026 16:57
@skirpichev @vstinner
Update Modules/_struct.c
979cc18
Co-authored-by: Victor Stinner <[email protected]>
@skirpichev
Merge branch 'master' into deprecate-struct-init-2/78724
f7492ec
@skirpichev
address review: reformat s_new()
db8f5f2
@skirpichev

skirpichev commented Feb 28, 2026

Copy link
Copy Markdown
Member Author

And what about the following case?

class MyStruct(struct.Struct):
    def __init__(self, arg):
        super().__init__('>h')

my_struct = MyStruct('<h')
my_struct.pack(12345)

There should emit a FutureWarning, because the current and the future code produce different results. MyStruct(5) should emit a DeprecationWarning, because it works in the current code, but will be error in future. ``MyStruct('>h')` should work without warnings.

@serhiy-storchaka, this seems too complex for me.

Look, we are going to that state: #94532. (I did a working patch to play with in skirpichev#17.) That means, eventually the Struct's __init__() will be no-op (and accept any arguments).

Thus, we should warn users on this pattern: explicit call of the __init__() on an object of some Struct subclass. Just in all above examples. Does make sense for you?

CC @vstinner

All reactions

Sorry, something went wrong.

Uh oh!

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

skirpichev added 2 commits February 28, 2026 11:21
@skirpichev
Merge branch 'master' into deprecate-struct-init-2/78724
26cde89
@skirpichev
issue DeprecationWarning for implicit tp_new calls in subclasses
7697820
This catch current pattern for Struct's subclassing like

class MyStruct(Struct):
    def __init__(self):
        super().__init__('>h')
@skirpichev
skirpichev marked this pull request as ready for review February 28, 2026 09:05
@bedevere-app bedevere-app Bot added the awaiting review label Feb 28, 2026
@skirpichev
skirpichev requested review from serhiy-storchaka and vstinner February 28, 2026 09:15
@skirpichev
skirpichev dismissed vstinner’s stale review March 2, 2026 06:19

severe code changes

@skirpichev skirpichev removed their assignment Mar 2, 2026
vstinner
vstinner reviewed Mar 2, 2026
View reviewed changes
Comment thread Lib/test/test_struct.py Resolved

Uh oh!

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

Comment thread Modules/_struct.c Resolved

Uh oh!

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

Comment thread Modules/_struct.c Resolved

Uh oh!

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

Comment thread Lib/test/test_struct.py Resolved

Uh oh!

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

Comment thread Misc/NEWS.d/next/Library/2026-01-10-16-23-21.gh-issue-143715.HZrfSA.rst Resolved

Uh oh!

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

skirpichev and others added 3 commits March 2, 2026 16:42
@skirpichev @vstinner
Apply suggestions from code review
8a1fa9a
Co-authored-by: Victor Stinner <[email protected]>
@skirpichev
address review: fix byte order in test_Struct_reinitialization()
d9ed032
@skirpichev
address review: PEP 7
309229c
@skirpichev
skirpichev requested a review from vstinner March 2, 2026 13:52
vstinner
vstinner reviewed Mar 2, 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.

The overall change LGTM, but I have a few more minor comments.

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_struct.py Resolved

Uh oh!

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

Comment thread Modules/_struct.c Resolved

Uh oh!

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

skirpichev and others added 2 commits March 2, 2026 17:23
@skirpichev
address review: test format attribute instead of pack()
d90dfc2
@skirpichev @vstinner
Update Modules/_struct.c
e1d3b7d
Co-authored-by: Victor Stinner <[email protected]>
vstinner
vstinner approved these changes Mar 2, 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.

@serhiy-storchaka: Do you want to review this change?

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Mar 2, 2026
@serhiy-storchaka serhiy-storchaka mentioned this pull request Mar 6, 2026
gh-143715: Deprecate incomplete initialization of struct.Struct() #145580
Merged
@serhiy-storchaka

serhiy-storchaka commented Mar 6, 2026

Copy link
Copy Markdown
Member

I tried to fix corner cases, but since all code and tests were rewritten, I created a separate PR #145580.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev

skirpichev commented Mar 7, 2026

Copy link
Copy Markdown
Member Author

Ah, I forgot that format is positional or keyword argument. Will adapt code a bit.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev
skirpichev marked this pull request as draft March 7, 2026 06:42
@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 7, 2026
@skirpichev skirpichev assigned skirpichev and unassigned skirpichev Mar 7, 2026
@skirpichev

skirpichev commented Mar 9, 2026

Copy link
Copy Markdown
Member Author

Apparently, this overflowed my bandwidth. I'm happy to see the issue is in more qualified hands now.

The #145580 looks ok for me, but I worry that such approach is much more complex just to satisfy the constraint:

  • It should be possible to write a user code compatible with future Python versions and with old Python versions which works without warnings during transitional period.

Transition could be handled with, say:

if sys.version_info < (3, 15):
    # old idiom:
    class MyStruct(struct.Struct):
        def __init__(self):
            super().__init__('>h')
else:
    # new idiom:
    class MyStruct2(struct.Struct):
        def __new__(cls):
            self = super().__new__(cls, '>h')
            return self

Thanks for reviews!

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev skirpichev closed this Mar 9, 2026
@skirpichev
skirpichev deleted the deprecate-struct-init-2/78724 branch March 9, 2026 03:26
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
@meadori meadori Awaiting requested review from meadori
@serhiy-storchaka serhiy-storchaka Awaiting requested review from serhiy-storchaka

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 @serhiy-storchaka @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.