cocotb 1.9.2__cp36-cp36m-win_amd64.whl → 2.0.0b1__cp36-cp36m-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cocotb might be problematic. Click here for more details.

Files changed (151) hide show
  1. cocotb/{ANSI.py → _ANSI.py} +5 -25
  2. cocotb/__init__.py +76 -332
  3. cocotb/_base_triggers.py +513 -0
  4. cocotb/_bridge.py +187 -0
  5. cocotb/_decorators.py +515 -0
  6. cocotb/_deprecation.py +3 -3
  7. cocotb/_exceptions.py +7 -0
  8. cocotb/_extended_awaitables.py +419 -0
  9. cocotb/_gpi_triggers.py +382 -0
  10. cocotb/_init.py +295 -0
  11. cocotb/_outcomes.py +54 -0
  12. cocotb/_profiling.py +46 -0
  13. cocotb/_py_compat.py +100 -29
  14. cocotb/_scheduler.py +454 -0
  15. cocotb/_test.py +245 -0
  16. cocotb/_test_factory.py +309 -0
  17. cocotb/_test_functions.py +42 -0
  18. cocotb/_typing.py +7 -0
  19. cocotb/_utils.py +296 -0
  20. cocotb/_version.py +3 -7
  21. cocotb/_xunit_reporter.py +66 -0
  22. cocotb/clock.py +271 -108
  23. cocotb/handle.py +1342 -795
  24. cocotb/libs/cocotb.dll +0 -0
  25. cocotb/libs/cocotb.exp +0 -0
  26. cocotb/libs/cocotb.lib +0 -0
  27. cocotb/libs/cocotbfli_modelsim.dll +0 -0
  28. cocotb/libs/cocotbfli_modelsim.exp +0 -0
  29. cocotb/libs/cocotbfli_modelsim.lib +0 -0
  30. cocotb/libs/cocotbutils.dll +0 -0
  31. cocotb/libs/cocotbutils.exp +0 -0
  32. cocotb/libs/cocotbutils.lib +0 -0
  33. cocotb/libs/cocotbvhpi_aldec.dll +0 -0
  34. cocotb/libs/cocotbvhpi_aldec.exp +0 -0
  35. cocotb/libs/cocotbvhpi_aldec.lib +0 -0
  36. cocotb/libs/cocotbvhpi_modelsim.dll +0 -0
  37. cocotb/libs/cocotbvhpi_modelsim.exp +0 -0
  38. cocotb/libs/cocotbvhpi_modelsim.lib +0 -0
  39. cocotb/libs/cocotbvpi_aldec.dll +0 -0
  40. cocotb/libs/cocotbvpi_aldec.exp +0 -0
  41. cocotb/libs/cocotbvpi_aldec.lib +0 -0
  42. cocotb/libs/cocotbvpi_ghdl.dll +0 -0
  43. cocotb/libs/cocotbvpi_ghdl.exp +0 -0
  44. cocotb/libs/cocotbvpi_ghdl.lib +0 -0
  45. cocotb/libs/cocotbvpi_icarus.exp +0 -0
  46. cocotb/libs/cocotbvpi_icarus.lib +0 -0
  47. cocotb/libs/cocotbvpi_icarus.vpl +0 -0
  48. cocotb/libs/cocotbvpi_modelsim.dll +0 -0
  49. cocotb/libs/cocotbvpi_modelsim.exp +0 -0
  50. cocotb/libs/cocotbvpi_modelsim.lib +0 -0
  51. cocotb/libs/embed.dll +0 -0
  52. cocotb/libs/embed.exp +0 -0
  53. cocotb/libs/embed.lib +0 -0
  54. cocotb/libs/gpi.dll +0 -0
  55. cocotb/libs/gpi.exp +0 -0
  56. cocotb/libs/gpi.lib +0 -0
  57. cocotb/libs/gpilog.dll +0 -0
  58. cocotb/libs/gpilog.exp +0 -0
  59. cocotb/libs/gpilog.lib +0 -0
  60. cocotb/libs/pygpilog.dll +0 -0
  61. cocotb/libs/pygpilog.exp +0 -0
  62. cocotb/libs/pygpilog.lib +0 -0
  63. cocotb/{log.py → logging.py} +105 -110
  64. cocotb/queue.py +103 -57
  65. cocotb/regression.py +667 -712
  66. cocotb/result.py +17 -188
  67. cocotb/share/def/aldec.exp +0 -0
  68. cocotb/share/def/aldec.lib +0 -0
  69. cocotb/share/def/ghdl.exp +0 -0
  70. cocotb/share/def/ghdl.lib +0 -0
  71. cocotb/share/def/icarus.exp +0 -0
  72. cocotb/share/def/icarus.lib +0 -0
  73. cocotb/share/def/modelsim.def +1 -0
  74. cocotb/share/def/modelsim.exp +0 -0
  75. cocotb/share/def/modelsim.lib +0 -0
  76. cocotb/share/include/cocotb_utils.h +6 -29
  77. cocotb/share/include/embed.h +5 -28
  78. cocotb/share/include/gpi.h +137 -92
  79. cocotb/share/include/gpi_logging.h +221 -142
  80. cocotb/share/include/py_gpi_logging.h +7 -4
  81. cocotb/share/include/vpi_user_ext.h +4 -26
  82. cocotb/share/lib/verilator/verilator.cpp +59 -54
  83. cocotb/simulator.cp36-win_amd64.exp +0 -0
  84. cocotb/simulator.cp36-win_amd64.lib +0 -0
  85. cocotb/simulator.cp36-win_amd64.pyd +0 -0
  86. cocotb/task.py +434 -212
  87. cocotb/triggers.py +55 -1092
  88. cocotb/types/__init__.py +25 -47
  89. cocotb/types/_abstract_array.py +151 -0
  90. cocotb/types/_array.py +264 -0
  91. cocotb/types/_logic.py +296 -0
  92. cocotb/types/_logic_array.py +834 -0
  93. cocotb/types/{range.py → _range.py} +36 -44
  94. cocotb/types/_resolve.py +76 -0
  95. cocotb/utils.py +119 -587
  96. cocotb-2.0.0b1.dist-info/METADATA +48 -0
  97. cocotb-2.0.0b1.dist-info/RECORD +139 -0
  98. cocotb-2.0.0b1.dist-info/entry_points.txt +3 -0
  99. {cocotb-1.9.2.dist-info → cocotb-2.0.0b1.dist-info}/top_level.txt +1 -0
  100. cocotb_tools/_coverage.py +33 -0
  101. cocotb_tools/_vendor/__init__.py +3 -0
  102. cocotb_tools/check_results.py +65 -0
  103. cocotb_tools/combine_results.py +152 -0
  104. cocotb_tools/config.py +241 -0
  105. {cocotb → cocotb_tools}/ipython_support.py +29 -22
  106. cocotb_tools/makefiles/Makefile.deprecations +27 -0
  107. {cocotb/share → cocotb_tools}/makefiles/Makefile.inc +82 -54
  108. {cocotb/share → cocotb_tools}/makefiles/Makefile.sim +8 -33
  109. {cocotb/share → cocotb_tools}/makefiles/simulators/Makefile.activehdl +9 -16
  110. cocotb_tools/makefiles/simulators/Makefile.cvc +61 -0
  111. cocotb_tools/makefiles/simulators/Makefile.dsim +39 -0
  112. {cocotb/share → cocotb_tools}/makefiles/simulators/Makefile.ghdl +13 -42
  113. cocotb_tools/makefiles/simulators/Makefile.icarus +80 -0
  114. cocotb_tools/makefiles/simulators/Makefile.ius +93 -0
  115. cocotb_tools/makefiles/simulators/Makefile.modelsim +9 -0
  116. cocotb_tools/makefiles/simulators/Makefile.nvc +60 -0
  117. cocotb_tools/makefiles/simulators/Makefile.questa +29 -0
  118. cocotb/share/makefiles/simulators/Makefile.questa → cocotb_tools/makefiles/simulators/Makefile.questa-compat +26 -54
  119. cocotb_tools/makefiles/simulators/Makefile.questa-qisqrun +149 -0
  120. {cocotb/share → cocotb_tools}/makefiles/simulators/Makefile.riviera +17 -56
  121. cocotb_tools/makefiles/simulators/Makefile.vcs +65 -0
  122. {cocotb/share → cocotb_tools}/makefiles/simulators/Makefile.verilator +15 -22
  123. {cocotb/share → cocotb_tools}/makefiles/simulators/Makefile.xcelium +20 -52
  124. {cocotb → cocotb_tools}/runner.py +794 -361
  125. cocotb/_sim_versions.py → cocotb_tools/sim_versions.py +16 -21
  126. pygpi/entry.py +34 -17
  127. cocotb/binary.py +0 -858
  128. cocotb/config.py +0 -289
  129. cocotb/decorators.py +0 -332
  130. cocotb/memdebug.py +0 -35
  131. cocotb/outcomes.py +0 -56
  132. cocotb/scheduler.py +0 -1099
  133. cocotb/share/makefiles/Makefile.deprecations +0 -12
  134. cocotb/share/makefiles/simulators/Makefile.cvc +0 -94
  135. cocotb/share/makefiles/simulators/Makefile.icarus +0 -111
  136. cocotb/share/makefiles/simulators/Makefile.ius +0 -125
  137. cocotb/share/makefiles/simulators/Makefile.modelsim +0 -32
  138. cocotb/share/makefiles/simulators/Makefile.nvc +0 -64
  139. cocotb/share/makefiles/simulators/Makefile.vcs +0 -98
  140. cocotb/types/array.py +0 -309
  141. cocotb/types/logic.py +0 -292
  142. cocotb/types/logic_array.py +0 -298
  143. cocotb/wavedrom.py +0 -199
  144. cocotb/xunit_reporter.py +0 -80
  145. cocotb-1.9.2.dist-info/METADATA +0 -170
  146. cocotb-1.9.2.dist-info/RECORD +0 -121
  147. cocotb-1.9.2.dist-info/entry_points.txt +0 -3
  148. {cocotb-1.9.2.dist-info → cocotb-2.0.0b1.dist-info}/LICENSE +0 -0
  149. {cocotb-1.9.2.dist-info → cocotb-2.0.0b1.dist-info}/WHEEL +0 -0
  150. {cocotb/_vendor → cocotb_tools}/__init__.py +0 -0
  151. {cocotb → cocotb_tools}/_vendor/distutils_version.py +0 -0
