Repository navigation
struct.Struct inheritance with Python 3.12.0 #112358
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Nov 24, 2023 bisected to c8c0afc
cc @kumaraditya303- addedextension-modulesC modules in the Modules dirC modules in the Modules dir
on Nov 24, 2023 - added3.12only security fixesonly security fixes3.13only security fixesonly security fixes
on Nov 24, 2023 Looks like another user also hit this problem, but reported it on the PR rather than opening an issue: #94532 (comment)
Hmm, this isn't nice. I'm not seeing any good options here other than simply reverting the offending commit (and re-opening the issues that PR #94532 was supposed to close). @kumaraditya303?
Hmm, this isn't nice. [...]
To elaborate: the existence of
struct.Structsubclasses that extend the__init__method means thatStruct.__new__can make essentially no assumptions about the arguments it gets. It might get no arguments; it might get just the format, it might get the format plus additional things, or it might get a collection of arguments that doesn't include the format at all. Previously all those things worked becauseStruct.__new__was simplyobject.__new__, which accepted any combination of args and kwargs.This makes the solution in #94532 of initialising the struct in
Struct.__new__non-viable - at least, not without backwards compatibility breakage, which would have to be managed in the usual way (deprecation period, etc.).I've opened #112424 for reversion. Once merged, it'll need to be backported to 3.12, and issues #75960 and #78724 will need to be re-opened.
- added 3 commits that reference this issue
on Nov 26, 2023 It is more convenient to initialize the Struct instance in
__new__than in__init__, and it makes sense, since Struct instances are cached and therefore can be considered immutable like ints or tuples. But the possibility of creating subclasses and the existence of subclasses in the wild makes this a breaking change.Can this breakage be considered a lesser evil?
@serhiy-storchaka: Maybe. I'd failed to recognise the scope of the breakage introduced by #94532, and I think reverting in the short term is the right solution. Longer term, if we can find a properly deprecated path to moving the initialisation to
__new__, I agree that that would be worth considering.For the particular issues that #94532 was addressing, I think there are likely less disruptive fixes available.
Reacted by Serhiy Storchaka- moved this to Done in Struct, memoryview and array issues 🏗️
on Jul 13, 2026
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDone
Bug report
Bug description:
When I run this code I receive an error:
It is rather strange error, I have passed
formatparameter to base class constructor, I receive this error with any value offormatparameter.There are no any problems with this code in Python 3.11 and older.
CPython versions tested on:
3.12
Operating systems tested on:
Linux
Linked PRs