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 ethan.furman
Recipients barry, eli.bendersky, ethan.furman, ezio.melotti, martin.panter, python-dev, r.david.murray, rhettinger, serhiy.storchaka, veky, vstinner
Date 2016-09-05.15:25:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473089113.28.0.28377302064.issue23591@psf.upfronthosting.co.za>
In-reply-to
Content
> I am quite aware about what's the intended use, but you can't just
> assume people will know about it.

Fair enough.

> In my view, you must do one of two things:
> 
> 1) (at least in documentation, and preferably in the code by raising Exceptions at class definition time) forbid the use of Flags whose values are not either a) powers of two, or b) bitwise or of some already declared values

The closest I would get to forbidding something would be to forbid non-integers as values -- and I'm not going to do that.  In general, Python doesn't take such steps (one notable exception being sum refusing to work with strings because it was such a common trap).

> -or-
> 
> 2) Specify and implement a robust algorithm for finding the "cover of
> bits" for a given numeric value. If the intended algorithm is really
> "pick the largest member value smaller than given value, subtract and
> repeat until 0 remains", then it should be said so in the
> documentation, and preferably some reasons given for the usage of that
> exact algorithm. ("it was easiest to implement" does not rank very high
> on my list.)

How about "it works for me"?  Seriously, if every bit is named then the exact repr used is irrelevant.  If you don't like the standard method then write your own __repr__ method, because that's what we're talking about -- not the actual value (which isn't going to be affected by the algorithm for finding the "cover of bits", but the display of the names).

> In other words, MyFlags(7) should either be illegal, or I should be
> able to interpret what it will be by reading the documentation. Leaving
> it unspecified is not acceptable IMO.

Thankfully, docs can still change during beta.

> (In case it isn't clear: I'm for option 1. I _don't_ intend to write
> MyFlags ever in Python. But if I happen to write it unintentionally
> (e.g. if I forget to declare 2), I would like Python to tell me I'm
> doing something wrong.)

We could certainly add a decorator that double-checks that, just like we have the `unique` decorator to ensure no duplicates in an Enum.  Any idea what to call it?  complete?  named?  allbits?

> (But if you really want option 2 for some reason, that's ok too. I'm
> just saying I would like it specified, with a rationale if it's not
> too much of a problem.)

(1) isn't going to happen, except possibly as a decorator.  If you would like to contribute code and/or docs for (2) and/or a decorator for (1) I'd be happy to review.
History
Date User Action Args
2016-09-05 15:25:13ethan.furmansetrecipients: + ethan.furman, barry, rhettinger, vstinner, ezio.melotti, r.david.murray, eli.bendersky, python-dev, martin.panter, serhiy.storchaka, veky
2016-09-05 15:25:13ethan.furmansetmessageid: <1473089113.28.0.28377302064.issue23591@psf.upfronthosting.co.za>
2016-09-05 15:25:13ethan.furmanlinkissue23591 messages
2016-09-05 15:25:12ethan.furmancreate