pyrefly-numpy-stubs 1.3.0.dev3__tar.gz → 1.3.0.dev4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pyrefly-numpy-stubs
3
- Version: 1.3.0.dev3
3
+ Version: 1.3.0.dev4
4
4
  Summary: NumPy type stubs with array-shape tracking, maintained by Pyrefly
5
5
  Project-URL: homepage, https://pyrefly.org
6
6
  Project-URL: documentation, https://pyrefly.org/en/docs/
@@ -33,7 +33,7 @@ Classifier: Programming Language :: Python
33
33
  Classifier: Programming Language :: Python :: 3
34
34
  Classifier: Typing :: Stubs Only
35
35
  Requires-Python: >=3.12
36
- Requires-Dist: pyrefly-shape-extensions==1.3.0.dev3
36
+ Requires-Dist: pyrefly-shape-extensions==1.3.0.dev4
37
37
  Description-Content-Type: text/markdown
38
38
 
39
39
  # pyrefly-numpy-stubs
@@ -3,17 +3,20 @@
3
3
  # This source code is licensed under the MIT license found in the
4
4
  # LICENSE file in the root directory of this source tree.
5
5
 
6
+ from collections.abc import Sequence
7
+ from types import EllipsisType
6
8
  from typing import Any, Literal, overload
7
9
 
8
10
  import shape_extensions
9
11
  from numpy._shapes import diag_extent, matmul_shape, reduce_shape
10
- from shape_extensions import broadcast, Flag, Int, IntTuple, IntVar
12
+ from shape_extensions import broadcast, Flag, Index, index_shape, Int, IntTuple, IntVar
11
13
 
12
14
  from . import linalg as linalg, random as random
13
15
 
14
16
  type _Shape = IntTuple
15
17
  type _AnyShape = tuple[Any, ...]
16
18
  type _Axis = int | tuple[int, ...] | None
19
+ type _BasicIndex = int | slice | list[int] | None | EllipsisType
17
20
 
18
21
  class generic: ...
19
22
  class bool_(generic): ...
@@ -23,6 +26,9 @@ class int32(generic): ...
23
26
  class int64(generic): ...
24
27
  class intp(generic): ...
25
28
 
29
+ type _IndexScalar = int | bool_ | int32 | int64 | intp
30
+ type _IndexSequence = Sequence[_IndexScalar] | Sequence[Sequence[_IndexScalar]]
31
+
26
32
  class dtype[Scalar = Any]:
27
33
  @overload
28
34
  def __new__[ScalarT: generic](cls, dtype: type[ScalarT]) -> dtype[ScalarT]: ...
@@ -34,7 +40,7 @@ class dtype[Scalar = Any]:
34
40
  # its body. Annotations inside the class reach the class through this alias.
35
41
  _dtype = dtype
36
42
 
37
- @shape_extensions.shaped_array(shape="Shape")
43
+ @shape_extensions.shaped_array(shape="Shape", builtin_indexing=False)
38
44
  class ndarray[Shape: _Shape = _AnyShape, DType = Any]:
39
45
  shape: Shape
40
46
  dtype: DType
@@ -42,9 +48,7 @@ class ndarray[Shape: _Shape = _AnyShape, DType = Any]:
42
48
  def __len__[N: IntVar](self: ndarray[[N]]) -> Int[N]: ...
43
49
  @overload
44
50
  def __len__[N: IntVar, M: IntVar](self: ndarray[[N, M]]) -> Int[N]: ...
45
- # TODO(stroxler): This overload does not bind: paired integer-array indexing
46
- # infers a gradual shape rather than `[I]`, and float indices are not
47
- # rejected. `test_indexing.py` covers both cases at runtime only.
51
+ @overload
48
52
  def __getitem__[
49
53
  N: IntVar,
50
54
  M: IntVar,
@@ -58,6 +62,21 @@ class ndarray[Shape: _Shape = _AnyShape, DType = Any]:
58
62
  ndarray[[I], _dtype[ColumnIndexScalar]],
59
63
  ],
60
64
  ) -> ndarray[[I], DType]: ...
65
+ @overload
66
+ def __getitem__[I: Index](
67
+ self: ndarray[Shape, DType], key: I
68
+ ) -> ndarray[index_shape(Shape, I), DType]: ...
69
+ # TODO(stroxler): Model general array-valued indices precisely enough to
70
+ # reject non-integer dtypes and incompatible advanced-index shapes.
71
+ @overload
72
+ def __getitem__(
73
+ self: ndarray[Shape, DType],
74
+ key: _BasicIndex
75
+ | _IndexScalar
76
+ | _IndexSequence
77
+ | ndarray
78
+ | tuple[_BasicIndex | _IndexScalar | _IndexSequence | ndarray, ...],
79
+ ) -> ndarray[IntTuple, DType]: ...
61
80
  # Only 2-D transpose is modeled for the NumPy shape-stub MVP.
