mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
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>
This commit is contained in:
parent
07804ce24c
commit
700ec657c8
5 changed files with 85 additions and 41 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue