gh-143715: Deprecate incomplete initialization of struct.Struct()#145580
Open
serhiy-storchaka wants to merge 29 commits intopython:mainfrom
Open
gh-143715: Deprecate incomplete initialization of struct.Struct()#145580serhiy-storchaka wants to merge 29 commits intopython:mainfrom
serhiy-storchaka wants to merge 29 commits intopython:mainfrom
Conversation
* ``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')
skirpichev
reviewed
Mar 7, 2026
| :meth:`~object.__init__` method on initialized :class:`~struct.Struct` | ||
| objects is deprecated and will be removed in Python 3.20. | ||
|
|
||
| (Contributed by Sergey B Kirpichev in :gh:`143715`.) |
Member
There was a problem hiding this comment.
Suggested change
| (Contributed by Sergey B Kirpichev in :gh:`143715`.) | |
| (Contributed by Serhiy Storchaka in :gh:`143715`.) |
| :meth:`~object.__init__` method on initialized :class:`~struct.Struct` | ||
| objects is deprecated and will be removed in Python 3.20. | ||
|
|
||
| (Contributed by Sergey B Kirpichev in :gh:`143715`.) |
Member
There was a problem hiding this comment.
Suggested change
| (Contributed by Sergey B Kirpichev in :gh:`143715`.) | |
| (Contributed by Serhiy Storchaka in :gh:`143715`.) |
| def check_sizeof(self, format_str, number_of_codes): | ||
| # The size of 'PyStructObject' | ||
| totalsize = support.calcobjsize('2n3P') | ||
| totalsize = support.calcobjsize('2n3P1?') |
Member
There was a problem hiding this comment.
Suggested change
| totalsize = support.calcobjsize('2n3P1?') | |
| totalsize = support.calcobjsize('2n3P?') |
| super().__init__('>h') | ||
|
|
||
| my_struct = MyStruct('>h') | ||
| self.assertEqual(my_struct.pack(12345), b'\x30\x39') |
Member
There was a problem hiding this comment.
I think this will be more clear, per Victor's suggestion:
Suggested change
| self.assertEqual(my_struct.pack(12345), b'\x30\x39') | |
| self.assertEqual(my_struct.format, '>h') |
(and in all cases below too)
| def __init__(self, *args, **kwargs): | ||
| super().__init__('>h') | ||
|
|
||
| my_struct = MyStruct('>h') |
Member
There was a problem hiding this comment.
Why this not raises a warning? I think we should warn in all cases, where Struct.__init__() was explicitly called. // #143659 (comment)
|
|
||
| my_struct = MyStruct('>h') | ||
| self.assertEqual(my_struct.pack(12345), b'\x30\x39') | ||
| my_struct = MyStruct(format='>h') |
Member
There was a problem hiding this comment.
Ah, I entirely forgot that format is a positional or keyword argument in the master. I'll fix my patch.
But again, this should emit a warning.
Comment on lines
+880
to
+888
| # New way, no warnings: | ||
| class MyStruct(struct.Struct): | ||
| def __new__(cls, newargs, initargs): | ||
| return super().__new__(cls, *newargs) | ||
| def __init__(self, newargs, initargs): | ||
| if initargs is not None: | ||
| super().__init__(*initargs) | ||
|
|
||
| my_struct = MyStruct(('>h',), ('>h',)) |
Member
There was a problem hiding this comment.
Again, why no warnings here?
BTW, I doubt this usage pattern come from reality ;-)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Struct.__new__()will require a mandatory argument (format)__init__()method on initialized Struct are deprecatedThis 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/