File tree Expand file tree Collapse file tree 2 files changed +21
-8
lines changed
Expand file tree Collapse file tree 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):
731731 """
732732 if isinstance (value , cls ):
733733 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+ )
742744
743745 def __delattr__ (cls , attr ):
744746 # 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):
15691569 self .assertIn (IntEnum1 .X , IntFlag1 )
15701570 self .assertIn (IntFlag1 .X , IntEnum1 )
15711571
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+
15721583 def test_inherited_data_type (self ):
15731584 class HexInt (int ):
15741585 __qualname__ = 'HexInt'
You can’t perform that action at this time.
0 commit comments