[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Python-Dev] Enum, Flag, __contains__, and False vs TypeError

On Apr 4, 2018, at 11:32, Ethan Furman <ethan at stoneleaf.us> wrote:
> (A) A standard Enum class is a container of Enum members.  It cannot hold anything else.  However, it has been returning False both in cases where the in-question object was an Enum of a different class (a Fruit in a Color, for example) and when the in-question object was not even an Enum ('apple' in Fruit, for example).
> (B) The waters get even more muddied when Fruit has a str mixin, so `Fruit.APPLE == 'apple' is True` -- in that case, should `'orange' in Fruit` return True, False, or raise TypeError?

Are you proposing to change current behavior, and if so, what?s your deprecation plan?

I?m not sure I feel that the purity is important enough to change how it currently works, especially since you?ll have to be prepared to catch exceptions rather than just handle the boolean value.  OTOH, since most of my use cases are comparisons against explicit enum values, I?m not sure how often people write code to check for enum values contained in the Enum (I do it in one or two places where I?m deserializing the actual value object, e.g. from a pickle).

(FWIW, I encourage individual comparisons use `is` rather than `==`.)

> Question 2:
> ----------
> (A) The new Flag type allows `in` tests on the members themselves; so, for example:
> --> SomeFlag.ONE in SomeFlag.One|SomeFlag.TWO
> True
> The question, of course, is what to do when a non-Flag member is tested for:
> --> 'apple' in SomeFlag.ONE
> # False or TypeError?
> --> 2 in SomeFlag.TWO
> # True or TypeError?
> (B) And, of course, the same muddier question arises with IntFlag, where SomeFlag.TWO == 2 is True.

Well, now I?m confused:

Python 3.7.0b2+ (heads/3.7:f328caf4ca, Mar 26 2018, 19:57:33)
[Clang 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from enum import IntFlag
>>> class Flag(IntFlag):
...   a = 1
...   b = 2
...   c = 4
...   d = 8
>>> 'foo' in (Flag.a|Flag.b)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: Message signed with OpenPGP
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180404/d41b6810/attachment.sig>