This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Dutcho
Recipients Dutcho
Date 2018-04-03.20:38:20
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1522787900.11.0.467229070634.issue33219@psf.upfronthosting.co.za>
In-reply-to
Content
While `enum.IntFlag.__and__` accepts an int arg `other` and converts it to `IntFlag` before masking, `enum.IntFlag.__contains__` handles an int arg `other` no different from a different type arg `other` (i.e. returns `True` in Python 3.6 due to issue 33217, but would raise `TypeError` after that's fixed):
    >>> import enum
    >>> ABC = enum.Flag('ABC', 'a, b, c')
    >>> ac = ABC.a | ABC.c
    >>> ABC.b in ac # works
    False
    >>> 2 in ac # should be the same; no exception due to issue 33217
    True
    >>> ac & 3 # works, equivalent to ac & ABC(3)
    <ABC.a: 1>

This is caused by a lack of specialized `IntFlag.__contains__`, so `Flag.__contains__` does the work. Can be fixed by adding a specialization, which (like in `IntFlag.__and__`) tests for `isinstance(other, (IntFlag, int))`.

    >>> def __contains__(self, other):
    ...     if not isinstance(other, (self.__class__, int)):
    ...         return TypeError
    ...     return other & self == other # conversion of int to IntFlag implicitly handled by IntFlag.__and__
    >>> IntFlag.__contains__ = __contains__
History
Date User Action Args
2018-04-03 20:38:20Dutchosetrecipients: + Dutcho
2018-04-03 20:38:20Dutchosetmessageid: <1522787900.11.0.467229070634.issue33219@psf.upfronthosting.co.za>
2018-04-03 20:38:20Dutcholinkissue33219 messages
2018-04-03 20:38:20Dutchocreate