62
81
  @property
63
82
  def T[N: IntVar, P: IntVar](
@@ -149,44 +168,33 @@ class ndarray[Shape: _Shape = _AnyShape, DType = Any]:
149
168
  def __rpow__[OtherShape: _Shape](
150
169
  self, other: ndarray[OtherShape]
151
170
  ) -> ndarray[broadcast(Shape, OtherShape), DType]: ...
152
- # TODO(stroxler): Share the DSL-backed `np.matmul` rule once operator methods can invoke it.
153
- def __matmul__[N: IntVar, M: IntVar, P: IntVar](
154
- self: ndarray[[N, M], DType],
155
- other: ndarray[[M, P]],
156
- ) -> ndarray[[N, P], DType]: ...
157
- # TODO(stroxler): Share the DSL-backed free-function reduction rules with bound methods.
158
- # These narrow method bridges currently cover the shapes used by the example corpus.
159
- @overload
160
- def mean[N: IntVar, M: IntVar](
161
- self: ndarray[[N, M], DType],
162
- axis: Literal[0],
171
+ def __matmul__[OtherShape: _Shape](
172
+ self, other: ndarray[OtherShape]
173
+ ) -> ndarray[matmul_shape(Shape, OtherShape), DType]: ...
174
+ def mean[Axis: Flag[_Axis], KeepDims: Flag[bool]](
175
+ self,
176
+ axis: Axis = None,
163
177
  *,
164
- keepdims: Literal[False] = False,
165
- ) -> ndarray[[M], DType]: ...
166
- @overload
167
- def mean[N: IntVar](
168
- self: ndarray[[N], DType],
169
- ) -> ndarray[[], DType]: ...
170
- @overload
171
- def sum[N: IntVar, M: IntVar](
172
- self: ndarray[[N, M], DType],
173
- axis: Literal[1],
178
+ keepdims: KeepDims = False,
179
+ ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), DType]: ...
180
+ def sum[Axis: Flag[_Axis], KeepDims: Flag[bool]](
181
+ self,
182
+ axis: Axis = None,
174
183
  *,
175
- keepdims: Literal[True],
176
- ) -> ndarray[[N, 1], DType]: ...
177
- @overload
178
- def sum[N: IntVar, M: IntVar, K: IntVar](
179
- self: ndarray[[N, M, K], DType],
180
- axis: Literal[1],
184
+ keepdims: KeepDims = False,
185
+ ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), DType]: ...
186
+ def min[Axis: Flag[_Axis], KeepDims: Flag[bool]](
187
+ self,
188
+ axis: Axis = None,
181
189
  *,
182
- keepdims: Literal[False] = False,
183
- ) -> ndarray[[N, K], DType]: ...
184
- def max[N: IntVar, M: IntVar](
185
- self: ndarray[[N, M], DType],
186
- axis: Literal[1],
190
+ keepdims: KeepDims = False,
191
+ ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), DType]: ...
192
+ def max[Axis: Flag[_Axis], KeepDims: Flag[bool]](
193
+ self,
194
+ axis: Axis = None,
187
195
  *,
188
- keepdims: Literal[True],
189
- ) -> ndarray[[N, 1], DType]: ...
196
+ keepdims: KeepDims = False,
197
+ ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), DType]: ...
190
198
 
191
199
  def abs[Shape: _Shape](x: ndarray[Shape]) -> ndarray[Shape]: ...
192
200
  def exp[Shape: _Shape](x: ndarray[Shape]) -> ndarray[Shape]: ...
@@ -224,7 +232,7 @@ def fill_diagonal[N: IntVar, DType](
224
232
  @overload
225
233
  def diag[N: IntVar, DType, K: Flag[int] = 0](
226
234
  v: ndarray[[N], DType], k: K = 0
227
- ) -> ndarray[[diag_extent(N, K), diag_extent(N, K)], DType]: ...
235
+ ) -> ndarray[[diag_extent(Int[N], K), diag_extent(Int[N], K)], DType]: ...
228
236
 
229
237
  # TODO(stroxler): Model the shape arithmetic here; we can do better than `int`.
230
238
  @overload
@@ -307,10 +315,8 @@ def argmin[N: IntVar, M: IntVar](
307
315
  keepdims: Literal[False] = False,
308
316
  ) -> ndarray[[N], dtype[intp]]: ...
