[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Serhiy Storchaka

Serhiy Storchaka  added the comment:

Use <= for this operation.

--

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Mark Dickinson

Mark Dickinson  added the comment:

> This one is common to both python bool and np.bool_.

It's not, though. One of my examples was using `~` (`__invert__`) from the 
proposed ABC). And the behaviour of that function differs between NumPy and 
Python

I think one of the rules about this sort of abstraction should be that there 
should be multiple concrete examples available that you intend the abstraction 
to apply to, *before* you abstract. Otherwise there's a risk of making an 
abstraction that doesn't turn out to fit actual use-cases. Right now, we only 
have Python's bool as a concrete example; NumPy's bool_ is IMO not close enough 
in its behaviour. Are you aware of other bool-like types around that the 
proposed ABC would be helpful for?

--

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Sylvain Marie

Sylvain Marie  added the comment:

@Mark: you are right. That's why the proposed ABC does NOT inherit from 
Integral/Integer and focuses on boolean logic in its simplest form (not, and, 
or, xor). This one is common to both python bool and np.bool_.

@Serhiy: thanks for this interesting suggestion ! However having been in the 
software development world for 12 years, I never met these concepts in practice 
yet. I suspect that therefore I will not be the only one - I'm afraid that 
might make this ABC hard to understand. Besides, I am not sure that numpy bool 
and python bool implement this correctly:

>>> True > False
True
>>> np.bool_(True) > np.bool_(False)
True

This does not seem to comply with the description I found in 
https://en.wikipedia.org/wiki/Material_conditional

For these reasons I would suggest these operations to be part of a separate 
class.

-Sylvain

--

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Serhiy Storchaka

Serhiy Storchaka  added the comment:

If a Boolean ABC includes __and__, __or__ and __xor__ which implement 
conjunction, disjunction and exclusive disjunction, shouldn't it include 
__le__, __gt__, __ge__ and __lt__? They implement other logical operations: 
material implication, material nonimplication, converse implication and 
converse nonimplication.

--
nosy: +serhiy.storchaka

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Mark Dickinson

Mark Dickinson  added the comment:

> - register numpy bool ('np.bool_') as a virtual subclass of 'Boolean' only

Be aware that np.bool_ behaves differently from Python's bool type in a number 
of ways. It may not make sense to have something that tries to abstract over 
both bool and np.bool_.

>>> import numpy as np
>>> np.bool_(True) + np.bool_(True)
True
>>> ~np.bool_(True)
False
>>> True + True
2
>>> ~True
-2

--
nosy: +mark.dickinson

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-21 Thread Sylvain Marie

Sylvain Marie  added the comment:

Thanks !

1. > ok
2. > ok
3. > That's simply 'the latest state' in the discussion. I guess that having 
Boolean in numbers is better than it being in its own module, since it is 
consistent with the other ABCs (and bool is a subclass of both Boolean and 
Integral-er). To quote Guido's answer in the discussion thread

GVR> A thought just occurred to me. Maybe we should just add a Boolean class to 
numbers?

(several positive answers)

SMA > I would rather suggest to keep that Boolean ABC class independent of 
Integral (see proposal in first post) to let it remain 'pure', i.e. represent 
logical booleans only. However nothing prevents us to register python bool as a 
virtual subclass of *both* Integral and Boolean - while np.bool would be 
registered as a virtual subclass of Boolean only. This would reflect quite well 
the reality - the fact that python bool is both a Boolean and an Integer, while 
numpy bool is only a Boolean.

GVR> OK, that could work. At this point I think you should just file an issue 
on bugs.python.org (but since Python 3.7 is in feature freeze, expect this to 
be put on the 3.8 track).

4. > Very good point. I do not know how do handle this at this point. As long 
as bool is a subclass of int (changing this is another discussion), the only 
way to go for now is probably to remove the 'invert' method from this ABC - if 
I am interpreting your answer correctly?


Finally let's note that the Integral > Integer renaming is now handled in a 
separate thread https://bugs.python.org/issue32891

--

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-20 Thread Josh Rosenberg

Josh Rosenberg  added the comment:

First off, link to discussion: 
https://groups.google.com/d/topic/python-ideas/-3QW3cxj3ko/discussion

1. bool is already a virtual subclass of Integral since it's an actual subclass 
of int (which is a virtual subclass of Integral); no need to explicitly 
register it

2. Don't try to register numpy's types for them; doing so would mean simply 
having numpy installed forces it to be imported if you import numbers, even if 
your script never uses numpy. Let numpy add registration for the type itself.

3. If Boolean is not a subclass of Integer/Integral, why is it in the numbers 
module at all? The discussion seemed to suggest putting it in numbers when the 
idea was that Boolean would subclass Integer/Integral; if it's not numeric at 
all, then the numbers module doesn't make sense.

4. Obviously, it's impossible to overload the not behavior (__bool__ is called 
directly to get a true bool, then the result is inverted, there is no special 
method for handling the not keyword), so it looks like the proposal is to make 
__invert__ part of the interface. Except bool itself doesn't behave in a way 
that would make __invert__ make sense as a drop in substitution for not; ~True 
produces -2, ~False produces -1, in neither case does it produce a bool result, 
and the result is always truthy. Changing this is impractical, since it would 
violate the int-like behavior of bool (which has been a historical guarantee).

--
nosy: +josh.r

___
Python tracker 

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



[issue32886] new Boolean ABC in numbers module + Integral>Integer renaming

2018-02-20 Thread Sylvain Marie

New submission from Sylvain Marie :

This issue is created following the discussion [Python-ideas] Boolean ABC 
similar to what's provided in the 'numbers' module.

The following items are suggested:
 - adding a 'Boolean' ABC class to the 'numbers' module
 - register python 'bool' as a virtual subclass of both 'Boolean' and 'Integral'
 - register numpy bool ('np.bool_') as a virtual subclass of 'Boolean' only
 - rename 'Integral' 'Integer' and leave 'Integral' as an alias for backwards 
compatibility

Below is a proposal Boolean class:

-

class Boolean(metaclass=ABCMeta):
"""
An abstract base class for booleans.
"""
__slots__ = ()

@abstractmethod
def __bool__(self):
"""Return a builtin bool instance. Called for bool(self)."""

@abstractmethod
def __and__(self, other):
"""self & other"""

@abstractmethod
def __rand__(self, other):
"""other & self"""

@abstractmethod
def __xor__(self, other):
"""self ^ other"""

@abstractmethod
def __rxor__(self, other):
"""other ^ self"""

@abstractmethod
def __or__(self, other):
"""self | other"""

@abstractmethod
def __ror__(self, other):
"""other | self"""

@abstractmethod
def __invert__(self):
"""~self"""


# register bool and numpy bool_ as virtual subclasses
# so that issubclass(bool, Boolean) = issubclass(np.bool_, Boolean) = True
Boolean.register(bool)

try:
import numpy as np
Boolean.register(np.bool_)
except ImportError:
# silently escape
pass

# bool is also a virtual subclass of Integral. np.bool_ is not.
Integral.register(bool)

--
components: Library (Lib)
messages: 312416
nosy: smarie
priority: normal
severity: normal
status: open
title: new Boolean ABC in numbers module + Integral>Integer renaming
type: enhancement
versions: Python 3.7, Python 3.8

___
Python tracker 

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