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 @@ -750,14 +750,16 @@ def __contains__(cls, value):
750750 """
751751 if isinstance (value , cls ):
752752 return True
753- try :
754- cls (value )
755- return True
756- except ValueError :
757- return (
758- value in cls ._unhashable_values_ # both structures are lists
759- or value in cls ._hashable_values_
760- )
753+ if issubclass (cls , Flag ):
754+ try :
755+ result = cls ._missing_ (value )
756+ return isinstance (result , cls )
757+ except ValueError :
758+ pass
759+ return (
760+ value in cls ._unhashable_values_ # both structures are lists
761+ or value in cls ._hashable_values_
762+ )
761763
762764 def __delattr__ (cls , attr ):
763765 # nicer error message when someone tries to delete an attribute
Original file line number Diff line number Diff line change @@ -1583,6 +1583,17 @@ class IntFlag1(IntFlag):
15831583 self .assertIn (IntEnum1 .X , IntFlag1 )
15841584 self .assertIn (IntFlag1 .X , IntEnum1 )
15851585
1586+ def test_contains_does_not_call_missing (self ):
1587+ class AnEnum (Enum ):
1588+ UNKNOWN = None
1589+ LUCKY = 3
1590+ @classmethod
1591+ def _missing_ (cls , * values ):
1592+ return cls .UNKNOWN
1593+ self .assertTrue (None in AnEnum )
1594+ self .assertTrue (3 in AnEnum )
1595+ self .assertFalse (7 in AnEnum )
1596+
15861597 def test_inherited_data_type (self ):
15871598 class HexInt (int ):
15881599 __qualname__ = 'HexInt'
You can’t perform that action at this time.
0 commit comments