Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 65e43ca

Browse files
[3.13] gh-125316: Fix using partial() as Enum member (GH-125361)
A FutureWarning with suggestion to use enum.member() is now emitted when the partial instance is used as an enum member.
1 parent d9dafc7 commit 65e43ca

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/enum.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import builtins as bltns
3+
from functools import partial
34
from types import MappingProxyType, DynamicClassAttribute
45

56

@@ -37,7 +38,7 @@ def _is_descriptor(obj):
3738
"""
3839
Returns True if obj is a descriptor, False otherwise.
3940
"""
40-
return (
41+
return not isinstance(obj, partial) and (
4142
hasattr(obj, '__get__') or
4243
hasattr(obj, '__set__') or
4344
hasattr(obj, '__delete__')
@@ -402,6 +403,12 @@ def __setitem__(self, key, value):
402403
elif isinstance(value, nonmember):
403404
# unwrap value here; it won't be processed by the below `else`
404405
value = value.value
406+
elif isinstance(value, partial):
407+
import warnings
408+
warnings.warn('functools.partial will be a method descriptor '
409+
'in future Python versions; wrap it in '
410+
'enum.member() if you want to preserve the '
411+
'old behavior', FutureWarning, stacklevel=2)
405412
elif _is_descriptor(value):
406413
pass
407414
elif _is_internal_class(self._cls_name, value):

Lib/test/test_enum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import builtins as bltns
1212
from collections import OrderedDict
1313
from datetime import date
14+
from functools import partial
1415
from enum import Enum, EnumMeta, IntEnum, StrEnum, EnumType, Flag, IntFlag, unique, auto
1516
from enum import STRICT, CONFORM, EJECT, KEEP, _simple_enum, _test_simple_enum
1617
from enum import verify, UNIQUE, CONTINUOUS, NAMED_FLAGS, ReprEnum
@@ -1537,6 +1538,19 @@ class Inner(Enum):
15371538
[Outer.a, Outer.b, Outer.Inner],
15381539
)
15391540

1541+
def test_partial(self):
1542+
def func(a, b=5):
1543+
return a, b
1544+
with self.assertWarnsRegex(FutureWarning, r'partial.*enum\.member') as cm:
1545+
class E(Enum):
1546+
a = 1
1547+
b = partial(func)
1548+
self.assertEqual(cm.filename, __file__)
1549+
self.assertIsInstance(E.b, partial)
1550+
self.assertEqual(E.b(2), (2, 5))
1551+
with self.assertWarnsRegex(FutureWarning, 'partial'):
1552+
self.assertEqual(E.a.b(2), (2, 5))
1553+
15401554
def test_enum_with_value_name(self):
15411555
class Huh(Enum):
15421556
name = 1
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix using :func:`functools.partial` as :class:`enum.Enum` member. A
2+
FutureWarning with suggestion to use :func:`enum.member` is now emitted when
3+
the ``partial`` instance is used as an enum member.

0 commit comments

Comments
 (0)