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 @@ -750,14 +750,16 @@ def __contains__(cls, value):
750
750
"""
751
751
if isinstance (value , cls ):
752
752
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
+ )
761
763
762
764
def __delattr__ (cls , attr ):
763
765
# 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):
1583
1583
self .assertIn (IntEnum1 .X , IntFlag1 )
1584
1584
self .assertIn (IntFlag1 .X , IntEnum1 )
1585
1585
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
+
1586
1597
def test_inherited_data_type (self ):
1587
1598
class HexInt (int ):
1588
1599
__qualname__ = 'HexInt'
You can’t perform that action at this time.
0 commit comments