builtins.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 zope.interface import classImplements
  19. from zope.interface._compat import PY313_OR_OLDER
  20. from zope.interface.common import collections
  21. from zope.interface.common import io
  22. from zope.interface.common import numbers
  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. This is :class:`str`
  53. """
  54. extra_classes = (str,)
  55. if PY313_OR_OLDER:
  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(ITextString):
  66. """
  67. Interface for native strings.
  68. On all Python versions, this is :class:`str`. Tt extends
  69. :class:`ITextString`.
  70. """
  71. # We're not extending ABCInterface so extra_classes won't work
  72. classImplements(str, INativeString)
  73. class IBool(numbers.IIntegral):
  74. """
  75. Interface for :class:`bool`
  76. """
  77. extra_classes = (bool,)
  78. class IDict(collections.IMutableMapping):
  79. """
  80. Interface for :class:`dict`
  81. """
  82. extra_classes = (dict,)
  83. class IFile(io.IIOBase):
  84. """
  85. Interface for :class:`file`.
  86. It is recommended to use the interfaces from
  87. :mod:`zope.interface.common.io` instead of this interface.
  88. On Python 3, there is no single implementation of this interface;
  89. depending on the arguments, the :func:`open` builtin can return
  90. many different classes that implement different interfaces from
  91. :mod:`zope.interface.common.io`.
  92. """
  93. extra_classes = ()