summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEthan Furman <ethan@stoneleaf.us>2021-08-21 13:05:47 -0700
committerEthan Furman <ethan@stoneleaf.us>2021-08-21 13:05:47 -0700
commit428583af7cca10b56f3012680d398005a51abcf0 (patch)
treed512eb435a96bb306c0a16c5fabd9b9365faba13
parentf323326984e1d61bc170894fa195d8bd8e48f4f0 (diff)
downloadcpython-git-pr_27789.tar.gz
use last module if multi-module stringpr_27789
when an enum's `__module__` contains several module names, only use the last one
-rw-r--r--Lib/enum.py17
-rw-r--r--Lib/test/test_enum.py82
2 files changed, 58 insertions, 41 deletions
diff --git a/Lib/enum.py b/Lib/enum.py
index 227090be5c..8fe98839b9 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -1390,17 +1390,28 @@ def _power_of_two(value):
return value == 2 ** _high_bit(value)
def global_enum_repr(self):
- return '%s.%s' % (self.__class__.__module__, self._name_)
+ """
+ use module.enum_name instead of class.enum_name
+
+ the module is the last module in case of a multi-module name
+ """
+ module = self.__class__.__module__.split('.')[-1]
+ return '%s.%s' % (module, self._name_)
def global_flag_repr(self):
- module = self.__class__.__module__
+ """
+ use module.flag_name instead of class.flag_name
+
+ the module is the last module in case of a multi-module name
+ """
+ module = self.__class__.__module__.split('.')[-1]
cls_name = self.__class__.__name__
if self._name_ is None:
return "%s.%s(%x)" % (module, cls_name, self._value_)
if _is_single_bit(self):
return '%s.%s' % (module, self._name_)
if self._boundary_ is not FlagBoundary.KEEP:
- return module + module.join(self.name.split('|'))
+ return '|'.join(['%s.%s' % (module, name) for name in self.name.split('|')])
else:
name = []
for n in self._name_.split('|'):
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py
index 1cc4d9c3ad..e755db587f 100644
--- a/Lib/test/test_enum.py
+++ b/Lib/test/test_enum.py
@@ -28,6 +28,9 @@ def load_tests(loader, tests, ignore):
))
return tests
+MODULE = ('test.test_enum', '__main__')[__name__=='__main__']
+SHORT_MODULE = MODULE.split('.')[-1]
+
# for pickle tests
try:
class Stooges(Enum):
@@ -143,6 +146,23 @@ class classproperty:
def __get__(self, instance, ownerclass):
return self.fget(ownerclass)
+# for global repr tests
+
+@enum.global_enum
+class HeadlightsK(IntFlag, boundary=enum.KEEP):
+ OFF_K = 0
+ LOW_BEAM_K = auto()
+ HIGH_BEAM_K = auto()
+ FOG_K = auto()
+
+
+@enum.global_enum
+class HeadlightsC(IntFlag, boundary=enum.CONFORM):
+ OFF_C = 0
+ LOW_BEAM_C = auto()
+ HIGH_BEAM_C = auto()
+ FOG_C = auto()
+
# tests
@@ -3112,20 +3132,6 @@ class TestFlag(unittest.TestCase):
self.assertFalse(NeverEnum.__dict__.get('_test2', False))
-@enum.global_enum
-class HeadlightsK(IntFlag, boundary=enum.KEEP):
- OFF = 0
- LOW_BEAM = auto()
- HIGH_BEAM = auto()
- FOG = auto()
-
-
-@enum.global_enum
-class HeadlightsC(IntFlag, boundary=enum.CONFORM):
- OFF = 0
- LOW_BEAM = auto()
- HIGH_BEAM = auto()
- FOG = auto()
class TestIntFlag(unittest.TestCase):
@@ -3240,23 +3246,25 @@ class TestIntFlag(unittest.TestCase):
self.assertEqual(repr(~(Open.WO | Open.CE)), 'Open.RW')
self.assertEqual(repr(Open(~4)), '-5')
- def test_global_repr_keep1(self):
- self.assertEqual(repr(HeadlightsK(0)), 'test.test_enum.HeadlightsK.OFF')
-
- def test_global_repr_keep2(self):
+ def test_global_repr_keep(self):
+ self.assertEqual(
+ repr(HeadlightsK(0)),
+ '%s.OFF_K' % SHORT_MODULE,
+ )
self.assertEqual(
- repr(HeadlightsK(2**0 + 2**2 + 2**3)),
- 'test.test_enum.HeadlightsK.LOW_BEAM|test.test_enum.HeadlightsK.FOG|0x8',
- )
+ repr(HeadlightsK(2**0 + 2**2 + 2**3)),
+ '%(m)s.LOW_BEAM_K|%(m)s.FOG_K|0x8' % {'m': SHORT_MODULE},
+ )
def test_global_repr_conform1(self):
- self.assertEqual(repr(HeadlightsC(0)), 'test.test_enum.HeadlightsC.OFF')
-
- def test_global_repr_conform2(self):
self.assertEqual(
- repr(HeadlightsC(2**0 + 2**2 + 2**3)),
- 'test.test_enum.HeadlightsC.LOW_BEAM|test.test_enum.HeadlightsC.FOG|0x8',
- )
+ repr(HeadlightsC(0)),
+ '%s.OFF_C' % SHORT_MODULE,
+ )
+ self.assertEqual(
+ repr(HeadlightsC(2**0 + 2**2 + 2**3)),
+ '%(m)s.LOW_BEAM_C|%(m)s.FOG_C' % {'m': SHORT_MODULE},
+ )
def test_format(self):
Perm = self.Perm
@@ -4119,7 +4127,7 @@ class TestIntEnumConvert(unittest.TestCase):
def test_convert_value_lookup_priority(self):
test_type = enum.IntEnum._convert_(
'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
+ MODULE,
filter=lambda x: x.startswith('CONVERT_TEST_'))
# We don't want the reverse lookup value to vary when there are
# multiple possible names for a given value. It should always
@@ -4129,7 +4137,7 @@ class TestIntEnumConvert(unittest.TestCase):
def test_convert(self):
test_type = enum.IntEnum._convert_(
'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
+ MODULE,
filter=lambda x: x.startswith('CONVERT_TEST_'))
# Ensure that test_type has all of the desired names and values.
self.assertEqual(test_type.CONVERT_TEST_NAME_F,
@@ -4149,7 +4157,7 @@ class TestIntEnumConvert(unittest.TestCase):
with self.assertWarns(DeprecationWarning):
enum.IntEnum._convert(
'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
+ MODULE,
filter=lambda x: x.startswith('CONVERT_TEST_'))
@unittest.skipUnless(python_version >= (3, 9),
@@ -4158,16 +4166,15 @@ class TestIntEnumConvert(unittest.TestCase):
with self.assertRaises(AttributeError):
enum.IntEnum._convert(
'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
+ MODULE,
filter=lambda x: x.startswith('CONVERT_TEST_'))
def test_convert_repr_and_str(self):
- module = ('test.test_enum', '__main__')[__name__=='__main__']
test_type = enum.IntEnum._convert_(
'UnittestConvert',
- module,
+ MODULE,
filter=lambda x: x.startswith('CONVERT_STRING_TEST_'))
- self.assertEqual(repr(test_type.CONVERT_STRING_TEST_NAME_A), '%s.CONVERT_STRING_TEST_NAME_A' % module)
+ self.assertEqual(repr(test_type.CONVERT_STRING_TEST_NAME_A), '%s.CONVERT_STRING_TEST_NAME_A' % SHORT_MODULE)
self.assertEqual(str(test_type.CONVERT_STRING_TEST_NAME_A), 'CONVERT_STRING_TEST_NAME_A')
self.assertEqual(format(test_type.CONVERT_STRING_TEST_NAME_A), '5')
@@ -4185,7 +4192,7 @@ class TestStrEnumConvert(unittest.TestCase):
def test_convert(self):
test_type = enum.StrEnum._convert_(
'UnittestConvert',
- ('test.test_enum', '__main__')[__name__=='__main__'],
+ MODULE,
filter=lambda x: x.startswith('CONVERT_STR_'))
# Ensure that test_type has all of the desired names and values.
self.assertEqual(test_type.CONVERT_STR_TEST_1, 'hello')
@@ -4196,12 +4203,11 @@ class TestStrEnumConvert(unittest.TestCase):
[], msg='Names other than CONVERT_STR_* found.')
def test_convert_repr_and_str(self):
- module = ('test.test_enum', '__main__')[__name__=='__main__']
test_type = enum.StrEnum._convert_(
'UnittestConvert',
- module,
+ MODULE,
filter=lambda x: x.startswith('CONVERT_STR_'))
- self.assertEqual(repr(test_type.CONVERT_STR_TEST_1), '%s.CONVERT_STR_TEST_1' % module)
+ self.assertEqual(repr(test_type.CONVERT_STR_TEST_1), '%s.CONVERT_STR_TEST_1' % SHORT_MODULE)
self.assertEqual(str(test_type.CONVERT_STR_TEST_2), 'goodbye')
self.assertEqual(format(test_type.CONVERT_STR_TEST_1), 'hello')