mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-92647: [Enum] use final status to determine lookup or create (GH-99500)
* use final status to determine lookup or create * 📜🤖 Added by blurb_it. Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
parent
3c57971a2d
commit
65dab1506e
3 changed files with 28 additions and 7 deletions
13
Lib/enum.py
13
Lib/enum.py
|
@ -692,7 +692,7 @@ class EnumType(type):
|
|||
"""
|
||||
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.
|
||||
|
||||
|
@ -700,6 +700,8 @@ class EnumType(type):
|
|||
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.
|
||||
|
@ -717,12 +719,15 @@ class EnumType(type):
|
|||
|
||||
`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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue