[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-04 Thread Ethan Furman

Ethan Furman  added the comment:

Nitish, Flag can check if the current Flag class has a mixin, and if so if the 
object being checked for is of that same type.

Dutcho, my apologies.  Looks like I did not fully understand how __contains__ 
is supposed to work and a TypeError is completely appropriate.

Looking into deprecation cycles now to get the change scheduled.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-04 Thread Dutcho

Dutcho  added the comment:

@Nitish
The easiest way would probably be to change __contains__ in Flag to:

def __contains__(self, other):
try:
return other & self == other # leave selection of _value_ attribute 
(if other is Flag) or conversion (if other is int mixin of IntFlag) to __and__
except TypeError:
return False

Although this would be somewhat convoluted (the generic delegation to __and__ 
isn't clear at first sight and therefore less maintainable) and may lead to 
confusing error messages

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-03 Thread Nitish

Nitish  added the comment:

@Ethan Furman how can Flag do that? IntFlag can deal with int values too. Would 
it be possible to deal with int values in this case in Flag.__contains__?

--
nosy: +nitishch

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-03 Thread Ethan Furman

Ethan Furman  added the comment:

issue33217 will not be "fixed" with a TypeError, but incorrect Falses are also 
bad.

Rather than add __contains__ to IntFlag (and every other Flag mixin), I think 
the best answer is to adjust Flag.__contains__ a little bit more to check if 
`other` is of the same type as the mixin class, and if so see if it's one of 
the values.

Thank you for being thorough and finding this problem as well.

--
keywords: +easy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-03 Thread Ethan Furman

Change by Ethan Furman :


--
assignee:  -> ethan.furman
nosy: +barry, eli.bendersky, ethan.furman
versions: +Python 3.8 -Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue33219] x in IntFlag() should test int x's inclusion in IntFlag

2018-04-03 Thread Dutcho

New submission from Dutcho :

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)


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__

--
components: Library (Lib)
messages: 314893
nosy: Dutcho
priority: normal
severity: normal
status: open
title: x in IntFlag() should test int x's inclusion in IntFlag
type: enhancement
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com