@@ -276,9 +276,10 @@ def __set_name__(self, enum_class, member_name):
276
276
enum_member ._sort_order_ = len (enum_class ._member_names_ )
277
277
278
278
if Flag is not None and issubclass (enum_class , Flag ):
279
- enum_class ._flag_mask_ |= value
280
- if _is_single_bit (value ):
281
- enum_class ._singles_mask_ |= value
279
+ if isinstance (value , int ):
280
+ enum_class ._flag_mask_ |= value
281
+ if _is_single_bit (value ):
282
+ enum_class ._singles_mask_ |= value
282
283
enum_class ._all_bits_ = 2 ** ((enum_class ._flag_mask_ ).bit_length ()) - 1
283
284
284
285
# If another member with the same value was already defined, the
@@ -306,6 +307,7 @@ def __set_name__(self, enum_class, member_name):
306
307
elif (
307
308
Flag is not None
308
309
and issubclass (enum_class , Flag )
310
+ and isinstance (value , int )
309
311
and _is_single_bit (value )
310
312
):
311
313
# no other instances found, record this member in _member_names_
@@ -1502,37 +1504,50 @@ def __str__(self):
1502
1504
def __bool__ (self ):
1503
1505
return bool (self ._value_ )
1504
1506
1507
+ def _get_value (self , flag ):
1508
+ if isinstance (flag , self .__class__ ):
1509
+ return flag ._value_
1510
+ elif self ._member_type_ is not object and isinstance (flag , self ._member_type_ ):
1511
+ return flag
1512
+ return NotImplemented
1513
+
1505
1514
def __or__ (self , other ):
1506
- if isinstance (other , self .__class__ ):
1507
- other = other ._value_
1508
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1509
- other = other
1510
- else :
1515
+ other_value = self ._get_value (other )
1516
+ if other_value is NotImplemented :
1511
1517
return NotImplemented
1518
+
1519
+ for flag in self , other :
1520
+ if self ._get_value (flag ) is None :
1521
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with |" )
1512
1522
value = self ._value_
1513
- return self .__class__ (value | other )
1523
+ return self .__class__ (value | other_value )
1514
1524
1515
1525
def __and__ (self , other ):
1516
- if isinstance (other , self .__class__ ):
1517
- other = other ._value_
1518
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1519
- other = other
1520
- else :
1526
+ other_value = self ._get_value (other )
1527
+ if other_value is NotImplemented :
1521
1528
return NotImplemented
1529
+
1530
+ for flag in self , other :
1531
+ if self ._get_value (flag ) is None :
1532
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with &" )
1522
1533
value = self ._value_
1523
- return self .__class__ (value & other )
1534
+ return self .__class__ (value & other_value )
1524
1535
1525
1536
def __xor__ (self , other ):
1526
- if isinstance (other , self .__class__ ):
1527
- other = other ._value_
1528
- elif self ._member_type_ is not object and isinstance (other , self ._member_type_ ):
1529
- other = other
1530
- else :
1537
+ other_value = self ._get_value (other )
1538
+ if other_value is NotImplemented :
1531
1539
return NotImplemented
1540
+
1541
+ for flag in self , other :
1542
+ if self ._get_value (flag ) is None :
1543
+ raise TypeError (f"'{ flag } ' cannot be combined with other flags with ^" )
1532
1544
value = self ._value_
1533
- return self .__class__ (value ^ other )
1545
+ return self .__class__ (value ^ other_value )
1534
1546
1535
1547
def __invert__ (self ):
1548
+ if self ._get_value (self ) is None :
1549
+ raise TypeError (f"'{ self } ' cannot be inverted" )
1550
+
1536
1551
if self ._inverted_ is None :
1537
1552
if self ._boundary_ in (EJECT , KEEP ):
1538
1553
self ._inverted_ = self .__class__ (~ self ._value_ )
0 commit comments