builtins.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. ##############################################################################
  2. # Copyright (c) 2020 Zope Foundation and Contributors.
  3. # All Rights Reserved.
  4. #
  5. # This software is subject to the provisions of the Zope Public License,
  6. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  7. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  8. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  9. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  10. # FOR A PARTICULAR PURPOSE.
  11. ##############################################################################
  12. """
  13. Interface definitions for builtin types.
  14. After this module is imported, the standard library types will declare
  15. that they implement the appropriate interface.
  16. .. versionadded:: 5.0.0
  17. """
  18. from __future__ import absolute_import
  19. from zope.interface import classImplements
  20. from zope.interface.common import collections
  21. from zope.interface.common import numbers
  22. from zope.interface.common import io
  23. __all__ = [
  24. 'IList',
  25. 'ITuple',
  26. 'ITextString',
  27. 'IByteString',
  28. 'INativeString',
  29. 'IBool',
  30. 'IDict',
  31. 'IFile',
  32. ]
  33. # pylint:disable=no-self-argument
  34. class IList(collections.IMutableSequence):
  35. """
  36. Interface for :class:`list`
  37. """
  38. extra_classes = (list,)
  39. def sort(key=None, reverse=False):
  40. """
  41. Sort the list in place and return None.
  42. *key* and *reverse* must be passed by name only.
  43. """
  44. class ITuple(collections.ISequence):
  45. """
  46. Interface for :class:`tuple`
  47. """
  48. extra_classes = (tuple,)
  49. class ITextString(collections.ISequence):
  50. """
  51. Interface for text (unicode) strings.
  52. On Python 2, this is :class:`unicode`. On Python 3,
  53. this is :class:`str`
  54. """
  55. extra_classes = (type(u'unicode'),)
  56. class IByteString(collections.IByteString):
  57. """
  58. Interface for immutable byte strings.
  59. On all Python versions this is :class:`bytes`.
  60. Unlike :class:`zope.interface.common.collections.IByteString`
  61. (the parent of this interface) this does *not* include
  62. :class:`bytearray`.
  63. """
  64. extra_classes = (bytes,)
  65. class INativeString(IByteString if str is bytes else ITextString):
  66. """
  67. Interface for native strings.
  68. On all Python versions, this is :class:`str`. On Python 2,
  69. this extends :class:`IByteString`, while on Python 3 it extends
  70. :class:`ITextString`.
  71. """
  72. # We're not extending ABCInterface so extra_classes won't work
  73. classImplements(str, INativeString)
  74. class IBool(numbers.IIntegral):
  75. """
  76. Interface for :class:`bool`
  77. """
  78. extra_classes = (bool,)
  79. class IDict(collections.IMutableMapping):
  80. """
  81. Interface for :class:`dict`
  82. """
  83. extra_classes = (dict,)
  84. class IFile(io.IIOBase):
  85. """
  86. Interface for :class:`file`.
  87. It is recommended to use the interfaces from :mod:`zope.interface.common.io`
  88. instead of this interface.
  89. On Python 3, there is no single implementation of this interface;
  90. depending on the arguments, the :func:`open` builtin can return
  91. many different classes that implement different interfaces from
  92. :mod:`zope.interface.common.io`.
  93. """
  94. try:
  95. extra_classes = (file,)
  96. except NameError:
  97. extra_classes = ()