diff options
author | Ethan Furman <ethan@stoneleaf.us> | 2023-04-18 23:19:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-18 23:19:23 (GMT) |
commit | 700ec657c80e78fb299963ffaa684c859ddb8f87 (patch) | |
tree | 61a25f3c6ed737451201814626b8af53e23db6ba /Lib/test/test_enum.py | |
parent | 07804ce24c3103ee1bb141af31b9a1a0f92f5e43 (diff) | |
download | cpython-700ec657c80e78fb299963ffaa684c859ddb8f87.zip cpython-700ec657c80e78fb299963ffaa684c859ddb8f87.tar.gz cpython-700ec657c80e78fb299963ffaa684c859ddb8f87.tar.bz2 |
gh-103596: [Enum] do not shadow mixed-in methods/attributes (GH-103600)
For example:
class Book(StrEnum):
title = auto()
author = auto()
desc = auto()
Book.author.desc is Book.desc
but
Book.author.title() == 'Author'
is commonly expected. Using upper-case member names avoids this confusion and possible performance impacts.
Co-authored-by: samypr100 <3933065+samypr100@users.noreply.github.com>
Diffstat (limited to 'Lib/test/test_enum.py')
-rw-r--r-- | Lib/test/test_enum.py | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/Lib/test/test_enum.py b/Lib/test/test_enum.py index e9dfcf8..fb7a016 100644 --- a/Lib/test/test_enum.py +++ b/Lib/test/test_enum.py @@ -819,10 +819,27 @@ class TestPlainFlag(_EnumTests, _PlainOutputTests, _FlagTests, unittest.TestCase class TestIntEnum(_EnumTests, _MinimalOutputTests, unittest.TestCase): enum_type = IntEnum + # + def test_shadowed_attr(self): + class Number(IntEnum): + divisor = 1 + numerator = 2 + # + self.assertEqual(Number.divisor.numerator, 1) + self.assertIs(Number.numerator.divisor, Number.divisor) class TestStrEnum(_EnumTests, _MinimalOutputTests, unittest.TestCase): enum_type = StrEnum + # + def test_shadowed_attr(self): + class Book(StrEnum): + author = 'author' + title = 'title' + # + self.assertEqual(Book.author.title(), 'Author') + self.assertEqual(Book.title.title(), 'Title') + self.assertIs(Book.title.author, Book.author) class TestIntFlag(_EnumTests, _MinimalOutputTests, _FlagTests, unittest.TestCase): |