309
317
 
310
- # NumPy uses the typed DSL for this rule; Torch still uses the legacy shaped-array
311
- # mechanism. This MVP models only two-dimensional inputs. The result dtype stays
312
- # gradual because dtype promotion is not modeled, while `ndarray.__matmul__`
313
- # carries the left operand's dtype.
318
+ # The result dtype stays gradual because dtype promotion is not modeled, while
319
+ # `ndarray.__matmul__` carries the left operand's dtype.
314
320
  def matmul[LeftShape: _Shape, RightShape: _Shape](
315
321
  a: ndarray[LeftShape], b: ndarray[RightShape], /
316
322
  ) -> ndarray[matmul_shape(LeftShape, RightShape), Any]: ...
@@ -4,7 +4,7 @@
4
4
  # LICENSE file in the root directory of this source tree.
5
5
 
6
6
  import shape_extensions.dsl as dsl
7
- from shape_extensions import Int, IntTuple, type_shape_dsl_function
7
+ from shape_extensions import gufunc_broadcast, Int, IntTuple, type_shape_dsl_function
8
8
 
9
9
  @type_shape_dsl_function
10
10
  def int_min(a: Int, b: Int) -> Int:
@@ -25,17 +25,17 @@ def diag_extent(n: Int, k: int) -> Int:
25
25
 
26
26
  @type_shape_dsl_function
27
27
  def matmul_shape(left: IntTuple, right: IntTuple) -> IntTuple:
28
- if len(left) != 2 or len(right) != 2:
29
- return dsl.Invalid("matmul expects 2-D arrays")
30
- left_inner = left[1]
31
- right_inner = right[0]
32
- if (
33
- dsl.is_concrete_int(left_inner)
34
- and dsl.is_concrete_int(right_inner)
35
- and left_inner != right_inner
36
- ):
37
- return dsl.Invalid("matmul inner dimensions must match")
38
- return dsl.IntTuple((left[0], right[1]))
28
+ if len(left) == 0 or len(right) == 0:
29
+ return dsl.Invalid("matmul expects at least 1-D arrays")
30
+ operands = dsl.IntTuples((left, right))
31
+ if len(right) == 1:
32
+ spec = "(n),(n)->()"
33
+ return gufunc_broadcast(spec, operands)
34
+ if len(left) == 1:
35
+ spec = "(n),(n,p)->(p)"
36
+ return gufunc_broadcast(spec, operands)
37
+ spec = "(m,n),(n,p)->(m,p)"
38
+ return gufunc_broadcast(spec, operands)
39
39
 
40
40
  @type_shape_dsl_function
41
41
  def reduce_shape(
@@ -6,7 +6,7 @@
6
6
  from typing import Literal, overload
7
7
 
8
8
  from numpy._shapes import int_min
9
- from shape_extensions import IntVar
9
+ from shape_extensions import Int, IntVar
10
10
 
11
11
  from .. import ndarray
12
12
 
@@ -37,7 +37,7 @@ def svd[M: IntVar, N: IntVar, DType](
37
37
  compute_uv: Literal[True] = True,
38
38
  hermitian: Literal[False] = False,
39
39
  ) -> tuple[
40
- ndarray[[M, int_min(M, N)], DType],
41
- ndarray[[int_min(M, N)], DType],
42
- ndarray[[int_min(M, N), N], DType],
40
+ ndarray[[M, int_min(Int[M], Int[N])], DType],
41
+ ndarray[[int_min(Int[M], Int[N])], DType],
42
+ ndarray[[int_min(Int[M], Int[N]), N], DType],
43
43
  ]: ...
@@ -8,7 +8,7 @@ description = "NumPy type stubs with array-shape tracking, maintained by Pyrefly
8
8
  readme = "README.md"
9
9
  requires-python = ">=3.12"
10
10
  license = { file = "LICENSE" }
11
- version = "1.3.0.dev3"
11
+ version = "1.3.0.dev4"
12
12
  keywords = ["typechecker", "typechecking", "stubs", "numpy"]
13
13
  classifiers = [
14
14
  "Development Status :: 4 - Beta",
@@ -18,7 +18,7 @@ classifiers = [
18
18
  "Typing :: Stubs Only",
19
19
  ]
20
20
  dependencies = [
21
- "pyrefly-shape-extensions==1.3.0.dev3",
21
+ "pyrefly-shape-extensions==1.3.0.dev4",
22
22
  ]
23
23
 
24
24
  [project.urls]