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 smarie
Recipients smarie
Date 2018-02-20.13:32:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1519133521.3.0.467229070634.issue32886@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2018-02-20 13:32:01smariesetrecipients: + smarie
2018-02-20 13:32:01smariesetmessageid: <1519133521.3.0.467229070634.issue32886@psf.upfronthosting.co.za>
2018-02-20 13:32:01smarielinkissue32886 messages
2018-02-20 13:32:01smariecreate