mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
[3.7] bpo-33217: deprecate non-Enum lookups in Enums (GH-6392)
deprecate non-Enum lookups in Enums Lookups such as `1 in Color` and `2 in SomeFlag()` will raise TypeError in 3.8+.
This commit is contained in:
parent
cbbf746186
commit
3715176557
5 changed files with 78 additions and 5 deletions
13
Lib/enum.py
13
Lib/enum.py
|
@ -309,6 +309,12 @@ class EnumMeta(type):
|
|||
return cls._create_(value, names, module=module, qualname=qualname, type=type, start=start)
|
||||
|
||||
def __contains__(cls, member):
|
||||
if not isinstance(member, Enum):
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"using non-Enums in containment checks will raise "
|
||||
"TypeError in Python 3.8",
|
||||
DeprecationWarning, 2)
|
||||
return isinstance(member, cls) and member._name_ in cls._member_map_
|
||||
|
||||
def __delattr__(cls, attr):
|
||||
|
@ -713,7 +719,12 @@ class Flag(Enum):
|
|||
|
||||
def __contains__(self, other):
|
||||
if not isinstance(other, self.__class__):
|
||||
return NotImplemented
|
||||
import warnings
|
||||
warnings.warn(
|
||||
"using non-Flags in containment checks will raise "
|
||||
"TypeError in Python 3.8",
|
||||
DeprecationWarning, 2)
|
||||
return False
|
||||
return other._value_ & self._value_ == other._value_
|
||||
|
||||
def __repr__(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue