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 — struct.Struct inheritance with Python 3.12.0 · Issue #112358 · python/cpython

struct.Struct inheritance with Python 3.12.0 #112358

New issue
Copy link
New issue
Copy link
Closed
Closed
struct.Struct inheritance with Python 3.12.0#112358
Copy link
Assignees
mdickinsonkumaraditya303
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@livrrr
livrrr
opened on Nov 24, 2023
Issue body actions

Bug report

Bug description:

import struct

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

obj = MyStruct()

When I run this code I receive an error:

Traceback (most recent call last):
  File "/home/user/bug.py", line 7, in <module>
    obj = MyStruct()
          ^^^^^^^^^^
TypeError: Struct() missing required argument 'format' (pos 1)

It is rather strange error, I have passed format parameter to base class constructor, I receive this error with any value of format parameter.
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

  • gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. #112424
  • [3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.Struct (GH-112424) #112426

Activity

  1. livrrr
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Nov 24, 2023
  2. Eclips4 commented on Nov 24, 2023

    @Eclips4
    Eclips4
    on Nov 24, 2023
    Member
    More actions

    bisected to c8c0afc
    cc @kumaraditya303

  3. Eclips4
    added
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    on Nov 24, 2023
  4. AlexWaygood
    added
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    on Nov 24, 2023
  5. AlexWaygood commented on Nov 24, 2023

    @AlexWaygood
    AlexWaygood
    on Nov 24, 2023
    Member
    More actions

    Looks like another user also hit this problem, but reported it on the PR rather than opening an issue: #94532 (comment)

  6. AlexWaygood
    assigned
    mdickinson
    and
    kumaraditya303
    on Nov 24, 2023
  7. mdickinson commented on Nov 26, 2023

    @mdickinson
    mdickinson
    on Nov 26, 2023
    Member
    More actions

    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?

  8. mdickinson commented on Nov 26, 2023

    @mdickinson
    mdickinson
    on Nov 26, 2023
    Member
    More actions

    Hmm, this isn't nice. [...]

    To elaborate: the existence of struct.Struct subclasses that extend the __init__ method means that Struct.__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 because Struct.__new__ was simply object.__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.

  9. mdickinson
    added 3 commits that reference this issue on Nov 26, 2023

    gh-112358: Fix Python 3.12 regression with subclassing struct.Struct. (…

    9fe6034

    [3.12] pythongh-112358: Fix Python 3.12 regression with subclassing s…

    72d90d0

    [3.12] gh-112358: Fix Python 3.12 regression with subclassing struct.…

    42df736
  10. mdickinson commented on Nov 27, 2023

    @mdickinson
    mdickinson
    on Nov 27, 2023
    Member
    More actions

    Fixed in #112424 (main), #112426 (3.12).

  11. mdickinson
    closed this as completedon Nov 27, 2023
  12. serhiy-storchaka commented on Nov 27, 2023

    @serhiy-storchaka
    serhiy-storchaka
    on Nov 27, 2023
    Member
    More actions

    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?

  13. mdickinson commented on Nov 27, 2023

    @mdickinson
    mdickinson
    on Nov 27, 2023
    Member
    More actions

    @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
  14. aisk
    added a commit that references this issue on Feb 11, 2024

    pythongh-112358: Fix Python 3.12 regression with subclassing struct.S…

    6d24e80
  15. Glyphack
    added a commit that references this issue on Sep 2, 2024

    pythongh-112358: Fix Python 3.12 regression with subclassing struct.S…

    6aabe84
  16. serhiy-storchaka
    added this to Struct, memoryview and array issues 🏗️on Jul 13, 2026
  17. github-project-automation
    moved this to Done in Struct, memoryview and array issues 🏗️on Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @kumaraditya303
    kumaraditya303
  • @mdickinson
    mdickinson

Labels

3.12only security fixesonly security fixes3.13only security fixesonly security fixesextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

  • Struct, memoryview and array issues 🏗️
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

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.