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 — Using functools.partial to make functions not considered as methods when given as enum values no longer works · Issue #125316 · python/cpython

Using functools.partial to make functions not considered as methods when given as enum values no longer works #125316

New issue
Copy link
New issue
Copy link
Closed
Closed
Using functools.partial to make functions not considered as methods when given as enum values no longer works#125316
Copy link
Labels
3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@bluthej
bluthej
opened on Oct 11, 2024
Issue body actions

Bug report

Bug description:

On python 3.10-3.12, the following snippet prints True, while on Python 3.13.0 it prints False:

from enum import Enum
from functools import partial


def a():
    pass


def b():
    pass


class MyCallables(Enum):
    A = a
    B = partial(b)


print(MyCallables.B in MyCallables)

This is because MyCallables.B is considered to be functools.partial(<function b at 0x756d7f0065c0>) on 3.13.0, when it used to be <MyCallables.B: functools.partial(<function b at 0x71f7cbc407c0>)> before.

This is something I've been using to allow function values in enums not to be considered methods, which I most likely got from this thread. In the example above, A would always be considered a method.

It seems like on 3.13.0, the "wrapper class" approach is the only one that works.

CPython versions tested on:

3.10, 3.11, 3.12, 3.13

Operating systems tested on:

Linux, Windows

Linked PRs

  • [3.13] gh-125316: Fix using partial() as Enum member #125361

Activity

  1. bluthej
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Oct 11, 2024
  2. ericvsmith commented on Oct 11, 2024

    @ericvsmith
    ericvsmith
    on Oct 11, 2024
    Member
    More actions

    @rhettinger

  3. ZeroIntensity
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    on Oct 11, 2024
  4. ZeroIntensity commented on Oct 11, 2024

    @ZeroIntensity
    ZeroIntensity
    on Oct 11, 2024
    Member
    More actions

    Confirmed on current main as well, technically a 3.13 regression.

  5. serhiy-storchaka commented on Oct 11, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 11, 2024
    Member
    More actions

    This is an intentional change, and it is documented. See #121027.

    Using staticmethod() to set a callable attribute without making it a method is more common idiom.

    Reacted by Peter Bierma
  6. bluthej commented on Oct 11, 2024

    @bluthej
    bluthej
    on Oct 11, 2024
    Author
    More actions

    @serhiy-storchaka maybe my title was not well formulated. This is what my issue boiled down to in my mind but I realize this is not exactly my problem, my problem is with the behavior I laid out in the snippet.

    It used to be that we could use partial in order to use a function as the value for an enum variant while still having that variant act like one. With the breaking change you mentioned this is no longer possible, the enum variant is considered to be the function itself. But that's just a consequence (maybe even unintended, I don't know).

    At least there is still the wrapper class approach that works! I guess I'll just update the stackoverflow thread I mentioned in case people run into that.

  7. bluthej commented on Oct 11, 2024

    @bluthej
    bluthej
    on Oct 11, 2024
    Author
    More actions

    I should probably add that using staticmethod in that context doesn't do the trick.

  8. serhiy-storchaka commented on Oct 11, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 11, 2024
    Member
    More actions

    As was mentioned in other answer, there is a better solution -- use enum.member() (I did not know about it!).

    Reacted by Joffrey Bluthé
  9. bluthej commented on Oct 11, 2024

    @bluthej
    bluthej
    on Oct 11, 2024
    Author
    More actions

    Thanks for the tip! I had seen that answer but since it wasn't upvoted much I didn't try 😅

  10. serhiy-storchaka
    added a commit that references this issue on Oct 12, 2024

    pythongh-125316: Fix using partial() as Enum member

    ca976c9
  11. serhiy-storchaka commented on Oct 12, 2024

    @serhiy-storchaka
    serhiy-storchaka
    on Oct 12, 2024
    Member
    More actions

    #125361 should fix this for 3.13. It restores the old behavior of partial in enums and adds a runtime warning. In 3.14 the behavior will be changed in any case.

    We tried to emit a warning about future behavior change, but adding a warning already changed the behavior in some cases.

    Reacted by Joffrey Bluthé
  12. ZeroIntensity commented on Oct 14, 2024

    @ZeroIntensity
    ZeroIntensity
    on Oct 14, 2024
    Member
    More actions

    @hacscred This kind of feedback isn't helpful--we don't intentionally break code, this is a regression. Please read PEP-387 for our backwards compatibility policy.

    Reacted by Jacob Coffee and Eric V. Smith
  13. serhiy-storchaka
    added a commit that references this issue on Oct 21, 2024

    [3.13] gh-125316: Fix using partial() as Enum member (GH-125361)

    65e43ca
  14. serhiy-storchaka
    removed
    3.14bugs and security fixesbugs and security fixes
    on Oct 21, 2024
  15. serhiy-storchaka
    closed this as completedon Oct 21, 2024
  16. MegaIng commented on Oct 25, 2024

    @MegaIng
    MegaIng
    on Oct 25, 2024
    More actions

    This kind of feedback isn't helpful--we don't intentionally break code, this is a regression. Please read PEP-387 for our backwards compatibility policy.

    No, this is clearly wrong. #121089 was an intentional break in backwards compatibility, disregarding PEP-387 with the argument that "this will only rarely break code".

  17. ZeroIntensity commented on Oct 25, 2024

    @ZeroIntensity
    ZeroIntensity
    on Oct 25, 2024
    Member
    More actions

    Yes, but this specific case with Enum was a regression and was fixed for this case. If that PR is causing more problems, open a new issue.

  18. rytilahti
    added a commit that references this issue on Jan 4, 2025

    Fix partial in enum for Python 3.13 (#1993)

    0aa4df3
  19. kmantel
    added a commit that references this issue on Jul 15, 2026

    conditions: Operation: support function as Enum member in python 3.13+

    4769df0
  20. kmantel
    added a commit that references this issue on Nov 6, 2026

    conditions: Operation: support function as Enum member in python 3.13+

    e1f3693
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixesonly security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.