Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Lib/enum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import builtins as bltns
from functools import partial
from types import MappingProxyType, DynamicClassAttribute


Expand Down Expand Up @@ -37,7 +38,7 @@ def _is_descriptor(obj):
"""
Returns True if obj is a descriptor, False otherwise.
"""
return (
return not isinstance(obj, partial) and (
hasattr(obj, '__get__') or
hasattr(obj, '__set__') or
hasattr(obj, '__delete__')
Expand Down Expand Up @@ -402,6 +403,12 @@ def __setitem__(self, key, value):
elif isinstance(value, nonmember):
# unwrap value here; it won't be processed by the below `else`
value = value.value
elif isinstance(value, partial):
import warnings
warnings.warn('functools.partial will be a method descriptor '
'in future Python versions; wrap it in '
'enum.member() if you want to preserve the '
'old behavior', FutureWarning, stacklevel=2)
elif _is_descriptor(value):
pass
elif _is_internal_class(self._cls_name, value):
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import builtins as bltns
from collections import OrderedDict
from datetime import date
from functools import partial
from enum import Enum, EnumMeta, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS, ReprEnum
Expand Down Expand Up @@ -1537,6 +1538,19 @@ class Inner(Enum):
[Outer.a, Outer.b, Outer.Inner],
)

def test_partial(self):
def func(a, b=5):
return a, b
with self.assertWarnsRegex(FutureWarning, r'partial.*enum\.member') as cm:
class E(Enum):
a = 1
b = partial(func)
self.assertEqual(cm.filename, __file__)
self.assertIsInstance(E.b, partial)
self.assertEqual(E.b(2), (2, 5))
with self.assertWarnsRegex(FutureWarning, 'partial'):
self.assertEqual(E.a.b(2), (2, 5))

def test_enum_with_value_name(self):
class Huh(Enum):
name = 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix using :func:`functools.partial` as :class:`enum.Enum` member. A
FutureWarning with suggestion to use :func:`enum.member` is now emitted when
the ``partial`` instance is used as an enum member.
Loading