@@ -279,9 +279,10 @@ def __set_name__(self, enum_class, member_name):
279
279
enum_member ._sort_order_ = len (enum_class ._member_names_ )
280
280
281
281
if Flag is not None and issubclass (enum_class , Flag ):
282
- enum_class ._flag_mask_ |= value
283
- if _is_single_bit (value ):
284
- enum_class ._singles_mask_ |= value
282
+ if isinstance (value , int ):
283
+ enum_class ._flag_mask_ |= value
284
+ if _is_single_bit (value ):
285
+ enum_class ._singles_mask_ |= value
285
286
enum_class ._all_bits_ = 2 ** ((enum_class ._flag_mask_ ).bit_length ()) - 1
286
287
287
288
# If another member with the same value was already defined, the
@@ -309,6 +310,7 @@ def __set_name__(self, enum_class, member_name):
309
310
elif (
310
311
Flag is not None
311
312
and issubclass (enum_class , Flag )
313
+ and isinstance (value , int )
312
314
and _is_single_bit (value )
313
315
):
314
316
# no other instances found, record this member in _member_names_
@@ -1558,37 +1560,50 @@ def __str__(self):
1558
1560
def __bool__ (self ):
1559
1561
return bool (self ._value_ )
1560
1562
1563
+ def _get_value (self , flag ):
1564
+ if isinstance (flag , self .__class__ ):
1565
+ return flag ._value_
1566
+ elif self ._member_type_ is not object and isinstance (flag , self ._member_type_ ):
1567
+ return flag
1568
+ return NotImplemented
1569
+
1561
1570
def __or__ (self , other ):
1562
- if isinstance (other , self .__class__ ):
1563
- other = other ._value_
1564
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1565
- other = other
1566
- else :
1571
+ other_value = self ._get_value (other )
1572
+ if other_value is NotImplemented :
1567
1573
return NotImplemented
1574
+
1575
+ for flag in self , other :
1576
+ if self ._get_value (flag ) is None :
1577
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with |" )
1568
1578
value = self ._value_
1569
- return self .__class__ (value | other )
1579
+ return self .__class__ (value | other_value )
1570
1580
1571
1581
def __and__ (self , other ):
1572
- if isinstance (other , self .__class__ ):
1573
- other = other ._value_
1574
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1575
- other = other
1576
- else :
1582
+ other_value = self ._get_value (other )
1583
+ if other_value is NotImplemented :
1577
1584
return NotImplemented
1585
+
1586
+ for flag in self , other :
1587
+ if self ._get_value (flag ) is None :
1588
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with &" )
1578
1589
value = self ._value_
1579
- return self .__class__ (value & other )
1590
+ return self .__class__ (value & other_value )
1580
1591
1581
1592
def __xor__ (self , other ):
1582
- if isinstance (other , self .__class__ ):
1583
- other = other ._value_
1584
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1585
- other = other
1586
- else :
1593
+ other_value = self ._get_value (other )
1594
+ if other_value is NotImplemented :
1587
1595
return NotImplemented
1596
+
1597
+ for flag in self , other :
1598
+ if self ._get_value (flag ) is None :
1599
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with ^" )
1588
1600
value = self ._value_
1589
- return self .__class__ (value ^ other )
1601
+ return self .__class__ (value ^ other_value )
1590
1602
1591
1603
def __invert__ (self ):
1604
+ if self ._get_value (self ) is None :
1605
+ raise TypeError (f"'{ self } ' cannot be inverted" )
1606
+
1592
1607
if self ._inverted_ is None :
1593
1608
if self ._boundary_ in (EJECT , KEEP ):
1594
1609
self ._inverted_ = self .__class__ (~ self ._value_ )
0 commit comments