Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
204 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
############################################################################## | ||
# Copyright (c) 2020 Zope Foundation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
############################################################################## | ||
""" | ||
Interface definitions paralleling the abstract base classes defined in | ||
:mod:`numbers`. | ||
After this module is imported, the standard library types will declare | ||
that they implement the appropriate interface. | ||
.. versionadded:: 5.0.0 | ||
""" | ||
from __future__ import absolute_import | ||
|
||
import numbers as abc | ||
|
||
from zope.interface.common import ABCInterface | ||
from zope.interface.common import optional | ||
|
||
from zope.interface._compat import PYTHON2 as PY2 | ||
|
||
# pylint:disable=inherit-non-class, | ||
# pylint:disable=no-self-argument,no-method-argument | ||
# pylint:disable=unexpected-special-method-signature | ||
# pylint:disable=no-value-for-parameter | ||
|
||
|
||
class INumber(ABCInterface): | ||
abc = abc.Number | ||
|
||
|
||
class IComplex(INumber): | ||
abc = abc.Complex | ||
|
||
@optional | ||
def __complex__(): | ||
""" | ||
Rarely implemented, even in builtin types. | ||
""" | ||
if PY2: | ||
@optional | ||
def __eq__(other): | ||
""" | ||
The interpreter may supply one through complicated rules. | ||
""" | ||
|
||
__ne__ = __eq__ | ||
|
||
class IReal(IComplex): | ||
abc = abc.Real | ||
|
||
@optional | ||
def __complex__(): | ||
""" | ||
Rarely implemented, even in builtin types. | ||
""" | ||
|
||
__floor__ = __ceil__ = __complex__ | ||
|
||
if PY2: | ||
@optional | ||
def __le__(other): | ||
""" | ||
The interpreter may supply one through complicated rules. | ||
""" | ||
|
||
__lt__ = __le__ | ||
|
||
|
||
class IRational(IReal): | ||
abc = abc.Rational | ||
|
||
|
||
class IIntegral(IRational): | ||
abc = abc.Integral |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,58 @@ | ||
############################################################################## | ||
# Copyright (c) 2020 Zope Foundation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This file is necessary to make this directory a package. | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
############################################################################## | ||
|
||
|
||
from zope.interface.common import ABCInterface | ||
from zope.interface.common import ABCInterfaceClass | ||
|
||
|
||
def iter_abc_interfaces(predicate=lambda iface: True): | ||
# Iterate ``(iface, classes)``, where ``iface`` is a descendent of | ||
# the ABCInterfaceClass passing the *predicate* and ``classes`` is | ||
# an iterable of classes registered to conform to that interface. | ||
# | ||
# Note that some builtin classes are registered for two distinct | ||
# parts of the ABC/interface tree. For example, bytearray is both ByteString | ||
# and MutableSequence. | ||
seen = set() | ||
stack = list(ABCInterface.dependents) # subclasses, but also implementedBy objects | ||
while stack: | ||
iface = stack.pop(0) | ||
if iface in seen or not isinstance(iface, ABCInterfaceClass): | ||
continue | ||
seen.add(iface) | ||
stack.extend(list(iface.dependents)) | ||
if not predicate(iface): | ||
continue | ||
|
||
registered = list(iface.getRegisteredConformers()) | ||
if registered: | ||
yield iface, registered | ||
|
||
|
||
def add_abc_interface_tests(cls, module): | ||
def predicate(iface): | ||
return iface.__module__ == module | ||
|
||
for iface, registered_classes in iter_abc_interfaces(predicate): | ||
for stdlib_class in registered_classes: | ||
|
||
def test(self, stdlib_class=stdlib_class, iface=iface): | ||
if stdlib_class in self.UNVERIFIABLE or stdlib_class.__name__ in self.UNVERIFIABLE: | ||
self.skipTest("Unable to verify %s" % stdlib_class) | ||
|
||
self.assertTrue(self.verify(iface, stdlib_class)) | ||
|
||
name = 'test_auto_' + stdlib_class.__name__ + '_' + iface.__name__ | ||
test.__name__ = name | ||
assert not hasattr(cls, name) | ||
setattr(cls, name, test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
############################################################################## | ||
# Copyright (c) 2020 Zope Foundation and Contributors. | ||
# All Rights Reserved. | ||
# | ||
# This software is subject to the provisions of the Zope Public License, | ||
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. | ||
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED | ||
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS | ||
# FOR A PARTICULAR PURPOSE. | ||
############################################################################## | ||
|
||
|
||
import unittest | ||
import numbers as abc | ||
|
||
from zope.interface.verify import verifyClass | ||
from zope.interface.verify import verifyObject | ||
|
||
# Note that importing z.i.c.numbers does work on import. | ||
from zope.interface.common import numbers | ||
|
||
from . import add_abc_interface_tests | ||
|
||
|
||
class TestVerifyClass(unittest.TestCase): | ||
verifier = staticmethod(verifyClass) | ||
UNVERIFIABLE = () | ||
|
||
def _adjust_object_before_verify(self, iface, x): | ||
return x | ||
|
||
def verify(self, iface, klass, **kwargs): | ||
return self.verifier(iface, | ||
self._adjust_object_before_verify(iface, klass), | ||
**kwargs) | ||
|
||
def test_int(self): | ||
self.assertIsInstance(int(), abc.Integral) | ||
self.assertTrue(self.verify(numbers.IIntegral, int)) | ||
|
||
def test_float(self): | ||
self.assertIsInstance(float(), abc.Real) | ||
self.assertTrue(self.verify(numbers.IReal, float)) | ||
|
||
add_abc_interface_tests(TestVerifyClass, numbers.INumber.__module__) | ||
|
||
|
||
class TestVerifyObject(TestVerifyClass): | ||
verifier = staticmethod(verifyObject) | ||
|
||
def _adjust_object_before_verify(self, iface, x): | ||
return x() |