Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-92647: [Enum] use final status to determine lookup or create#99500

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanfurman merged 3 commits intopython:mainfromethanfurman:enum-value_lookup
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
use final status to determine lookup or create
  • Loading branch information
@ethanfurman
ethanfurman committedNov 15, 2022
commite1599ae92b7ae045c3d7ccc9467f8e8689df1f4d
13 changes: 9 additions & 4 deletionsLib/enum.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -692,14 +692,16 @@ def __bool__(cls):
"""
return True

def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None):
def __call__(cls, value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None):
"""
Either returns an existing member, or creates a new enum class.

This method is used both when an enum class is given a value to match
to an enumeration member (i.e. Color(3)) and for the functional API
(i.e. Color = Enum('Color', names='RED GREEN BLUE')).

The value lookup branch is chosen if the enum is final.

When used for the functional API:

`value` will be the name of the new class.
Expand All@@ -717,12 +719,15 @@ def __call__(cls, value, names=None, *, module=None, qualname=None, type=None, s

`type`, if set, will be mixed in as the first base class.
"""
if names is None: # simple value lookup
if cls._member_map_:
# simple value lookup if members exist
if names:
value = (value, names) + values
return cls.__new__(cls, value)
# otherwise, functional API: we're creating a new Enum type
return cls._create_(
value,
names,
class_name=value,
names=names,
module=module,
qualname=qualname,
type=type,
Expand Down
21 changes: 18 additions & 3 deletionsLib/test/test_enum.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1321,6 +1321,21 @@ def test_programmatic_function_type_from_subclass_with_start(self):
self.assertIn(e, MinorEnum)
self.assertIs(type(e), MinorEnum)

def test_programmatic_function_is_value_call(self):
class TwoPart(Enum):
ONE = 1, 1.0
TWO = 2, 2.0
THREE = 3, 3.0
self.assertRaisesRegex(ValueError, '1 is not a valid .*TwoPart', TwoPart, 1)
self.assertIs(TwoPart((1, 1.0)), TwoPart.ONE)
self.assertIs(TwoPart(1, 1.0), TwoPart.ONE)
class ThreePart(Enum):
ONE = 1, 1.0, 'one'
TWO = 2, 2.0, 'two'
THREE = 3, 3.0, 'three'
self.assertIs(ThreePart((3, 3.0, 'three')), ThreePart.THREE)
self.assertIs(ThreePart(3, 3.0, 'three'), ThreePart.THREE)

def test_intenum_from_bytes(self):
self.assertIs(IntStooges.from_bytes(b'\x00\x03', 'big'), IntStooges.MOE)
with self.assertRaises(ValueError):
Expand DownExpand Up@@ -1463,7 +1478,7 @@ class MoreColor(Color):
class EvenMoreColor(Color, IntEnum):
chartruese = 7
#
with self.assertRaisesRegex(TypeError, "<enum.Foo.> cannot extend <enum .Color.>"):
with self.assertRaisesRegex(ValueError, "\(.Foo., \(.pink., .black.\)\) is not a valid .*Color"):
Color('Foo', ('pink', 'black'))

def test_exclude_methods(self):
Expand DownExpand Up@@ -4181,7 +4196,7 @@ class TestEnumTypeSubclassing(unittest.TestCase):
Help on class Color in module %s:

class Color(enum.Enum)
| Color(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
| Color(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)
|
| Method resolution order:
| Color
Expand DownExpand Up@@ -4237,7 +4252,7 @@ class Color(enum.Enum)
Help on class Color in module %s:

class Color(enum.Enum)
| Color(value, names=None, *, module=None, qualname=None, type=None, start=1)
| Color(value, names=None, *values, module=None, qualname=None, type=None, start=1)
|
| Method resolution order:
| Color
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp