[Python-ideas] Boolean ABC similar to what's provided in the 'numbers' module

Guido van Rossum guido at python.org
Thu Feb 15 11:27:57 EST 2018


A thought just occurred to me. Maybe we should just add a Boolean class to
numbers? It's a subclass of Integral, presumably. And normally only
builtins.bool is registered with it. But np.bool can be added at the same
point you register the other np integral types.

On Wed, Feb 14, 2018 at 10:23 AM, Sylvain MARIE <
sylvain.marie at schneider-electric.com> wrote:

> Yes, this is used in combination  dynamic type checking, currently using
> enforce (https://github.com/RussBaz/enforce ) but I know that others
> exist (pytypes in particular)
>
>
>
> As per examples…all utility functions that we write that are receiving a
> number or a boolean in their parameters are now written using the numbers
> and additional Boolean classes:
>
>
>
> ------------- example where Integral is used instead of int
> -----------------
>
>
>
> *from *numbers *import *Integral
>
> *import *pandas *as *pd
>
> *from *enforce *import *runtime_validation, config
> config(dict(mode=*'covariant'*))
>
>
> *# type validation will accept subclasses too *@runtime_validation
> *def *only_keep_events_lasting_at_least(boolean_series: pd.Series,
> min_nb_occurrences: Integral):
>
>
>
> *"""     Filters boolean flags to keep 'true' only when it appears at
> least min_nb_occurrences times in a row     **:param*
> * boolean_series:     **:param*
> * min_nb_occurrences:     **:return*
> *:     """*
>
>           (contents skipped for clarity)
>
>
>
> -------------------------------------
>
>
>
> Similarly when a bool type hint is in the signature we try to replace it
> with a Boolean, so that people can call it with a numpy bool. But maybe
> that’s too much of type checking for the python philosophy ? I’m wondering
> if we’re going too far here…
>
>
>
> Anyway, again, my point is just about consistency: if this is available
> for numbers, why not for simple Booleans?
>
>
>
> Sylvain
>
>
>
> *De :* Guido van Rossum [mailto:gvanrossum at gmail.com]
> *Envoyé :* mercredi 14 février 2018 17:14
> *À :* Sylvain MARIE <sylvain.marie at schneider-electric.com>
> *Cc :* Python-Ideas <python-ideas at python.org>
>
> *Objet :* Re: [Python-ideas] Boolean ABC similar to what's provided in
> the 'numbers' module
>
>
>
> Can you show some sample code that you have written that shows where this
> would be useful?
>
>
>
> Note that using the numbers package actually makes static type checking
> through e.g. mypy difficult. So I presume you are talking about dynamic
> checking?
>
>
>
> --Guido
>
>
>
>
>
> On Feb 14, 2018 12:42 AM, "Sylvain MARIE" <sylvain.marie at schneider-
> electric.com> wrote:
>
> My point is just that today, I use the ‘numbers’ package classes
> (Integral, Real, …) for PEP484 type-hinting, and I find it quite useful in
> term of input type validation (in combination with PEP484-compliant type
> checkers, whether static or dynamic). Adding a Boolean ABC with a similar
> behavior would certainly add consistency to that ‘numbers’ package – only
> for users who already find it useful, of course.
>
>
>
> Note that my use case is not about converting an object to a Boolean, I’m
> just speaking about type validation of a ‘true’ boolean object, for example
> to be received as a function argument for a flag option. This is for
> example for users who want to define strongly-typed APIs for interaction
> with the ‘outside world’, and keep using duck-typing for internals.
>
>
>
> Sylvain
>
>
>
> *De :* Python-ideas [mailto:python-ideas-bounces+sylvain.marie=schneider-
> electric.com at python.org] *De la part de* Chris Barker
> *Envoyé :* mardi 13 février 2018 21:12
> *À :* David Mertz <mertz at gnosis.cx>
> *Cc :* python-ideas <python-ideas at python.org>
> *Objet :* Re: [Python-ideas] Boolean ABC similar to what's provided in
> the 'numbers' module
>
>
>
>
>
>
>
> On Mon, Feb 12, 2018 at 10:07 PM, David Mertz <mertz at gnosis.cx> wrote:
>
> I'm not sure I'm convinced by Sylvain that Boolean needs to be an ABC in
> the standard library; Guido expresses skepticism.  Of course it is possible
> to define it in some other library that actually needs to use
> `isinstance(x, Boolean)` as Sylvain demonstraits in his post.  I'm not sure
> I'm unconvinced either, I can see a certain value to saying a given value
> is "fully round-trippable to bool" (as is np.bool_).
>
>
>
> But is an ABC the way to do it? Personally, I'm skeptical that ABCs are a
> solution to, well, anything (as apposed to duck typing and EAFTP). Take
> Nick's example:
>
>
>
> """
>
> The other comparison that comes to mind would be the distinction
> between "__int__" ("can be coerced to an integer, but may lose
> information in the process") and "__index__" ("can be losslessly
> converted to and from a builtin integer").
>
> """
>
>
>
> I suppose we could have had an Index ABC -- but that seems painful to me.
>
>
>
> so maybe we could use a __true_bool__ special method?
>
>
>
> (and an operator.true_bool() function ???)
>
>
>
> (this all makes me wish that python bools were more pure -- but way to
> late for that!)
>
>
>
> I guess it comes down to whether you want to:
>
>
>
>  - Ask the question: "is this object a boolean?"
>
>
>
> or
>
>
>
>  - Make this object a boolean
>
>
>
> __index__ (and operator.index())  is essentially the later -- you want to
> make an index out of whatever object you have, if you can do so.
>
>
>
> -CHB
>
>
>
>
>
>
>
> --
>
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
>
>
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> ______________________________________________________________________
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>
>
>
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> ______________________________________________________________________
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180215/947cb89b/attachment-0001.html>


More information about the Python-ideas mailing list