File tree 2 files changed +21
-8
lines changed
2 files changed +21
-8
lines changed Original file line number Diff line number Diff line change @@ -731,14 +731,16 @@ def __contains__(cls, value):
731
731
"""
732
732
if isinstance (value , cls ):
733
733
return True
734
- try :
735
- cls (value )
736
- return True
737
- except ValueError :
738
- return (
739
- value in cls ._unhashable_values_ # both structures are lists
740
- or value in cls ._hashable_values_
741
- )
734
+ if issubclass (cls , Flag ):
735
+ try :
736
+ result = cls ._missing_ (value )
737
+ return isinstance (result , cls )
738
+ except ValueError :
739
+ pass
740
+ return (
741
+ value in cls ._unhashable_values_ # both structures are lists
742
+ or value in cls ._hashable_values_
743
+ )
742
744
743
745
def __delattr__ (cls , attr ):
744
746
# nicer error message when someone tries to delete an attribute
Original file line number Diff line number Diff line change @@ -1569,6 +1569,17 @@ class IntFlag1(IntFlag):
1569
1569
self .assertIn (IntEnum1 .X , IntFlag1 )
1570
1570
self .assertIn (IntFlag1 .X , IntEnum1 )
1571
1571
1572
+ def test_contains_does_not_call_missing (self ):
1573
+ class AnEnum (Enum ):
1574
+ UNKNOWN = None
1575
+ LUCKY = 3
1576
+ @classmethod
1577
+ def _missing_ (cls , * values ):
1578
+ return cls .UNKNOWN
1579
+ self .assertTrue (None in AnEnum )
1580
+ self .assertTrue (3 in AnEnum )
1581
+ self .assertFalse (7 in AnEnum )
1582
+
1572
1583
def test_inherited_data_type (self ):
1573
1584
class HexInt (int ):
1574
1585
__qualname__ = 'HexInt'
You can’t perform that action at this time.
0 commit comments