pyrefly-numpy-stubs 1.3.2__tar.gz → 1.4.0.dev2__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.2
3
+ Version: 1.4.0.dev2
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.2
36
+ Requires-Dist: pyrefly-shape-extensions==1.4.0.dev2
37
37
  Description-Content-Type: text/markdown
38
38
 
39
39
  # pyrefly-numpy-stubs
@@ -4,7 +4,7 @@
4
4
  # LICENSE file in the root directory of this source tree.
5
5
 
6
6
  from builtins import bool as py_bool
7
- from collections.abc import Sequence
7
+ from collections.abc import Iterator, Sequence
8
8
  from types import EllipsisType
9
9
  from typing import Any, Final, Literal, overload, SupportsIndex
10
10
 
@@ -183,6 +183,7 @@ from numpy._core.shape_base import (
183
183
  from numpy._pytesttester import PytestTester
184
184
  from numpy._shapes import (
185
185
  diag_extent,
186
+ expand_dims_shape,
186
187
  matmul_shape,
187
188
  matvec_shape,
188
189
  reduce_shape,
@@ -376,6 +377,7 @@ from numpy.matrixlib import (
376
377
  )
377
378
  from shape_extensions import (
378
379
  broadcast as _broadcast_shape,
380
+ Elements,
379
381
  Flag,
380
382
  Index,
381
383
  index_shape,
@@ -384,6 +386,7 @@ from shape_extensions import (
384
386
  IntTuples,
385
387
  IntVar,
386
388
  MapIntTuples,
389
+ RegularNestedList,
387
390
  )
388
391
 
389
392
  # Preserve NumPy's canonical re-exports before local shape-aware declarations.
@@ -408,9 +411,12 @@ from . import (
408
411
 
409
412
  type _Shape = IntTuple
410
413
  type _Axis = int | tuple[int, ...] | None
414
+ type _SingleAxis = int | None
411
415
  type _BasicIndex = int | slice | list[int] | None | EllipsisType
412
416
 
413
- class generic: ...
417
+ class generic:
418
+ def astype(self, dtype: Any, **kwargs: Any) -> Any: ...
419
+
414
420
  class bool_(generic): ...
415
421
  class float32(generic): ...
416
422
  class float64(generic): ...
@@ -420,6 +426,14 @@ class intp(generic): ...
420
426
 
421
427
  type _IndexScalar = int | bool_ | int32 | int64 | intp
422
428
  type _IndexSequence = Sequence[_IndexScalar] | Sequence[Sequence[_IndexScalar]]
429
+ type _ArrayIndex = (
430
+ _BasicIndex
431
+ | _IndexScalar
432
+ | _IndexSequence
433
+ | ndarray
434
+ | tuple[_BasicIndex | _IndexScalar | _IndexSequence | ndarray, ...]
435
+ )
436
+ type _ArrayScalar = None | bool | int | float | complex | str | bytes | generic
423
437
 
424
438
  __version__: Final[str]
425
439
  e: Final[float]
@@ -446,6 +460,7 @@ _dtype = dtype
446
460
 
447
461
  class _Flags:
448
462
  f_contiguous: py_bool
463
+ def __getitem__(self, key: str) -> py_bool: ...
449
464
 
450
465
  class ndarray[Shape: _Shape = _Shape, DType = Any]:
451
466
  shape: Shape
@@ -480,10 +495,9 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
480
495
  def real(self) -> ndarray[Shape]: ...
481
496
  @real.setter
482
497
  def real(self, value: Any) -> None: ...
483
- @overload
484
- def __len__[N: IntVar](self: ndarray[[N]]) -> Int[N]: ...
485
- @overload
486
- def __len__[N: IntVar, M: IntVar](self: ndarray[[N, M]]) -> Int[N]: ...
498
+ def __len__[N: IntVar, Rest: _Shape = []](
499
+ self: ndarray[[N, *Elements[Rest]]],
500
+ ) -> Int[N]: ...
487
501
  @overload
488
502
  def __getitem__[
489
503
  N: IntVar,
@@ -507,103 +521,186 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
507
521
  @overload
508
522
  def __getitem__(
509
523
  self: ndarray[Shape, DType],
510
- key: _BasicIndex
511
- | _IndexScalar
512
- | _IndexSequence
513
- | ndarray
514
- | tuple[_BasicIndex | _IndexScalar | _IndexSequence | ndarray, ...],
524
+ key: _ArrayIndex,
515
525
  ) -> ndarray[IntTuple, DType]: ...
526
+ # Assignment preserves the receiver's shape; value broadcasting is gradual.
527
+ def __setitem__(self, key: _ArrayIndex, value: ArrayLike, /) -> None: ...
528
+ @overload
529
+ def __iter__[N: IntVar, M: IntVar, Rest: _Shape = []](
530
+ self: ndarray[[N, M, *Elements[Rest]], DType], /
531
+ ) -> Iterator[ndarray[[M, *Elements[Rest]], DType]]: ...
532
+ # Iteration over one-dimensional arrays yields NumPy scalars.
533
+ @overload
534
+ def __iter__[N: IntVar](self: ndarray[[N], DType], /) -> Iterator[Any]: ...
535
+ def __int__(self: ndarray[[]], /) -> int: ...
536
+ def __float__(self: ndarray[[]], /) -> float: ...
537
+ def __complex__(self: ndarray[[]], /) -> complex: ...
516
538
  # Only 2-D transpose is modeled for the NumPy shape-stub MVP.
517
539
  @property
518
540
  def T[N: IntVar, P: IntVar](
519
541
  self: ndarray[[N, P], DType],
520
542
  ) -> ndarray[[P, N], DType]: ...
521
- @overload
522
- def __add__(self, other: int | float) -> ndarray[Shape, DType]: ...
523
- @overload
524
- def __add__[OtherShape: _Shape](
525
- self, other: ndarray[OtherShape]
543
+ # Unpacking preserves gradual array shapes instead of using the scalar default.
544
+ def __add__[OtherShape: _Shape = []](
545
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
526
546
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
527
- @overload
528
- def __radd__(self, other: int | float) -> ndarray[Shape, DType]: ...
529
- @overload
530
- def __radd__[OtherShape: _Shape](
531
- self, other: ndarray[OtherShape]
547
+ def __radd__[OtherShape: _Shape = []](
548
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
532
549
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
533
- @overload
534
- def __sub__(self, other: int | float) -> ndarray[Shape, DType]: ...
535
- @overload
536
- def __sub__[OtherShape: _Shape](
537
- self, other: ndarray[OtherShape]
550
+ def __sub__[OtherShape: _Shape = []](
551
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
538
552
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
539
- @overload
540
- def __rsub__(self, other: int | float) -> ndarray[Shape, DType]: ...
541
- @overload
542
- def __rsub__[OtherShape: _Shape](
543
- self, other: ndarray[OtherShape]
553
+ def __rsub__[OtherShape: _Shape = []](
554
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
544
555
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
545
- @overload
546
- def __truediv__(self, other: int | float) -> ndarray[Shape, DType]: ...
547
- @overload
548
- def __truediv__[OtherShape: _Shape](
549
- self, other: ndarray[OtherShape]
556
+ def __truediv__[OtherShape: _Shape = []](
557
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
550
558
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
551
- @overload
552
- def __rtruediv__(self, other: int | float) -> ndarray[Shape, DType]: ...
553
- @overload
554
- def __rtruediv__[OtherShape: _Shape](
555
- self, other: ndarray[OtherShape]
559
+ def __rtruediv__[OtherShape: _Shape = []](
560
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
561
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
562
+ def __mul__[OtherShape: _Shape = []](
563
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
564
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
565
+ def __rmul__[OtherShape: _Shape = []](
566
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
567
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
568
+ def __floordiv__[OtherShape: _Shape = []](
569
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
570
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
571
+ def __rfloordiv__[OtherShape: _Shape = []](
572
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
573
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
574
+ def __mod__[OtherShape: _Shape = []](
575
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
576
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
577
+ def __rmod__[OtherShape: _Shape = []](
578
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
579
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
580
+ def __neg__(self) -> ndarray[Shape, DType]: ...
581
+ def __pos__(self) -> ndarray[Shape, DType]: ...
582
+ def __pow__[OtherShape: _Shape = []](
583
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
584
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
585
+ def __rpow__[OtherShape: _Shape = []](
586
+ self, other: ndarray[[*Elements[OtherShape]]] | int | float
556
587
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
588
+ # Scalar results also satisfy Python's truth-valued comparison protocols.
557
589
  @overload
558
- def __mul__(self, other: int | float) -> ndarray[Shape, DType]: ...
590
+ def __lt__(self: ndarray[[]], other: Any, /) -> Any: ...
559
591
  @overload
560
- def __mul__[OtherShape: _Shape](
561
- self, other: ndarray[OtherShape]
562
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
592
+ def __lt__[OtherShape: _Shape = []](
593
+ self,
594
+ other: ndarray[[*Elements[OtherShape]]]
595
+ | int
596
+ | float
597
+ | complex
598
+ | str
599
+ | bytes
600
+ | generic,
601
+ /,
602
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
563
603
  @overload
564
- def __rmul__(self, other: int | float) -> ndarray[Shape, DType]: ...
604
+ def __lt__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
565
605
  @overload
566
- def __rmul__[OtherShape: _Shape](
567
- self, other: ndarray[OtherShape]
568
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
606
+ def __le__(self: ndarray[[]], other: Any, /) -> Any: ...
569
607
  @overload
570
- def __floordiv__(self, other: int | float) -> ndarray[Shape, DType]: ...
608
+ def __le__[OtherShape: _Shape = []](
609
+ self,
610
+ other: ndarray[[*Elements[OtherShape]]]
611
+ | int
612
+ | float
613
+ | complex
614
+ | str
615
+ | bytes
616
+ | generic,
617
+ /,
618
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
571
619
  @overload
572
- def __floordiv__[OtherShape: _Shape](
573
- self, other: ndarray[OtherShape]
574
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
620
+ def __le__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
575
621
  @overload
576
- def __rfloordiv__(self, other: int | float) -> ndarray[Shape, DType]: ...
622
+ def __gt__(self: ndarray[[]], other: Any, /) -> Any: ...
577
623
  @overload
578
- def __rfloordiv__[OtherShape: _Shape](
579
- self, other: ndarray[OtherShape]
580
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
624
+ def __gt__[OtherShape: _Shape = []](
625
+ self,
626
+ other: ndarray[[*Elements[OtherShape]]]
627
+ | int
628
+ | float
629
+ | complex
630
+ | str
631
+ | bytes
632
+ | generic,
633
+ /,
634
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
581
635
  @overload
582
- def __mod__(self, other: int | float) -> ndarray[Shape, DType]: ...
636
+ def __gt__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
583
637
  @overload
584
- def __mod__[OtherShape: _Shape](
585
- self, other: ndarray[OtherShape]
586
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
638
+ def __ge__(self: ndarray[[]], other: Any, /) -> Any: ...
587
639
  @overload
588
- def __rmod__(self, other: int | float) -> ndarray[Shape, DType]: ...
640
+ def __ge__[OtherShape: _Shape = []](
641
+ self,
642
+ other: ndarray[[*Elements[OtherShape]]]
643
+ | int
644
+ | float
645
+ | complex
646
+ | str
647
+ | bytes
648
+ | generic,
649
+ /,
650
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
589
651
  @overload
590
- def __rmod__[OtherShape: _Shape](
591
- self, other: ndarray[OtherShape]
592
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
593
- def __neg__(self) -> ndarray[Shape, DType]: ...
594
- def __pos__(self) -> ndarray[Shape, DType]: ...
652
+ def __ge__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
595
653
  @overload
596
- def __pow__(self, other: int | float) -> ndarray[Shape, DType]: ...
654
+ def __eq__(self: ndarray[[]], other: Any, /) -> Any: ... # type: ignore[bad-override]
597
655
  @overload
598
- def __pow__[OtherShape: _Shape](
599
- self, other: ndarray[OtherShape]
600
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
656
+ def __eq__[OtherShape: _Shape = []](
657
+ self,
658
+ other: ndarray[[*Elements[OtherShape]]]
659
+ | int
660
+ | float
661
+ | complex
662
+ | str
663
+ | bytes
664
+ | generic,
665
+ /,
666
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
601
667
  @overload
602
- def __rpow__(self, other: int | float) -> ndarray[Shape, DType]: ...
668
+ def __eq__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
603
669
  @overload
604
- def __rpow__[OtherShape: _Shape](
605
- self, other: ndarray[OtherShape]
606
- ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
670
+ def __ne__(self: ndarray[[]], other: Any, /) -> Any: ... # type: ignore[bad-override]
671
+ @overload
672
+ def __ne__[OtherShape: _Shape = []](
673
+ self,
674
+ other: ndarray[[*Elements[OtherShape]]]
675
+ | int
676
+ | float
677
+ | complex
678
+ | str
679
+ | bytes
680
+ | generic,
681
+ /,
682
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
683
+ @overload
684
+ def __ne__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
685
+ def __invert__(self) -> ndarray[Shape, DType]: ...
686
+ def __and__[OtherShape: _Shape = []](
687
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
688
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
689
+ def __or__[OtherShape: _Shape = []](
690
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
691
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
692
+ def __xor__[OtherShape: _Shape = []](
693
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
694
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
695
+ def __rand__[OtherShape: _Shape = []](
696
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
697
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
698
+ def __ror__[OtherShape: _Shape = []](
699
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
700
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
701
+ def __rxor__[OtherShape: _Shape = []](
702
+ self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
703
+ ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
607
704
  def __matmul__[OtherShape: _Shape](
608
705
  self, other: ndarray[OtherShape]
609
706
  ) -> ndarray[matmul_shape(Shape, OtherShape), DType]: ...
@@ -647,14 +744,14 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
647
744
  *,
648
745
  where: Any = True,
649
746
  ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), _dtype[bool_]]: ...
650
- def argmax[Axis: Flag[_Axis], KeepDims: Flag[py_bool]](
747
+ def argmax[Axis: Flag[_SingleAxis], KeepDims: Flag[py_bool]](
651
748
  self,
652
749
  axis: Axis = None,
653
750
  out: Any = None,
654
751
  *,
655
752
  keepdims: KeepDims = False,
656
753
  ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), _dtype[intp]]: ...
657
- def argmin[Axis: Flag[_Axis], KeepDims: Flag[py_bool]](
754
+ def argmin[Axis: Flag[_SingleAxis], KeepDims: Flag[py_bool]](
658
755
  self,
659
756
  axis: Axis = None,
660
757
  out: Any = None,
@@ -1141,10 +1238,120 @@ matvec: _MatvecUFunc
1141
1238
  vecdot: _VecdotUFunc
1142
1239
  vecmat: _VecmatUFunc
1143
1240
 
1241
+ # Constructor overloads preserve dtype only when it is omitted, preserve shape only for
1242
+ # `ndmin=0`, and delegate non-None `like` dispatch with an `Any` result.
1243
+ @overload
1244
+ def array[Shape: _Shape, DType](
1245
+ object: ndarray[Shape, DType],
1246
+ dtype: None = None,
1247
+ *,
1248
+ copy: bool | None = ...,
1249
+ order: str | None = None,
1250
+ subok: bool = ...,
1251
+ ndmin: Literal[0] = 0,
1252
+ like: None = None,
1253
+ ) -> ndarray[Shape, DType]: ...
1254
+ @overload
1255
+ def array[Shape: _Shape](
1256
+ object: ndarray[Shape, Any],
1257
+ dtype: Any,
1258
+ *,
1259
+ copy: bool | None = ...,
1260
+ order: str | None = None,
1261
+ subok: bool = ...,
1262
+ ndmin: Literal[0] = 0,
1263
+ like: None = None,
1264
+ ) -> ndarray[Shape]: ...
1265
+ @overload
1266
+ def array[Shape: _Shape = []](
1267
+ object: _ArrayScalar | RegularNestedList[Shape, _ArrayScalar],
1268
+ dtype: Any = ...,
1269
+ *,
1270
+ copy: bool | None = ...,
1271
+ order: str | None = None,
1272
+ subok: bool = ...,
1273
+ ndmin: Literal[0] = 0,
1274
+ like: None = None,
1275
+ ) -> ndarray[Shape]: ...
1276
+ @overload
1277
+ def array(
1278
+ object: Any,
1279
+ dtype: Any = ...,
1280
+ *,
1281
+ copy: bool | None = ...,
1282
+ order: str | None = None,
1283
+ subok: bool = ...,
1284
+ ndmin: int = ...,
1285
+ like: None = None,
1286
+ ) -> ndarray[IntTuple]: ...
1287
+ @overload
1288
+ def array(
1289
+ object: Any,
1290
+ dtype: Any = ...,
1291
+ *,
1292
+ copy: bool | None = ...,
1293
+ order: str | None = None,
1294
+ subok: bool = ...,
1295
+ ndmin: int = ...,
1296
+ like: Any,
1297
+ ) -> Any: ...
1298
+ @overload
1299
+ def asarray[Shape: _Shape, DType](
1300
+ a: ndarray[Shape, DType],
1301
+ dtype: None = None,
1302
+ order: str | None = None,
1303
+ *,
1304
+ device: Any = ...,
1305
+ copy: bool | None = ...,
1306
+ like: None = None,
1307
+ ) -> ndarray[Shape, DType]: ...
1308
+ @overload
1309
+ def asarray[Shape: _Shape](
1310
+ a: ndarray[Shape, Any],
1311
+ dtype: Any,
1312
+ order: str | None = None,
1313
+ *,
1314
+ device: Any = ...,
1315
+ copy: bool | None = ...,
1316
+ like: None = None,
1317
+ ) -> ndarray[Shape]: ...
1318
+ @overload
1319
+ def asarray[Shape: _Shape = []](
1320
+ a: _ArrayScalar | RegularNestedList[Shape, _ArrayScalar],
1321
+ dtype: Any = ...,
1322
+ order: str | None = None,
1323
+ *,
1324
+ device: Any = ...,
1325
+ copy: bool | None = ...,
1326
+ like: None = None,
1327
+ ) -> ndarray[Shape]: ...
1328
+ @overload
1329
+ def asarray(
1330
+ a: Any,
1331
+ dtype: Any = ...,
1332
+ order: str | None = None,
1333
+ *,
1334
+ device: Any = ...,
1335
+ copy: bool | None = ...,
1336
+ like: None = None,
1337
+ ) -> ndarray[IntTuple]: ...
1338
+ @overload
1339
+ def asarray(
1340
+ a: Any,
1341
+ dtype: Any = ...,
1342
+ order: str | None = None,
1343
+ *,
1344
+ device: Any = ...,
1345
+ copy: bool | None = ...,
1346
+ like: Any,
1347
+ ) -> Any: ...
1144
1348
  def round[Shape: _Shape](x: ndarray[Shape]) -> ndarray[Shape]: ...
1349
+ @overload
1145
1350
  def clip[Shape: _Shape](
1146
1351
  a: ndarray[Shape], a_min: int | float, a_max: int | float
1147
1352
  ) -> ndarray[Shape]: ...
1353
+ @overload
1354
+ def clip(a: ArrayLike, a_min: ArrayLike | None, a_max: ArrayLike | None) -> Any: ...
1148
1355
  def fill_diagonal[N: IntVar, DType](
1149
1356
  a: ndarray[[N, N], DType],
1150
1357
  val: Any,
@@ -1170,21 +1377,9 @@ def diag[S: _Shape, DType](
1170
1377
  v: ndarray[S, DType], k: int = 0
1171
1378
  ) -> ndarray[IntTuple, DType]: ...
1172
1379
  def arange[N: IntVar](stop: Int[N], /) -> ndarray[[N], dtype[intp]]: ...
1173
- @overload
1174
- def expand_dims[N: IntVar, M: IntVar, DType](
1175
- a: ndarray[[N, M], DType],
1176
- axis: Literal[0, -3],
1177
- ) -> ndarray[[1, N, M], DType]: ...
1178
- @overload
1179
- def expand_dims[N: IntVar, M: IntVar, DType](
1180
- a: ndarray[[N, M], DType],
1181
- axis: Literal[1, -2],
1182
- ) -> ndarray[[N, 1, M], DType]: ...
1183
- @overload
1184
- def expand_dims[N: IntVar, M: IntVar, DType](
1185
- a: ndarray[[N, M], DType],
1186
- axis: Literal[2, -1],
1187
- ) -> ndarray[[N, M, 1], DType]: ...
1380
+ def expand_dims[Shape: _Shape, DType, Axis: Flag[int]](
1381
+ a: ndarray[Shape, DType], axis: Axis
1382
+ ) -> ndarray[expand_dims_shape(Shape, Axis), DType]: ...
1188
1383
 
1189
1384
  # These stubs track reduction shapes but leave reduction dtype gradual.
1190
1385
  def sum[
@@ -1203,6 +1398,7 @@ def mean[
1203
1398
  ](
1204
1399
  a: ndarray[Shape, DType], axis: Axis = None, *, keepdims: KeepDims = False
1205
1400
  ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), Any]: ...
1401
+ @overload
1206
1402
  def min[
1207
1403
  Shape: _Shape,
1208
1404
  DType,
@@ -1211,6 +1407,11 @@ def min[
1211
1407
  ](
1212
1408
  a: ndarray[Shape, DType], axis: Axis = None, *, keepdims: KeepDims = False
1213
1409
  ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), Any]: ...
1410
+ @overload
1411
+ def min(
1412
+ a: Sequence[ArrayLike], axis: _Axis = None, *, keepdims: py_bool = False
1413
+ ) -> Any: ...
1414
+ @overload
1214
1415
  def max[
1215
1416
  Shape: _Shape,
1216
1417
  DType,
@@ -1220,115 +1421,82 @@ def max[
1220
1421
  a: ndarray[Shape, DType], axis: Axis = None, *, keepdims: KeepDims = False
1221
1422
  ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), Any]: ...
1222
1423
  @overload
1223
- def argmin[N: IntVar, M: IntVar](
1224
- a: ndarray[[N, M]],
1225
- axis: Literal[0, -2],
1226
- *,
1227
- keepdims: Literal[False] = False,
1228
- ) -> ndarray[[M], dtype[intp]]: ...
1424
+ def max(
1425
+ a: Sequence[ArrayLike], axis: _Axis = None, *, keepdims: py_bool = False
1426
+ ) -> Any: ...
1229
1427
  @overload
1230
- def argmin[N: IntVar, M: IntVar](
1231
- a: ndarray[[N, M]],
1232
- axis: Literal[1, -1],
1233
- *,
1234
- keepdims: Literal[False] = False,
1235
- ) -> ndarray[[N], dtype[intp]]: ...
1236
-
1237
- # TODO(stroxler): Replace these finite tuple-shape constructor overloads with a
1238
- # generic `Shape: tuple[int, ...]` overload once whole-shape parameters flow
1239
- # through downstream array operations without degrading to unknown.
1428
+ def argmin[
1429
+ Shape: _Shape,
1430
+ DType,
1431
+ Axis: Flag[_SingleAxis],
1432
+ KeepDims: Flag[py_bool],
1433
+ ](
1434
+ a: ndarray[Shape, DType], axis: Axis = None, *, keepdims: KeepDims = False
1435
+ ) -> ndarray[reduce_shape(Shape, Axis, KeepDims), dtype[intp]]: ...
1240
1436
  @overload
1241
- def zeros[N: IntVar, ScalarT: generic](
1242
- shape: Int[N], dtype: type[ScalarT], order: str = ...
1243
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1437
+ def argmin(
1438
+ a: Sequence[ArrayLike], axis: _SingleAxis = None, *, keepdims: py_bool = False
1439
+ ) -> Any: ...
1244
1440
  @overload
1245
1441
  def zeros[N: IntVar, ScalarT: generic](
1246
- shape: IntTuple[N], dtype: type[ScalarT], order: str = ...
1442
+ shape: Int[N], dtype: type[ScalarT], order: str = ...
1247
1443
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1248
1444
  @overload
1249
- def zeros[N: IntVar, M: IntVar, ScalarT: generic](
1250
- shape: IntTuple[N, M], dtype: type[ScalarT], order: str = ...
1251
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1445
+ def zeros[Shape: IntTuple, ScalarT: generic](
1446
+ shape: Shape, dtype: type[ScalarT], order: str = ...
1447
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1252
1448
  @overload
1253
1449
  def zeros[N: IntVar, ScalarT: generic](
1254
1450
  shape: Int[N], dtype: dtype[ScalarT], order: str = ...
1255
1451
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1256
1452
  @overload
1257
- def zeros[N: IntVar, ScalarT: generic](
1258
- shape: IntTuple[N], dtype: dtype[ScalarT], order: str = ...
1259
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1260
- @overload
1261
- def zeros[N: IntVar, M: IntVar, ScalarT: generic](
1262
- shape: IntTuple[N, M], dtype: dtype[ScalarT], order: str = ...
1263
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1453
+ def zeros[Shape: IntTuple, ScalarT: generic](
1454
+ shape: Shape, dtype: dtype[ScalarT], order: str = ...
1455
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1264
1456
  @overload
1265
1457
  def zeros[N: IntVar](
1266
1458
  shape: Int[N], dtype: None = ..., order: str = ...
1267
1459
  ) -> ndarray[[N], dtype[float64]]: ...
1268
1460
  @overload
1269
- def zeros[N: IntVar](
1270
- shape: IntTuple[N], dtype: None = ..., order: str = ...
1271
- ) -> ndarray[[N], dtype[float64]]: ...
1272
- @overload
1273
- def zeros[N: IntVar, M: IntVar](
1274
- shape: IntTuple[N, M], dtype: None = ..., order: str = ...
1275
- ) -> ndarray[[N, M], dtype[float64]]: ...
1461
+ def zeros[Shape: IntTuple](
1462
+ shape: Shape, dtype: None = ..., order: str = ...
1463
+ ) -> ndarray[Shape, dtype[float64]]: ...
1276
1464
  @overload
1277
1465
  def zeros[N: IntVar](shape: Int[N], dtype: Any, order: str = ...) -> ndarray[[N]]: ...
1278
1466
  @overload
1279
- def zeros[N: IntVar](
1280
- shape: IntTuple[N], dtype: Any, order: str = ...
1281
- ) -> ndarray[[N]]: ...
1282
- @overload
1283
- def zeros[N: IntVar, M: IntVar](
1284
- shape: IntTuple[N, M], dtype: Any, order: str = ...
1285
- ) -> ndarray[[N, M]]: ...
1467
+ def zeros[Shape: IntTuple](
1468
+ shape: Shape, dtype: Any, order: str = ...
1469
+ ) -> ndarray[Shape]: ...
1286
1470
  @overload
1287
1471
  def ones[N: IntVar, ScalarT: generic](
1288
1472
  shape: Int[N], dtype: type[ScalarT], order: str = ...
1289
1473
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1290
1474
  @overload
1291
- def ones[N: IntVar, ScalarT: generic](
1292
- shape: IntTuple[N], dtype: type[ScalarT], order: str = ...
1293
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1294
- @overload
1295
- def ones[N: IntVar, M: IntVar, ScalarT: generic](
1296
- shape: IntTuple[N, M], dtype: type[ScalarT], order: str = ...
1297
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1475
+ def ones[Shape: IntTuple, ScalarT: generic](
1476
+ shape: Shape, dtype: type[ScalarT], order: str = ...
1477
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1298
1478
  @overload
1299
1479
  def ones[N: IntVar, ScalarT: generic](
1300
1480
  shape: Int[N], dtype: dtype[ScalarT], order: str = ...
1301
1481
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1302
1482
  @overload
1303
- def ones[N: IntVar, ScalarT: generic](
1304
- shape: IntTuple[N], dtype: dtype[ScalarT], order: str = ...
1305
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1306
- @overload
1307
- def ones[N: IntVar, M: IntVar, ScalarT: generic](
1308
- shape: IntTuple[N, M], dtype: dtype[ScalarT], order: str = ...
1309
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1483
+ def ones[Shape: IntTuple, ScalarT: generic](
1484
+ shape: Shape, dtype: dtype[ScalarT], order: str = ...
1485
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1310
1486
  @overload
1311
1487
  def ones[N: IntVar](
1312
1488
  shape: Int[N], dtype: None = ..., order: str = ...
1313
1489
  ) -> ndarray[[N], dtype[float64]]: ...
1314
1490
  @overload
1315
- def ones[N: IntVar](
1316
- shape: IntTuple[N], dtype: None = ..., order: str = ...
1317
- ) -> ndarray[[N], dtype[float64]]: ...
1318
- @overload
1319
- def ones[N: IntVar, M: IntVar](
1320
- shape: IntTuple[N, M], dtype: None = ..., order: str = ...
1321
- ) -> ndarray[[N, M], dtype[float64]]: ...
1491
+ def ones[Shape: IntTuple](
1492
+ shape: Shape, dtype: None = ..., order: str = ...
1493
+ ) -> ndarray[Shape, dtype[float64]]: ...
1322
1494
  @overload
1323
1495
  def ones[N: IntVar](shape: Int[N], dtype: Any, order: str = ...) -> ndarray[[N]]: ...
1324
1496
  @overload
1325
- def ones[N: IntVar](
1326
- shape: IntTuple[N], dtype: Any, order: str = ...
1327
- ) -> ndarray[[N]]: ...
1328
- @overload
1329
- def ones[N: IntVar, M: IntVar](
1330
- shape: IntTuple[N, M], dtype: Any, order: str = ...
1331
- ) -> ndarray[[N, M]]: ...
1497
+ def ones[Shape: IntTuple](
1498
+ shape: Shape, dtype: Any, order: str = ...
1499
+ ) -> ndarray[Shape]: ...
1332
1500
  @overload
1333
1501
  def full[N: IntVar, ScalarT: generic](
1334
1502
  shape: Int[N],
@@ -1337,19 +1505,12 @@ def full[N: IntVar, ScalarT: generic](
1337
1505
  order: str = ...,
1338
1506
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1339
1507
  @overload
1340
- def full[N: IntVar, ScalarT: generic](
1341
- shape: IntTuple[N],
1508
+ def full[Shape: IntTuple, ScalarT: generic](
1509
+ shape: Shape,
1342
1510
  fill_value: Any,
1343
1511
  dtype: type[ScalarT],
1344
1512
  order: str = ...,
1345
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1346
- @overload
1347
- def full[N: IntVar, M: IntVar, ScalarT: generic](
1348
- shape: IntTuple[N, M],
1349
- fill_value: Any,
1350
- dtype: type[ScalarT],
1351
- order: str = ...,
1352
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1513
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1353
1514
  @overload
1354
1515
  def full[N: IntVar, ScalarT: generic](
1355
1516
  shape: Int[N],
@@ -1358,80 +1519,50 @@ def full[N: IntVar, ScalarT: generic](
1358
1519
  order: str = ...,
1359
1520
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1360
1521
  @overload
1361
- def full[N: IntVar, ScalarT: generic](
1362
- shape: IntTuple[N],
1363
- fill_value: Any,
1364
- dtype: dtype[ScalarT],
1365
- order: str = ...,
1366
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1367
- @overload
1368
- def full[N: IntVar, M: IntVar, ScalarT: generic](
1369
- shape: IntTuple[N, M],
1522
+ def full[Shape: IntTuple, ScalarT: generic](
1523
+ shape: Shape,
1370
1524
  fill_value: Any,
1371
1525
  dtype: dtype[ScalarT],
1372
1526
  order: str = ...,
1373
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1527
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1374
1528
  @overload
1375
1529
  def full[N: IntVar](
1376
1530
  shape: Int[N], fill_value: Any, dtype: Any = ..., order: str = ...
1377
1531
  ) -> ndarray[[N]]: ...
1378
1532
  @overload
1379
- def full[N: IntVar](
1380
- shape: IntTuple[N], fill_value: Any, dtype: Any = ..., order: str = ...
1381
- ) -> ndarray[[N]]: ...
1382
- @overload
1383
- def full[N: IntVar, M: IntVar](
1384
- shape: IntTuple[N, M],
1385
- fill_value: Any,
1386
- dtype: Any = ...,
1387
- order: str = ...,
1388
- ) -> ndarray[[N, M]]: ...
1533
+ def full[Shape: IntTuple](
1534
+ shape: Shape, fill_value: Any, dtype: Any = ..., order: str = ...
1535
+ ) -> ndarray[Shape]: ...
1389
1536
  @overload
1390
1537
  def empty[N: IntVar, ScalarT: generic](
1391
1538
  shape: Int[N], dtype: type[ScalarT], order: str = ...
1392
1539
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1393
1540
  @overload
1394
- def empty[N: IntVar, ScalarT: generic](
1395
- shape: IntTuple[N], dtype: type[ScalarT], order: str = ...
1396
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1397
- @overload
1398
- def empty[N: IntVar, M: IntVar, ScalarT: generic](
1399
- shape: IntTuple[N, M], dtype: type[ScalarT], order: str = ...
1400
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1541
+ def empty[Shape: IntTuple, ScalarT: generic](
1542
+ shape: Shape, dtype: type[ScalarT], order: str = ...
1543
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1401
1544
  @overload
1402
1545
  def empty[N: IntVar, ScalarT: generic](
1403
1546
  shape: Int[N], dtype: dtype[ScalarT], order: str = ...
1404
1547
  ) -> ndarray[[N], dtype[ScalarT]]: ...
1405
1548
  @overload
1406
- def empty[N: IntVar, ScalarT: generic](
1407
- shape: IntTuple[N], dtype: dtype[ScalarT], order: str = ...
1408
- ) -> ndarray[[N], dtype[ScalarT]]: ...
1409
- @overload
1410
- def empty[N: IntVar, M: IntVar, ScalarT: generic](
1411
- shape: IntTuple[N, M], dtype: dtype[ScalarT], order: str = ...
1412
- ) -> ndarray[[N, M], dtype[ScalarT]]: ...
1549
+ def empty[Shape: IntTuple, ScalarT: generic](
1550
+ shape: Shape, dtype: dtype[ScalarT], order: str = ...
1551
+ ) -> ndarray[Shape, dtype[ScalarT]]: ...
1413
1552
  @overload
1414
1553
  def empty[N: IntVar](
1415
1554
  shape: Int[N], dtype: None = ..., order: str = ...
1416
1555
  ) -> ndarray[[N], dtype[float64]]: ...
1417
1556
  @overload
1418
- def empty[N: IntVar](
1419
- shape: IntTuple[N], dtype: None = ..., order: str = ...
1420
- ) -> ndarray[[N], dtype[float64]]: ...
1421
- @overload
1422
- def empty[N: IntVar, M: IntVar](
1423
- shape: IntTuple[N, M], dtype: None = ..., order: str = ...
1424
- ) -> ndarray[[N, M], dtype[float64]]: ...
1557
+ def empty[Shape: IntTuple](
1558
+ shape: Shape, dtype: None = ..., order: str = ...
1559
+ ) -> ndarray[Shape, dtype[float64]]: ...
1425
1560
  @overload
1426
1561
  def empty[N: IntVar](shape: Int[N], dtype: Any, order: str = ...) -> ndarray[[N]]: ...
1427
1562
  @overload
1428
- def empty[N: IntVar](
1429
- shape: IntTuple[N], dtype: Any, order: str = ...
1430
- ) -> ndarray[[N]]: ...
1431
- @overload
1432
- def empty[N: IntVar, M: IntVar](
1433
- shape: IntTuple[N, M], dtype: Any, order: str = ...
1434
- ) -> ndarray[[N, M]]: ...
1563
+ def empty[Shape: IntTuple](
1564
+ shape: Shape, dtype: Any, order: str = ...
1565
+ ) -> ndarray[Shape]: ...
1435
1566
  @overload
1436
1567
  def eye[N: IntVar, ScalarT: generic](
1437
1568
  N: Int[N], M: None = ..., k: int = ..., *, dtype: type[ScalarT], order: str = ...
@@ -1478,7 +1609,12 @@ def identity[N: IntVar](
1478
1609
  n: Int[N], dtype: Any, *, like: Any = ...
1479
1610
  ) -> ndarray[[N, N]]: ...
1480
1611
 
1481
- # TODO(stroxler): Replace these placeholders with NumPy's scalar type hierarchy.
1612
+ # TODO(stroxler): Replace the remaining placeholders with NumPy's scalar hierarchy.
1613
+ class number(generic): ...
1614
+ class inexact(number): ...
1615
+ class floating(inexact): ...
1616
+ class integer(number): ...
1617
+
1482
1618
  bool: Any
1483
1619
  broadcast: Any
1484
1620
  byte: Any
@@ -1494,18 +1630,14 @@ datetime64: Any
1494
1630
  double: Any
1495
1631
  flexible: Any
1496
1632
  float16: Any
1497
- floating: Any
1498
1633
  half: Any
1499
- inexact: Any
1500
1634
  int16: Any
1501
1635
  int8: Any
1502
1636
  int_: Any
1503
1637
  intc: Any
1504
- integer: Any
1505
1638
  long: Any
1506
1639
  longdouble: Any
1507
1640
  longlong: Any
1508
- number: Any
1509
1641
  object_: Any
1510
1642
  short: Any
1511
1643
  signedinteger: Any
@@ -123,3 +123,22 @@ def stack_shape(shapes: IntTuples, axis: int) -> IntTuple:
123
123
  for index in range(output_rank)
124
124
  )
125
125
  )
126
+
127
+ @type_shape_dsl_function
128
+ def expand_dims_shape(shape: IntTuple, axis: int) -> IntTuple:
129
+ output_rank = len(shape) + 1
130
+ # Unary minus is not supported by the type-level DSL.
131
+ if axis < 0 - output_rank or axis >= output_rank:
132
+ return dsl.Invalid("expand_dims axis out of bounds")
133
+ if axis < 0:
134
+ norm_axis = axis + output_rank
135
+ else:
136
+ norm_axis = axis + 0
137
+ return dsl.IntTuple(
138
+ (
139
+ 1
140
+ if index == norm_axis
141
+ else shape[index if index < norm_axis else index - 1]
142
+ for index in range(output_rank)
143
+ )
144
+ )
@@ -39,7 +39,7 @@ from numpy.linalg._linalg import (
39
39
  )
40
40
  from shape_extensions import Int, IntVar
41
41
 
42
- from .. import ndarray
42
+ from .. import floating, ndarray
43
43
 
44
44
  class LinAlgError(ValueError): ...
45
45
 
@@ -70,6 +70,14 @@ def norm[N: IntVar, M: IntVar, DType](
70
70
  keepdims: Literal[True],
71
71
  ) -> ndarray[[N, M, 1], DType]: ...
72
72
  @overload
73
+ def norm(
74
+ x: ndarray,
75
+ ord: Any = None,
76
+ axis: None = None,
77
+ keepdims: Literal[False] = False,
78
+ **kwargs: Any,
79
+ ) -> floating: ...
80
+ @overload
73
81
  def norm(
74
82
  x: Any,
75
83
  ord: Any = None,
@@ -79,13 +79,11 @@ from numpy.random.mtrand import (
79
79
  weibull as weibull,
80
80
  zipf as zipf,
81
81
  )
82
- from shape_extensions import Int, IntVar
82
+ from shape_extensions import IntTuple
83
83
 
84
84
  from .. import dtype, float64, ndarray
85
85
 
86
86
  @overload
87
- def randn[N: IntVar](d0: Int[N], /) -> ndarray[[N], dtype[float64]]: ...
87
+ def randn() -> float: ...
88
88
  @overload
89
- def randn[N: IntVar, M: IntVar](
90
- d0: Int[N], d1: Int[M], /
91
- ) -> ndarray[[N, M], dtype[float64]]: ...
89
+ def randn[Shape: IntTuple](*dims: *Shape) -> ndarray[Shape, dtype[float64]]: ...
@@ -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.2"
11
+ version = "1.4.0.dev2"
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.2",
21
+ "pyrefly-shape-extensions==1.4.0.dev2",
22
22
  ]
23
23
 
24
24
  [project.urls]