@@ -283,9 +283,10 @@ def __set_name__(self, enum_class, member_name):
283
283
enum_member ._sort_order_ = len (enum_class ._member_names_ )
284
284
285
285
if Flag is not None and issubclass (enum_class , Flag ):
286
- enum_class ._flag_mask_ |= value
287
- if _is_single_bit (value ):
288
- enum_class ._singles_mask_ |= value
286
+ if isinstance (value , int ):
287
+ enum_class ._flag_mask_ |= value
288
+ if _is_single_bit (value ):
289
+ enum_class ._singles_mask_ |= value
289
290
enum_class ._all_bits_ = 2 ** ((enum_class ._flag_mask_ ).bit_length ()) - 1
290
291
291
292
# If another member with the same value was already defined, the
@@ -313,6 +314,7 @@ def __set_name__(self, enum_class, member_name):
313
314
elif (
314
315
Flag is not None
315
316
and issubclass (enum_class , Flag )
317
+ and isinstance (value , int )
316
318
and _is_single_bit (value )
317
319
):
318
320
# no other instances found, record this member in _member_names_
@@ -1534,37 +1536,50 @@ def __str__(self):
1534
1536
def __bool__ (self ):
1535
1537
return bool (self ._value_ )
1536
1538
1539
+ def _get_value (self , flag ):
1540
+ if isinstance (flag , self .__class__ ):
1541
+ return flag ._value_
1542
+ elif self ._member_type_ is not object and isinstance (flag , self ._member_type_ ):
1543
+ return flag
1544
+ return NotImplemented
1545
+
1537
1546
def __or__ (self , other ):
1538
- if isinstance (other , self .__class__ ):
1539
- other = other ._value_
1540
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1541
- other = other
1542
- else :
1547
+ other_value = self ._get_value (other )
1548
+ if other_value is NotImplemented :
1543
1549
return NotImplemented
1550
+
1551
+ for flag in self , other :
1552
+ if self ._get_value (flag ) is None :
1553
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with |" )
1544
1554
value = self ._value_
1545
- return self .__class__ (value | other )
1555
+ return self .__class__ (value | other_value )
1546
1556
1547
1557
def __and__ (self , other ):
1548
- if isinstance (other , self .__class__ ):
1549
- other = other ._value_
1550
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1551
- other = other
1552
- else :
1558
+ other_value = self ._get_value (other )
1559
+ if other_value is NotImplemented :
1553
1560
return NotImplemented
1561
+
1562
+ for flag in self , other :
1563
+ if self ._get_value (flag ) is None :
1564
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with &" )
1554
1565
value = self ._value_
1555
- return self .__class__ (value & other )
1566
+ return self .__class__ (value & other_value )
1556
1567
1557
1568
def __xor__ (self , other ):
1558
- if isinstance (other , self .__class__ ):
1559
- other = other ._value_
1560
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1561
- other = other
1562
- else :
1569
+ other_value = self ._get_value (other )
1570
+ if other_value is NotImplemented :
1563
1571
return NotImplemented
1572
+
1573
+ for flag in self , other :
1574
+ if self ._get_value (flag ) is None :
1575
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with ^" )
1564
1576
value = self ._value_
1565
- return self .__class__ (value ^ other )
1577
+ return self .__class__ (value ^ other_value )
1566
1578
1567
1579
def __invert__ (self ):
1580
+ if self ._get_value (self ) is None :
1581
+ raise TypeError (f"'{ self } ' cannot be inverted" )
1582
+
1568
1583
if self ._inverted_ is None :
1569
1584
if self ._boundary_ in (EJECT , KEEP ):
1570
1585
self ._inverted_ = self .__class__ (~ self ._value_ )
0 commit comments