Skip to content

fix: wrap callable enum values with enum.member for python 3.13#583

Merged
gsprochette merged 2 commits intomainfrom
fix/wrap-callable-enum-values-with-enum.member-for-python-3.13
Apr 3, 2026
Merged

fix: wrap callable enum values with enum.member for python 3.13#583
gsprochette merged 2 commits intomainfrom
fix/wrap-callable-enum-values-with-enum.member-for-python-3.13

Conversation

@gsprochette
Copy link
Copy Markdown
Collaborator

Description

Problem: On Python 3.13, functools.partial used as an Enum member is treated as a method descriptor, so members like LOAD_FUNCTIONS.diffusers are no longer real enum members and accessing .name fails (e.g. during save_pruna_model).

Change: Wrap callable enum values with enum.member(...) in LOAD_FUNCTIONS, SAVE_FUNCTIONS, and the artifact enums. For Python 3.10 (no enum.member), use functools.partial aliased as member so functions are still stored as values, not methods.

Refs: PEP 663 — enum.member; CPython FutureWarning on partial in Enum for 3.11+ leading to the 3.13 behavior.

Related Issue

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

I ran a torchao test (including save and load using the new enum) on python 3.10 (no enum.member yet), 3.11 (enum.member introduced but partial still functional) and 3.13 (partial not functional anymore). All passed.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Additional Notes

Copy link
Copy Markdown
Member

@begumcig begumcig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, this looks really good to me already but since this is the first time I am seeing it I have a question: I believe in TorchMetrics enum in pruna/evaluation/metrics/metric.torch.py We also have a tuple where the first item is a class wrapped in partial (e.g.
fid = (partial(FrechetInceptionDistance), fid_update, "gt_y"). Would this also be a problem and needs to change?

@gsprochette
Copy link
Copy Markdown
Collaborator Author

Hi, this looks really good to me already but since this is the first time I am seeing it I have a question: I believe in TorchMetrics enum in pruna/evaluation/metrics/metric.torch.py We also have a tuple where the first item is a class wrapped in partial (e.g. fid = (partial(FrechetInceptionDistance), fid_update, "gt_y"). Would this also be a problem and needs to change?

Probably yes, I'll double check and look for any other Enum import to make sure. Thanks for pointing it out :)

@github-actions
Copy link
Copy Markdown

This PR has been inactive for 10 days and is now marked as stale.

@github-actions github-actions bot added the stale label Mar 31, 2026
@gsprochette gsprochette force-pushed the fix/wrap-callable-enum-values-with-enum.member-for-python-3.13 branch from 4f94b9b to 5a3beb0 Compare April 3, 2026 10:07
@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

TIP This summary will be updated as you push new changes. Give us feedback

@gsprochette
Copy link
Copy Markdown
Collaborator Author

Hello @begumcig , I reviewed the enum in TorchMetrics and it turns out that it doesn't suffer from the same problem: the member itself is not the partial but a tuple whose first element is the partial.
Because of this, the partial was not used in the first place (except in one case for passing arguments to the __init__), so I cleaned them up and updated the docstring which was still listing the value to be a callable. How does it look to you ?

@begumcig begumcig self-requested a review April 3, 2026 13:13
Copy link
Copy Markdown
Member

@begumcig begumcig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LSGTM!!

@gsprochette gsprochette merged commit 9b8eb07 into main Apr 3, 2026
7 checks passed
@gsprochette gsprochette deleted the fix/wrap-callable-enum-values-with-enum.member-for-python-3.13 branch April 3, 2026 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants