Skip to content

bpo-38250: minor Flag refactor #22734

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
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
16 changes: 8 additions & 8 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ def _create_pseudo_member_(cls, value):
pseudo_member = cls._value2member_map_.setdefault(value, pseudo_member)
return pseudo_member

# TODO: document that Flag `in` is a subset operation
def __contains__(self, other):
if not isinstance(other, self.__class__):
raise TypeError(
Expand All @@ -784,14 +785,14 @@ def __contains__(self, other):
return other._value_ & self._value_ == other._value_

def __iter__(self):
members, extra_flags = _decompose(self.__class__, self.value)
members, _ = _decompose(self.__class__, self.value)
return (m for m in members if m._value_ != 0)

def __repr__(self):
cls = self.__class__
if self._name_ is not None:
return '<%s.%s: %r>' % (cls.__name__, self._name_, self._value_)
members, uncovered = _decompose(cls, self._value_)
members, _ = _decompose(cls, self._value_)
return '<%s.%s: %r>' % (
cls.__name__,
'|'.join([str(m._name_ or m._value_) for m in members]),
Expand All @@ -802,7 +803,7 @@ def __str__(self):
cls = self.__class__
if self._name_ is not None:
return '%s.%s' % (cls.__name__, self._name_)
members, uncovered = _decompose(cls, self._value_)
members, _ = _decompose(cls, self._value_)
if len(members) == 1 and members[0]._name_ is None:
return '%s.%r' % (cls.__name__, members[0]._value_)
else:
Expand Down Expand Up @@ -830,12 +831,12 @@ def __xor__(self, other):
return self.__class__(self._value_ ^ other._value_)

def __invert__(self):
members, uncovered = _decompose(self.__class__, self._value_)
members, _ = _decompose(self.__class__, self._value_)
inverted = self.__class__(0)
for m in self.__class__:
if m not in members and not (m._value_ & self._value_):
inverted = inverted | m
return self.__class__(inverted)
inverted |= m
return inverted


class IntFlag(int, Flag):
Expand Down Expand Up @@ -899,8 +900,7 @@ def __xor__(self, other):
__rxor__ = __xor__

def __invert__(self):
result = self.__class__(~self._value_)
return result
return self.__class__(~self._value_)


def _high_bit(value):
Expand Down