[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:
Ethan Furman 2018-04-11 18:56:25 -07:00 committed by GitHub
parent cbbf746186
commit 3715176557
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 78 additions and 5 deletions

View file

@ -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):