cocotb/types/__init__.py CHANGED
@@ -1,50 +1,28 @@
1
1
  # Copyright cocotb contributors
2
2
  # Licensed under the Revised BSD License, see LICENSE for details.
3
3
  # SPDX-License-Identifier: BSD-3-Clause
4
- import typing
5
-
6
- from .array import Array # noqa: F401
7
- from .logic import Bit, Logic # noqa: F401
8
- from .logic_array import LogicArray # noqa: F401
9
- from .range import Range # noqa: F401
10
-
11
-
12
- def concat(a: typing.Any, b: typing.Any) -> typing.Any:
13
- """
14
- Create a new array that is the concatenation of one array with another.
15
-
16
- Uses the :meth:`__concat__` or :meth:`__rconcat__` special methods to dispatch to a particular implementation,
17
- exactly like other binary operations in Python.
18
-
19
- Raises:
20
- TypeError: when the arguments do not support concatenation in the given order.
21
- """
22
- MISSING = object()
23
- type_a = type(a)
24
- type_b = type(b)
25
- a_concat = getattr(type_a, "__concat__", MISSING)
26
- a_rconcat = getattr(type_a, "__rconcat__", MISSING)
27
- b_rconcat = getattr(type_b, "__rconcat__", MISSING)
28
-
29
- if type_a is not type_b and issubclass(type_b, type_a) and a_rconcat != b_rconcat:
30
- # 'b' is a subclass of 'a' with a more specific implementation of 'concat(a, b)'
31
- call_order = [(b, b_rconcat, a), (a, a_concat, b)]
32
- elif type_a is not type_b:
33
- # normal call order
34
- call_order = [(a, a_concat, b), (b, b_rconcat, a)]
35
- else:
36
- # types are the same, we expect implementation of 'concat(a, b)' to be in 'a.__concat__'
37
- call_order = [(a, a_concat, b)]
38
-
39
- for lhs, method, rhs in call_order:
40
- if method is MISSING:
41
- continue
42
- res = method(lhs, rhs)
43
- if res is not NotImplemented:
44
- return res
45
-
46
- raise TypeError(
47
- "cannot concatenate {!r} with {!r}".format(
48
- type_a.__qualname__, type_b.__qualname__
49
- )
50
- )
4
+ from ._abstract_array import AbstractArray, AbstractMutableArray
5
+ from ._array import Array
6
+ from ._logic import Logic
7
+ from ._logic_array import LogicArray
8
+ from ._range import Range
9
+
10
+ # isort: split
11
+ # These are imports for doctests in the submodules. Since we fix up the `__module__`
12
+ # attribute, `--doctest-modules` thinks this is the module the types were defined in
13
+ # and will evaluate this module first before running tests.
14
+ from typing import Tuple # noqa: F401
15
+
16
+ __all__ = (
17
+ "AbstractArray",
18
+ "AbstractMutableArray",
19
+ "Array",
20
+ "Logic",
21
+ "LogicArray",
22
+ "Range",
23
+ )
24
+
25
+ # Set __module__ on re-exports
26
+ for name in __all__:
27
+ obj = globals()[name]
28
+ obj.__module__ = __name__
@@ -0,0 +1,151 @@
1
+ # Copyright cocotb contributors
2
+ # Licensed under the Revised BSD License, see LICENSE for details.
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ from abc import abstractmethod
5
+ from typing import Generic, Iterable, Iterator, Optional, TypeVar, Union, overload
6
+
7
+ from cocotb.types._range import Range
8
+
9
+ T_co = TypeVar("T_co", covariant=True)
10
+ T = TypeVar("T")
11
+
12
+
13
+ class AbstractArray(Generic[T_co]):
14
+ r"""Abstract base class for non-mutating Array-like collections.
15
+
16
+ Arrays are similar to :class:`~collections.abc.Sequence`\ s,
17
+ but their size cannot change after creation and they support arbitrary indexing schemes.
18
+
19
+ Abstract methods
20
+ ^^^^^^^^^^^^^^^^
21
+
22
+ * :attr:`range`
23
+ * :meth:`!__getitem__`
24
+
25
+ Mixin methods
26
+ ^^^^^^^^^^^^^
27
+
28
+ * :attr:`left`, :attr:`right`, and :attr:`direction`
29
+ * :meth:`!__len__`
30
+ * :meth:`!__iter__` and :meth:`!__reversed__`
31
+ * :meth:`!__contains__`
32
+ * :meth:`index` and :meth:`count`
33
+ """
34
+
35
+ @property
36
+ def left(self) -> int:
37
+ """Leftmost index of the array."""
38
+ return self.range.left
39
+
40
+ @property
41
+ def direction(self) -> str:
42
+ """``"to"`` if indexes are ascending, ``"downto"`` otherwise."""
43
+ return self.range.direction
44
+
45
+ @property
46
+ def right(self) -> int:
47
+ """Rightmost index of the array."""
48
+ return self.range.right
49
+
50
+ @property
51
+ @abstractmethod
52
+ def range(self) -> Range:
53
+ """:class:`Range` of the indexes of the array."""
54
+
55
+ @range.setter
56
+ @abstractmethod
57
+ def range(self, new_range: Range) -> None:
58
+ """Set a new indexing scheme on the array.
59
+
60
+ Must be the same size.
61
+ """
62
+
63
+ def __len__(self) -> int:
64
+ return len(self.range)
65
+
66
+ def __iter__(self) -> Iterator[T_co]:
67
+ for i in self.range:
68
+ yield self[i]
69
+
70
+ def __reversed__(self) -> Iterator[T_co]:
71
+ for i in reversed(self.range):
72
+ yield self[i]
73
+
74
+ def __contains__(self, item: object) -> bool:
75
+ return any(v == item for v in self)
76
+
77
+ @overload
78
+ def __getitem__(self, item: int) -> T_co: ...
79
+
80
+ @overload
81
+ def __getitem__(self, item: slice) -> "AbstractArray[T_co]": ...
82
+
83
+ @abstractmethod
84
+ def __getitem__(
85
+ self, item: Union[int, slice]
86
+ ) -> Union[T_co, "AbstractArray[T_co]"]: ...
87
+
88
+ def index(
89
+ self,
90
+ value: object,
91
+ start: Optional[int] = None,
92
+ stop: Optional[int] = None,
93
+ ) -> int:
94
+ """Find first occurrence of value.
95
+
96
+ Args:
97
+ value: Value to search for.
98
+ start: Index to start search at.
99
+ stop: Index to stop search at.
100
+
101
+ Returns:
102
+ Index of first occurrence of *value*.
103
+
104
+ Raises:
105
+ ValueError: If the value is not present.
106
+ """
107
+ if start is None:
108
+ start = self.left
109
+ if stop is None:
110
+ stop = self.right
111
+ for i in Range(start, self.direction, stop):
112
+ if self[i] == value:
113
+ return i
114
+ raise IndexError(f"{value!r} not in array")
115
+
116
+ def count(self, value: object) -> int:
117
+ """Return number of occurrences of value.
118
+
119
+ Args:
120
+ value: Value to search for.
121
+
122
+ Returns:
123
+ Number of occurrences of *value*.
124
+ """
125
+ count: int = 0
126
+ for v in self:
127
+ if v == value:
128
+ count += 1
129
+ return count
130
+
131
+
132
+ class AbstractMutableArray(AbstractArray[T]):
133
+ """Abstract base class for mutating Array-like collections.
134
+
135
+ See :class:`.AbstractArray` for more details.
136
+
137
+ Additional abstract methods:
138
+
139
+ * :meth:`!__setitem__`
140
+ """
141
+
142
+ @overload
143
+ def __setitem__(self, item: int, value: T) -> None: ...
144
+
145
+ @overload
146
+ def __setitem__(self, item: slice, value: Iterable[T]) -> None: ...
147
+
148
+ @abstractmethod
149
+ def __setitem__(
150
+ self, item: Union[int, slice], value: Union[T, Iterable[T]]
151
+ ) -> None: ...
cocotb/types/_array.py ADDED
@@ -0,0 +1,264 @@
1
+ # Copyright cocotb contributors
2
+ # Licensed under the Revised BSD License, see LICENSE for details.
3
+ # SPDX-License-Identifier: BSD-3-Clause
4
+ from typing import Iterable, Iterator, List, TypeVar, Union, cast, overload
5
+
6
+ from cocotb.types._abstract_array import AbstractMutableArray
7
+ from cocotb.types._range import Range
8
+
9
+ T = TypeVar("T")
10
+
11
+
12
+ class Array(AbstractMutableArray[T]):
13
+ r"""Fixed-size, arbitrarily-indexed, homogeneous collection type.
14
+
15
+ Arrays are similar to, but different from Python :class:`list`\ s.
16
+ An array can store values of any type or values of multiple types at a time, just like a :class:`!list`.
17
+ Array constructor values can be any Iterable.
18
+
19
+ .. code-block:: pycon3
20
+
21
+ >>> Array([1, False, "example", None])
22
+ Array([1, False, 'example', None], Range(0, 'to', 3))
23
+
24
+ >>> Array("Hello!")
25
+ Array(['H', 'e', 'l', 'l', 'o', '!'], Range(0, 'to', 5))
26
+
27
+ Unlike :class:`!list`\ s, an array's size cannot change.
28
+ This means they do not support methods like :meth:`~list.append` or :meth:`~list.insert`.
29
+
30
+ The indexes of an Array can start or end at any integer value, they are not limited to 0-based indexing.
31
+ Indexing schemes are selected using :class:`Range` objects.
32
+ If *range* is not given, the range ``Range(0, "to", len(value)-1)`` is used.
33
+ If an :class:`int` is passed for *range*, the range ``Range(0, "to", range-1)`` is used.
34
+
35
+ .. code-block:: pycon3
36
+
37
+ >>> a = Array([0, 1, 2, 3], Range(-1, "downto", -4))
38
+ >>> a[-1]
39
+ 0
40
+ >>> a[-4]
41
+ 3
42
+
43
+ Arrays can also have 0 length using "null" :class:`Range`\ s.
44
+
45
+ .. code-block:: pycon3
46
+
47
+ >>> Array([], Range(1, "to", 0)) # 1 to 0 is a null Range
48
+ Array([], Range(1, 'to', 0))
49
+
50
+ Indexing and slicing is very similar to :class:`!list`\ s, but it uses the indexing scheme specified with the *range*.
51
+ Unlike :class:`!list`, slicing uses an inclusive right bound, which is common in HDLs.
52
+ But like :class:`!list`, if a start or stop index is not specified in the slice, it is inferred as the start or end of the array.
53
+ Slicing an Array returns a new :class:`~cocotb.types.Array` object, whose bounds are the slice indexes.
54
+
55
+ .. code-block:: pycon3
56
+
57
+ >>> a = Array("1234abcd")
58
+ >>> a[7]
59
+ 'd'
60
+ >>> a[2:5]
61
+ Array(['3', '4', 'a', 'b'], Range(2, 'to', 5))
62
+ >>> a[2:5] = reversed(a[2:5])
63
+ >>> "".join(a)
64
+ '12ba43cd'
65
+
66
+ >>> b = Array("1234", Range(0, -3))
67
+ >>> b[-2]
68
+ '3'
69
+ >>> b[-1:]
70
+ Array(['2', '3', '4'], Range(-1, 'downto', -3))
71
+ >>> b[:] = reversed(b)
72
+ >>> b
73
+ Array(['4', '3', '2', '1'], Range(0, 'downto', -3))
74
+
75
+ .. warning::
76
+ Arrays behave differently in certain situations than Python's built-in sequence types (:class:`list`, :class:`tuple`, etc.).
77
+
78
+ - Arrays are not necessarily 0-based and slices use inclusive right bounds,
79
+ so many functions that work on Python sequences by index (like :mod:`bisect`) may not work on arrays.
80
+ - Slice indexes must be specified in the same direction as the array and do not support specifying a "step".
81
+ - When setting a slice, the new value must be an iterable of the same size as the slice.
82
+ - Negative indexes are *not* treated as an offset from the end of the array, but are treated literally.
83
+
84
+ Arrays are equal to other arrays of the same length with the same values (structural equality).
85
+ Bounds do not matter for equality.
86
+
87
+ .. code-block:: pycon3
88
+
89
+ >>> a = Array([1, 1, 2, 3, 5], Range(4, "downto", 0))
90
+ >>> b = Array([1, 1, 2, 3, 5], Range(-2, "to", 2))
91
+ >>> a == b
92
+ True
93
+
94
+ You can change the bounds of an array by setting the :attr:`range` to a new value.
95
+ The new bounds must be the same length of the array.
96
+
97
+ .. code-block:: pycon3
98
+
99
+ >>> a = Array("1234")
100
+ >>> a.range
101
+ Range(0, 'to', 3)
102
+ >>> a.range = Range(3, "downto", 0)
103
+ >>> a.range
104
+ Range(3, 'downto', 0)
105
+
106
+ Arrays support many of the methods and semantics defined by :class:`collections.abc.Sequence`.
107
+
108
+ .. code-block:: pycon3
109
+
110
+ >>> a = Array("stuff", Range(2, "downto", -2))
111
+ >>> len(a)
112
+ 5
113
+ >>> "t" in a
114
+ True
115
+ >>> a.index("u")
116
+ 0
117
+ >>> for c in a:
118
+ ... print(c)
119
+ s
120
+ t
121
+ u
122
+ f
123
+ f
124
+
125
+ Args:
126
+ value: Initial value for the Array.
127
+ range: The indexing scheme of the Array.
128
+
129
+ Raises:
130
+ ValueError: When argument values cannot be used to construct an Array.
131
+ TypeError: When invalid argument types are used.
132
+ """
133
+
134
+ def __init__(
135
+ self, value: Iterable[T], range: Union[Range, int, None] = None
136
+ ) -> None:
137
+ self._value = list(value)
138
+ if range is None:
139
+ self._range = Range(0, "to", len(self._value) - 1)
140
+ else:
141
+ if isinstance(range, int):
142
+ self._range = Range(0, "to", range - 1)
143
+ elif isinstance(range, Range):
144
+ self._range = range
145
+ else:
146
+ raise TypeError(
147
+ f"Expected Range or int for parameter 'range', not {type(range).__qualname__}"
148
+ )
149
+
150
+ if len(self._value) != len(self._range):
151
+ raise ValueError(
152
+ f"Value of length {len(self._value)!r} does not fit in {self._range!r}"
153
+ )
154
+
155
+ @classmethod
156
+ def _from_handle(cls, value: List[T], range: Range) -> "Array[T]":
157
+ self = cls.__new__(cls)
158
+ self._value = value
159
+ self._range = range
160
+ return self
161
+
162
+ @property
163
+ def range(self) -> Range:
164
+ """:class:`Range` of the indexes of the array."""
165
+ return self._range
166
+
167
+ @range.setter
168
+ def range(self, new_range: Range) -> None:
169
+ """Sets a new indexing scheme on the array, must be the same size"""
170
+ if not isinstance(new_range, Range):
171
+ raise TypeError("range argument must be of type 'Range'")
172
+ if len(new_range) != len(self):
173
+ raise ValueError(
174
+ f"{new_range!r} not the same length as old range ({self._range!r})."
175
+ )
176
+ self._range = new_range
177
+
178
+ def __iter__(self) -> Iterator[T]:
179
+ return iter(self._value)
180
+
181
+ def __reversed__(self) -> Iterator[T]:
182
+ return reversed(self._value)
183
+
184
+ def __contains__(self, item: object) -> bool:
185
+ return item in self._value
186
+
187
+ def __eq__(self, other: object) -> bool:
188
+ if isinstance(other, Array):
189
+ return self._value == other._value
190
+ elif isinstance(other, list):
191
+ return self._value == other
192
+ elif isinstance(other, tuple):
193
+ return tuple(self._value) == other
194
+ else:
195
+ return NotImplemented
196
+
197
+ @overload
198
+ def __getitem__(self, item: int) -> T: ...
199
+
200
+ @overload
201
+ def __getitem__(self, item: slice) -> "Array[T]": ...
202
+
203
+ def __getitem__(self, item: Union[int, slice]) -> Union[T, "Array[T]"]:
204
+ if isinstance(item, int):
205
+ idx = self._translate_index(item)
206
+ return self._value[idx]
207
+ elif isinstance(item, slice):
208
+ start = item.start if item.start is not None else self.left
209
+ stop = item.stop if item.stop is not None else self.right
210
+ if item.step is not None:
211
+ raise IndexError("do not specify step")
212
+ start_i = self._translate_index(start)
213
+ stop_i = self._translate_index(stop)
214
+ if start_i > stop_i:
215
+ raise IndexError(
216
+ f"slice [{start}:{stop}] direction does not match array direction [{self.left}:{self.right}]"
217
+ )
218
+ value = self._value[start_i : stop_i + 1]
219
+ range = Range(start, self.direction, stop)
220
+ return Array(value=value, range=range)
221
+ raise TypeError(f"indexes must be ints or slices, not {type(item).__name__}")
222
+
223
+ @overload
224
+ def __setitem__(self, item: int, value: T) -> None: ...
225
+
226
+ @overload
227
+ def __setitem__(self, item: slice, value: Iterable[T]) -> None: ...
228
+
229
+ def __setitem__(
230
+ self, item: Union[int, slice], value: Union[T, Iterable[T]]
231
+ ) -> None:
232
+ if isinstance(item, int):
233
+ idx = self._translate_index(item)
234
+ self._value[idx] = cast("T", value)
235
+ elif isinstance(item, slice):
236
+ start = item.start if item.start is not None else self.left
237
+ stop = item.stop if item.stop is not None else self.right
238
+ if item.step is not None:
239
+ raise IndexError("do not specify step")
240
+ start_i = self._translate_index(start)
241
+ stop_i = self._translate_index(stop)
242
+ if start_i > stop_i:
243
+ raise IndexError(
244
+ f"slice [{start}:{stop}] direction does not match array direction [{self.left}:{self.right}]"
245
+ )
246
+ value = list(cast("Iterable[T]", value))
247
+ if len(value) != (stop_i - start_i + 1):
248
+ raise ValueError(
249
+ f"value of length {len(value)!r} will not fit in slice [{start}:{stop}]"
250
+ )
251
+ self._value[start_i : stop_i + 1] = value
252
+ else:
253
+ raise TypeError(
254
+ f"indexes must be ints or slices, not {type(item).__name__}"
255
+ )
256
+
257
+ def __repr__(self) -> str:
258
+ return f"{type(self).__name__}({self._value!r}, {self._range!r})"
259
+
260
+ def _translate_index(self, item: int) -> int:
261
+ try:
262
+ return self._range.index(item)
263
+ except ValueError:
264
+ raise IndexError(f"index {item} out of range") from None