pyrefly-numpy-stubs 1.4.0.dev2__py3-none-any.whl → 1.4.0.dev4__py3-none-any.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.
numpy-stubs/__init__.pyi CHANGED
@@ -6,7 +6,7 @@
6
6
  from builtins import bool as py_bool
7
7
  from collections.abc import Iterator, Sequence
8
8
  from types import EllipsisType
9
- from typing import Any, Final, Literal, overload, SupportsIndex
9
+ from typing import Any, Final, Literal, Never, overload, SupportsIndex
10
10
 
11
11
  import shape_extensions
12
12
  from numpy.__config__ import (
@@ -18,6 +18,7 @@ from numpy._array_api_info import (
18
18
  from numpy._core._asarray import (
19
19
  require as require,
20
20
  )
21
+ from numpy._core._internal import _ctypes
21
22
  from numpy._core._type_aliases import (
22
23
  sctypeDict as sctypeDict,
23
24
  )
@@ -182,12 +183,26 @@ from numpy._core.shape_base import (
182
183
  )
183
184
  from numpy._pytesttester import PytestTester
184
185
  from numpy._shapes import (
186
+ compress_shape,
185
187
  diag_extent,
188
+ diag_matrix_shape,
189
+ diagonal_shape,
190
+ dot_shape,
186
191
  expand_dims_shape,
187
192
  matmul_shape,
188
193
  matvec_shape,
194
+ nonzero_shapes,
189
195
  reduce_shape,
196
+ repeat_sequence_shape,
197
+ repeat_shape,
198
+ reshape_shape,
199
+ reshape_varargs_shape,
200
+ reverse_shape,
201
+ squeeze_shape,
190
202
  stack_shape,
203
+ swapaxes_shape,
204
+ take_shape,
205
+ transpose_shape,
191
206
  vecdot_shape,
192
207
  vecmat_shape,
193
208
  )
@@ -377,12 +392,13 @@ from numpy.matrixlib import (
377
392
  )
378
393
  from shape_extensions import (
379
394
  broadcast as _broadcast_shape,
380
- Elements,
381
395
  Flag,
382
396
  Index,
383
397
  index_shape,
384
398
  Int,
399
+ IntListLiteral,
385
400
  IntTuple,
401
+ IntTupleOrList,
386
402
  IntTuples,
387
403
  IntVar,
388
404
  MapIntTuples,
@@ -417,14 +433,47 @@ type _BasicIndex = int | slice | list[int] | None | EllipsisType
417
433
  class generic:
418
434
  def astype(self, dtype: Any, **kwargs: Any) -> Any: ...
419
435
 
436
+ class number(generic): ...
437
+ class integer(number): ...
438
+
439
+ class signedinteger(integer):
440
+ # TODO(stroxler): Sequence inputs to scalar constructors can produce arrays.
441
+ def __init__(self, value: object = ...) -> None: ...
442
+
443
+ class unsignedinteger(integer): ...
444
+ class inexact(number): ...
445
+ class floating(inexact): ...
446
+ class complexfloating(inexact): ...
420
447
  class bool_(generic): ...
421
- class float32(generic): ...
422
- class float64(generic): ...
423
- class int32(generic): ...
424
- class int64(generic): ...
425
- class intp(generic): ...
448
+ class float16(floating): ...
449
+ class float32(floating): ...
450
+ class float64(floating): ...
451
+ class longdouble(floating): ...
452
+ class complex64(complexfloating): ...
453
+ class complex128(complexfloating): ...
454
+ class clongdouble(complexfloating): ...
455
+ class flexible(generic): ...
456
+ class character(flexible): ...
457
+ class bytes_(character, bytes): ...
458
+ class str_(character, str): ...
459
+ class void(flexible): ...
460
+ class object_(generic): ...
461
+ class datetime64(generic): ...
462
+ class timedelta64(signedinteger): ...
463
+ class int8(signedinteger): ...
464
+ class int16(signedinteger): ...
465
+ class int32(signedinteger): ...
466
+ class int64(signedinteger): ...
467
+ class intp(signedinteger): ...
468
+ class longlong(signedinteger): ...
469
+ class uint8(unsignedinteger): ...
470
+ class uint16(unsignedinteger): ...
471
+ class uint32(unsignedinteger): ...
472
+ class uint64(unsignedinteger): ...
473
+ class uintp(unsignedinteger): ...
474
+ class ulonglong(unsignedinteger): ...
426
475
 
427
- type _IndexScalar = int | bool_ | int32 | int64 | intp
476
+ type _IndexScalar = int | bool_ | integer
428
477
  type _IndexSequence = Sequence[_IndexScalar] | Sequence[Sequence[_IndexScalar]]
429
478
  type _ArrayIndex = (
430
479
  _BasicIndex
@@ -433,7 +482,7 @@ type _ArrayIndex = (
433
482
  | ndarray
434
483
  | tuple[_BasicIndex | _IndexScalar | _IndexSequence | ndarray, ...]
435
484
  )
436
- type _ArrayScalar = None | bool | int | float | complex | str | bytes | generic
485
+ type _ArrayScalar = None | py_bool | int | float | complex | str | bytes | generic
437
486
 
438
487
  __version__: Final[str]
439
488
  e: Final[float]
@@ -496,7 +545,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
496
545
  @real.setter
497
546
  def real(self, value: Any) -> None: ...
498
547
  def __len__[N: IntVar, Rest: _Shape = []](
499
- self: ndarray[[N, *Elements[Rest]]],
548
+ self: ndarray[[N, *Rest]],
500
549
  ) -> Int[N]: ...
501
550
  @overload
502
551
  def __getitem__[
@@ -527,8 +576,8 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
527
576
  def __setitem__(self, key: _ArrayIndex, value: ArrayLike, /) -> None: ...
528
577
  @overload
529
578
  def __iter__[N: IntVar, M: IntVar, Rest: _Shape = []](
530
- self: ndarray[[N, M, *Elements[Rest]], DType], /
531
- ) -> Iterator[ndarray[[M, *Elements[Rest]], DType]]: ...
579
+ self: ndarray[[N, M, *Rest], DType], /
580
+ ) -> Iterator[ndarray[[M, *Rest], DType]]: ...
532
581
  # Iteration over one-dimensional arrays yields NumPy scalars.
533
582
  @overload
534
583
  def __iter__[N: IntVar](self: ndarray[[N], DType], /) -> Iterator[Any]: ...
@@ -542,48 +591,49 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
542
591
  ) -> ndarray[[P, N], DType]: ...
543
592
  # Unpacking preserves gradual array shapes instead of using the scalar default.
544
593
  def __add__[OtherShape: _Shape = []](
545
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
594
+ self, other: ndarray[[*OtherShape]] | int | float
546
595
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
547
596
  def __radd__[OtherShape: _Shape = []](
548
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
597
+ self, other: ndarray[[*OtherShape]] | int | float
549
598
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
550
599
  def __sub__[OtherShape: _Shape = []](
551
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
600
+ self, other: ndarray[[*OtherShape]] | int | float
552
601
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
553
602
  def __rsub__[OtherShape: _Shape = []](
554
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
603
+ self, other: ndarray[[*OtherShape]] | int | float
555
604
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
556
605
  def __truediv__[OtherShape: _Shape = []](
557
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
606
+ self, other: ndarray[[*OtherShape]] | int | float
558
607
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
559
608
  def __rtruediv__[OtherShape: _Shape = []](
560
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
609
+ self, other: ndarray[[*OtherShape]] | int | float
561
610
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
562
611
  def __mul__[OtherShape: _Shape = []](
563
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
612
+ self, other: ndarray[[*OtherShape]] | int | float
564
613
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
565
614
  def __rmul__[OtherShape: _Shape = []](
566
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
615
+ self, other: ndarray[[*OtherShape]] | int | float
567
616
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
568
617
  def __floordiv__[OtherShape: _Shape = []](
569
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
618
+ self, other: ndarray[[*OtherShape]] | int | float
570
619
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
571
620
  def __rfloordiv__[OtherShape: _Shape = []](
572
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
621
+ self, other: ndarray[[*OtherShape]] | int | float
573
622
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
574
623
  def __mod__[OtherShape: _Shape = []](
575
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
624
+ self, other: ndarray[[*OtherShape]] | int | float
576
625
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
577
626
  def __rmod__[OtherShape: _Shape = []](
578
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
627
+ self, other: ndarray[[*OtherShape]] | int | float
579
628
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
580
629
  def __neg__(self) -> ndarray[Shape, DType]: ...
581
630
  def __pos__(self) -> ndarray[Shape, DType]: ...
631
+ def __abs__(self, /) -> ndarray[Shape]: ...
582
632
  def __pow__[OtherShape: _Shape = []](
583
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
633
+ self, other: ndarray[[*OtherShape]] | int | float
584
634
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
585
635
  def __rpow__[OtherShape: _Shape = []](
586
- self, other: ndarray[[*Elements[OtherShape]]] | int | float
636
+ self, other: ndarray[[*OtherShape]] | int | float
587
637
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), DType]: ...
588
638
  # Scalar results also satisfy Python's truth-valued comparison protocols.
589
639
  @overload
@@ -591,13 +641,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
591
641
  @overload
592
642
  def __lt__[OtherShape: _Shape = []](
593
643
  self,
594
- other: ndarray[[*Elements[OtherShape]]]
595
- | int
596
- | float
597
- | complex
598
- | str
599
- | bytes
600
- | generic,
644
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
601
645
  /,
602
646
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
603
647
  @overload
@@ -607,13 +651,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
607
651
  @overload
608
652
  def __le__[OtherShape: _Shape = []](
609
653
  self,
610
- other: ndarray[[*Elements[OtherShape]]]
611
- | int
612
- | float
613
- | complex
614
- | str
615
- | bytes
616
- | generic,
654
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
617
655
  /,
618
656
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
619
657
  @overload
@@ -623,13 +661,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
623
661
  @overload
624
662
  def __gt__[OtherShape: _Shape = []](
625
663
  self,
626
- other: ndarray[[*Elements[OtherShape]]]
627
- | int
628
- | float
629
- | complex
630
- | str
631
- | bytes
632
- | generic,
664
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
633
665
  /,
634
666
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
635
667
  @overload
@@ -639,13 +671,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
639
671
  @overload
640
672
  def __ge__[OtherShape: _Shape = []](
641
673
  self,
642
- other: ndarray[[*Elements[OtherShape]]]
643
- | int
644
- | float
645
- | complex
646
- | str
647
- | bytes
648
- | generic,
674
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
649
675
  /,
650
676
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
651
677
  @overload
@@ -655,13 +681,7 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
655
681
  @overload
656
682
  def __eq__[OtherShape: _Shape = []](
657
683
  self,
658
- other: ndarray[[*Elements[OtherShape]]]
659
- | int
660
- | float
661
- | complex
662
- | str
663
- | bytes
664
- | generic,
684
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
665
685
  /,
666
686
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
667
687
  @overload
@@ -671,35 +691,29 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
671
691
  @overload
672
692
  def __ne__[OtherShape: _Shape = []](
673
693
  self,
674
- other: ndarray[[*Elements[OtherShape]]]
675
- | int
676
- | float
677
- | complex
678
- | str
679
- | bytes
680
- | generic,
694
+ other: ndarray[[*OtherShape]] | int | float | complex | str | bytes | generic,
681
695
  /,
682
696
  ) -> ndarray[_broadcast_shape(Shape, OtherShape), _dtype[bool_]]: ...
683
697
  @overload
684
698
  def __ne__(self, other: ArrayLike, /) -> ndarray[IntTuple, _dtype[bool_]]: ...
685
699
  def __invert__(self) -> ndarray[Shape, DType]: ...
686
700
  def __and__[OtherShape: _Shape = []](
687
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
701
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
688
702
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
689
703
  def __or__[OtherShape: _Shape = []](
690
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
704
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
691
705
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
692
706
  def __xor__[OtherShape: _Shape = []](
693
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
707
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
694
708
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
695
709
  def __rand__[OtherShape: _Shape = []](
696
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
710
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
697
711
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
698
712
  def __ror__[OtherShape: _Shape = []](
699
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
713
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
700
714
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
701
715
  def __rxor__[OtherShape: _Shape = []](
702
- self, other: ndarray[[*Elements[OtherShape]]] | _IndexScalar, /
716
+ self, other: ndarray[[*OtherShape]] | _IndexScalar, /
703
717
  ) -> ndarray[_broadcast_shape(Shape, OtherShape)]: ...
704
718
  def __matmul__[OtherShape: _Shape](
705
719
  self, other: ndarray[OtherShape]
@@ -884,8 +898,13 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
884
898
  ) -> None: ...
885
899
  def put(self, indices: Any, values: Any, mode: str = "raise") -> None: ...
886
900
  def ravel(self, order: str = "C") -> ndarray[[int], DType]: ...
887
- # TODO(stroxler): Model receiver invalidation for shape-changing mutation.
888
- resize: Any
901
+ def resize(self, *, not_usable_with_shape_types: Never) -> None:
902
+ """Unsupported: resizing changes the receiver's shape in place.
903
+
904
+ A statically typed array cannot acquire a new shape type after a method call.
905
+ Create a new array instead of resizing an existing one.
906
+ """
907
+ ...
889
908
  def round(self, decimals: int = 0, out: Any = None) -> ndarray[Shape, DType]: ...
890
909
  def setfield(self, val: Any, dtype: Any, offset: int = 0) -> None: ...
891
910
  def setflags(
@@ -909,24 +928,270 @@ class ndarray[Shape: _Shape = _Shape, DType = Any]:
909
928
  def tofile(self, fid: Any, sep: str = "", format: str = "%s") -> None: ...
910
929
  def tolist(self) -> Any: ...
911
930
 
912
- # TODO(stroxler): Replace these placeholders with shape-aware declarations.
913
- choose: Any
914
- compress: Any
915
- ctypes: Any
916
- diagonal: Any
917
- dot: Any
918
- flat: Any
919
- mT: Any
920
- nonzero: Any
921
- repeat: Any
922
- reshape: Any
923
- searchsorted: Any
924
- squeeze: Any
925
- swapaxes: Any
926
- take: Any
927
- trace: Any
928
- transpose: Any
929
- view: Any
931
+ # TODO(stroxler): Model broadcast choices and condition-dependent compress lengths.
932
+ @overload
933
+ def choose(
934
+ self,
935
+ choices: ArrayLike,
936
+ out: None = None,
937
+ mode: Literal["raise", "wrap", "clip"] = "raise",
938
+ ) -> ndarray[IntTuple]: ...
939
+ @overload
940
+ def choose[Out: ndarray](
941
+ self,
942
+ choices: ArrayLike,
943
+ out: Out,
944
+ mode: Literal["raise", "wrap", "clip"] = "raise",
945
+ ) -> Out: ...
946
+ @overload
947
+ def compress[Axis: Flag[int | None]](
948
+ self, condition: Sequence[int] | ndarray, axis: Axis = None, out: None = None
949
+ ) -> ndarray[compress_shape(Shape, Axis), DType]: ...
950
+ @overload
951
+ def compress[Out: ndarray](
952
+ self,
953
+ condition: Sequence[int] | ndarray,
954
+ axis: int | None,
955
+ out: Out,
956
+ ) -> Out: ...
957
+ @overload
958
+ def compress[Out: ndarray](
959
+ self,
960
+ condition: Sequence[int] | ndarray,
961
+ axis: int | None = None,
962
+ *,
963
+ out: Out,
964
+ ) -> Out: ...
965
+ @property
966
+ def ctypes(self) -> _ctypes[int]: ...
967
+ def diagonal[Offset: Flag[int], First: Flag[int], Second: Flag[int]](
968
+ self, offset: Offset = 0, axis1: First = 0, axis2: Second = 1
969
+ ) -> ndarray[diagonal_shape(Shape, First, Second, True, Offset), DType]: ...
970
+ @overload
971
+ def dot[N: IntVar](
972
+ self: ndarray[[N], DType], b: ndarray[[N]], /, out: None = None
973
+ ) -> generic: ...
974
+ @overload
975
+ def dot(self: ndarray[[]], b: _ArrayScalar, /, out: None = None) -> generic: ...
976
+ @overload
977
+ def dot[Right: IntTuple](
978
+ self, b: ndarray[Right], /, out: None = None
979
+ ) -> ndarray[dot_shape(Shape, Right)]: ...
980
+ @overload
981
+ def dot[Right: IntTuple](
982
+ self,
983
+ b: RegularNestedList[Right, _ArrayScalar],
984
+ /,
985
+ out: None = None,
986
+ ) -> ndarray[dot_shape(Shape, Right)]: ...
987
+ @overload
988
+ def dot(
989
+ self, b: int | float | complex | generic, /, out: None = None
990
+ ) -> ndarray[Shape]: ...
991
+ @overload
992
+ def dot[Out: ndarray](self, b: ArrayLike, /, out: Out) -> Out: ...
993
+ # TODO(stroxler): Infer scalar dtype promotion for vector inner products.
994
+ @property
995
+ def flat(self) -> flatiter[ndarray[Shape, DType]]: ...
996
+ @property
997
+ def mT(self) -> ndarray[swapaxes_shape(Shape, -1, -2), DType]: ...
998
+ def nonzero(
999
+ self,
1000
+ ) -> MapIntTuples[lambda S: ndarray[S, dtype[intp]], nonzero_shapes(Shape)]: ...
1001
+ @overload
1002
+ def repeat[Repeats: Int, Axis: Flag[int | None]](
1003
+ self, repeats: Repeats, /, axis: Axis = None
1004
+ ) -> ndarray[repeat_shape(Shape, Repeats, Axis), DType]: ...
1005
+ @overload
1006
+ def repeat[Repeats: IntTuple, Axis: Flag[int | None]](
1007
+ self, repeats: Repeats | IntListLiteral[Repeats], /, axis: Axis = None
1008
+ ) -> ndarray[repeat_sequence_shape(Shape, Repeats, Axis), DType]: ...
1009
+ @overload
1010
+ def repeat[Axis: Flag[int | None]](
1011
+ self, repeats: Sequence[int] | ndarray, /, axis: Axis = None
1012
+ ) -> ndarray[repeat_shape(Shape, Int, Axis), DType]: ...
1013
+ @overload
1014
+ def reshape(
1015
+ self,
1016
+ shape: None,
1017
+ /,
1018
+ *,
1019
+ order: Literal["C", "F", "A"] = "C",
1020
+ copy: py_bool | None = None,
1021
+ ) -> ndarray[Shape, DType]: ...
1022
+ @overload
1023
+ def reshape[Target: IntTuple](
1024
+ self,
1025
+ shape: IntTupleOrList[Target],
1026
+ /,
1027
+ *,
1028
+ order: Literal["C", "F", "A"] = "C",
1029
+ copy: py_bool | None = None,
1030
+ ) -> ndarray[reshape_shape(Shape, Target), DType]: ...
1031
+ @overload
1032
+ def reshape[Target: IntTuple](
1033
+ self,
1034
+ /,
1035
+ *shape: *Target,
1036
+ order: Literal["C", "F", "A"] = "C",
1037
+ copy: py_bool | None = None,
1038
+ ) -> ndarray[reshape_varargs_shape(Shape, Target), DType]: ...
1039
+ @overload
1040
+ def searchsorted[N: IntVar](
1041
+ self: ndarray[[N]],
1042
+ v: _ArrayScalar | ndarray[[]],
1043
+ /,
1044
+ side: Literal["left", "right"] = "left",
1045
+ sorter: ndarray[[N], _dtype[intp]] | Sequence[int] | None = None,
1046
+ ) -> intp: ...
1047
+ @overload
1048
+ def searchsorted[N: IntVar, Values: _Shape](
1049
+ self: ndarray[[N]],
1050
+ v: ndarray[Values],
1051
+ /,
1052
+ side: Literal["left", "right"] = "left",
1053
+ sorter: ndarray[[N], _dtype[intp]] | Sequence[int] | None = None,
1054
+ ) -> ndarray[Values, _dtype[intp]]: ...
1055
+ @overload
1056
+ def searchsorted[N: IntVar, Values: _Shape](
1057
+ self: ndarray[[N]],
1058
+ v: RegularNestedList[Values, _ArrayScalar],
1059
+ /,
1060
+ side: Literal["left", "right"] = "left",
1061
+ sorter: ndarray[[N], _dtype[intp]] | Sequence[int] | None = None,
1062
+ ) -> ndarray[Values, _dtype[intp]]: ...
1063
+ def squeeze[Axis: Flag[_Axis]](
1064
+ self, axis: Axis = None
1065
+ ) -> ndarray[squeeze_shape(Shape, Axis), DType]: ...
1066
+ def swapaxes[First: Flag[int], Second: Flag[int]](
1067
+ self, axis1: First, axis2: Second, /
1068
+ ) -> ndarray[swapaxes_shape(Shape, First, Second), DType]: ...
1069
+ @overload
1070
+ def take(
1071
+ self,
1072
+ indices: int | int32 | int64 | intp,
1073
+ /,
1074
+ axis: None = None,
1075
+ out: None = None,
1076
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1077
+ ) -> generic: ...
1078
+ @overload
1079
+ def take[Axis: Flag[int]](
1080
+ self,
1081
+ indices: int | int32 | int64 | intp,
1082
+ /,
1083
+ axis: Axis,
1084
+ out: None = None,
1085
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1086
+ ) -> ndarray[take_shape(Shape, IntTuple[()], Axis), DType]: ...
1087
+ @overload
1088
+ def take[Indices: IntTuple](
1089
+ self,
1090
+ indices: ndarray[Indices],
1091
+ /,
1092
+ axis: None = None,
1093
+ out: None = None,
1094
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1095
+ ) -> ndarray[Indices, DType]: ...
1096
+ @overload
1097
+ def take(
1098
+ self,
1099
+ indices: Sequence[int],
1100
+ /,
1101
+ axis: None = None,
1102
+ out: None = None,
1103
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1104
+ ) -> ndarray[[int], DType]: ...
1105
+ @overload
1106
+ def take[Axis: Flag[int]](
1107
+ self,
1108
+ indices: Sequence[int],
1109
+ /,
1110
+ axis: Axis,
1111
+ out: None = None,
1112
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1113
+ ) -> ndarray[take_shape(Shape, IntTuple[int], Axis), DType]: ...
1114
+ @overload
1115
+ def take[Indices: IntTuple, Axis: Flag[int]](
1116
+ self,
1117
+ indices: ndarray[Indices],
1118
+ /,
1119
+ axis: Axis,
1120
+ out: None = None,
1121
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1122
+ ) -> ndarray[take_shape(Shape, Indices, Axis), DType]: ...
1123
+ @overload
1124
+ def take[Out: ndarray](
1125
+ self,
1126
+ indices: int | Sequence[int] | ndarray,
1127
+ /,
1128
+ axis: int | None,
1129
+ out: Out,
1130
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1131
+ ) -> Out: ...
1132
+ @overload
1133
+ def take[Out: ndarray](
1134
+ self,
1135
+ indices: int | Sequence[int] | ndarray,
1136
+ /,
1137
+ axis: int | None = None,
1138
+ *,
1139
+ out: Out,
1140
+ mode: Literal["raise", "wrap", "clip"] = "raise",
1141
+ ) -> Out: ...
1142
+ # TODO(stroxler): Restrict index-array dtypes to valid index types.
1143
+ @overload
1144
+ def trace[M: IntVar, N: IntVar](
1145
+ self: ndarray[[M, N], DType],
1146
+ offset: int = 0,
1147
+ axis1: int = 0,
1148
+ axis2: int = 1,
1149
+ dtype: Any = None,
1150
+ out: None = None,
1151
+ ) -> generic: ...
1152
+ @overload
1153
+ def trace[First: Flag[int], Second: Flag[int]](
1154
+ self,
1155
+ offset: int = 0,
1156
+ axis1: First = 0,
1157
+ axis2: Second = 1,
1158
+ dtype: Any = None,
1159
+ out: None = None,
1160
+ ) -> ndarray[diagonal_shape(Shape, First, Second, False, 0)]: ...
1161
+ @overload
1162
+ def trace[Out: ndarray](
1163
+ self,
1164
+ offset: int = 0,
1165
+ axis1: int = 0,
1166
+ axis2: int = 1,
1167
+ dtype: Any = None,
1168
+ *,
1169
+ out: Out,
1170
+ ) -> Out: ...
1171
+ # TODO(stroxler): Validate 2-D axes and infer scalar dtype promotion.
1172
+ @overload
1173
+ def transpose(
1174
+ self, axes: None = None, /
1175
+ ) -> ndarray[reverse_shape(Shape), DType]: ...
1176
+ @overload
1177
+ def transpose[Axes: IntTuple](
1178
+ self, axes: Axes, /
1179
+ ) -> ndarray[transpose_shape(Shape, Axes), DType]: ...
1180
+ @overload
1181
+ def transpose[Axes: IntTuple](
1182
+ self, *axes: *Axes
1183
+ ) -> ndarray[transpose_shape(Shape, Axes), DType]: ...
1184
+ # TODO(stroxler): Reinterpretation can change the final dimension's size.
1185
+ @overload
1186
+ def view(self, /) -> ndarray[Shape, DType]: ...
1187
+ @overload
1188
+ def view[ArrayT: ndarray](self, /, *, type: type[ArrayT]) -> ArrayT: ...
1189
+ @overload
1190
+ def view[ScalarT: generic](
1191
+ self, dtype: type[ScalarT] | _dtype[ScalarT], /
1192
+ ) -> ndarray[IntTuple, _dtype[ScalarT]]: ...
1193
+ @overload
1194
+ def view(self, dtype: str, /) -> ndarray[IntTuple]: ...
930
1195
 
931
1196
  class ufunc:
932
1197
  __name__: str
@@ -947,8 +1212,8 @@ class ufunc:
947
1212
  def at(self, a: ndarray, indices: Any, b: Any = None, /) -> None: ...
948
1213
 
949
1214
  permute_dims = transpose
950
- # TODO(stroxler): Make this precise when `concatenate` accepts overlay ndarrays.
951
- concat: Any = concatenate
1215
+ # TODO(stroxler): Model concatenation of overlay ndarrays with a shape rule.
1216
+ concat = concatenate
952
1217
 
953
1218
  @overload
954
1219
  def stack[Shapes: IntTuples, Axis: Flag[int]](
@@ -1245,9 +1510,9 @@ def array[Shape: _Shape, DType](
1245
1510
  object: ndarray[Shape, DType],
1246
1511
  dtype: None = None,
1247
1512
  *,
1248
- copy: bool | None = ...,
1513
+ copy: py_bool | None = ...,
1249
1514
  order: str | None = None,
1250
- subok: bool = ...,
1515
+ subok: py_bool = ...,
1251
1516
  ndmin: Literal[0] = 0,
1252
1517
  like: None = None,
1253
1518
  ) -> ndarray[Shape, DType]: ...
@@ -1256,9 +1521,9 @@ def array[Shape: _Shape](
1256
1521
  object: ndarray[Shape, Any],
1257
1522
  dtype: Any,
1258
1523
  *,
1259
- copy: bool | None = ...,
1524
+ copy: py_bool | None = ...,
1260
1525
  order: str | None = None,
1261
- subok: bool = ...,
1526
+ subok: py_bool = ...,
1262
1527
  ndmin: Literal[0] = 0,
1263
1528
  like: None = None,
1264
1529
  ) -> ndarray[Shape]: ...
@@ -1267,9 +1532,9 @@ def array[Shape: _Shape = []](
1267
1532
  object: _ArrayScalar | RegularNestedList[Shape, _ArrayScalar],
1268
1533
  dtype: Any = ...,
1269
1534
  *,
1270
- copy: bool | None = ...,
1535
+ copy: py_bool | None = ...,
1271
1536
  order: str | None = None,
1272
- subok: bool = ...,
1537
+ subok: py_bool = ...,
1273
1538
  ndmin: Literal[0] = 0,
1274
1539
  like: None = None,
1275
1540
  ) -> ndarray[Shape]: ...
@@ -1278,9 +1543,9 @@ def array(
1278
1543
  object: Any,
1279
1544
  dtype: Any = ...,
1280
1545
  *,
1281
- copy: bool | None = ...,
1546
+ copy: py_bool | None = ...,
1282
1547
  order: str | None = None,
1283
- subok: bool = ...,
1548
+ subok: py_bool = ...,
1284
1549
  ndmin: int = ...,
1285
1550
  like: None = None,
1286
1551
  ) -> ndarray[IntTuple]: ...
@@ -1289,9 +1554,9 @@ def array(
1289
1554
  object: Any,
1290
1555
  dtype: Any = ...,
1291
1556
  *,
1292
- copy: bool | None = ...,
1557
+ copy: py_bool | None = ...,
1293
1558
  order: str | None = None,
1294
- subok: bool = ...,
1559
+ subok: py_bool = ...,
1295
1560
  ndmin: int = ...,
1296
1561
  like: Any,
1297
1562
  ) -> Any: ...
@@ -1302,7 +1567,7 @@ def asarray[Shape: _Shape, DType](
1302
1567
  order: str | None = None,
1303
1568
  *,
1304
1569
  device: Any = ...,
1305
- copy: bool | None = ...,
1570
+ copy: py_bool | None = ...,
1306
1571
  like: None = None,
1307
1572
  ) -> ndarray[Shape, DType]: ...
1308
1573
  @overload
@@ -1312,7 +1577,7 @@ def asarray[Shape: _Shape](
1312
1577
  order: str | None = None,
1313
1578
  *,
1314
1579
  device: Any = ...,
1315
- copy: bool | None = ...,
1580
+ copy: py_bool | None = ...,
1316
1581
  like: None = None,
1317
1582
  ) -> ndarray[Shape]: ...
1318
1583
  @overload
@@ -1322,7 +1587,7 @@ def asarray[Shape: _Shape = []](
1322
1587
  order: str | None = None,
1323
1588
  *,
1324
1589
  device: Any = ...,
1325
- copy: bool | None = ...,
1590
+ copy: py_bool | None = ...,
1326
1591
  like: None = None,
1327
1592
  ) -> ndarray[Shape]: ...
1328
1593
  @overload
@@ -1332,7 +1597,7 @@ def asarray(
1332
1597
  order: str | None = None,
1333
1598
  *,
1334
1599
  device: Any = ...,
1335
- copy: bool | None = ...,
1600
+ copy: py_bool | None = ...,
1336
1601
  like: None = None,
1337
1602
  ) -> ndarray[IntTuple]: ...
1338
1603
  @overload
@@ -1342,7 +1607,7 @@ def asarray(
1342
1607
  order: str | None = None,
1343
1608
  *,
1344
1609
  device: Any = ...,
1345
- copy: bool | None = ...,
1610
+ copy: py_bool | None = ...,
1346
1611
  like: Any,
1347
1612
  ) -> Any: ...
1348
1613
  def round[Shape: _Shape](x: ndarray[Shape]) -> ndarray[Shape]: ...
@@ -1358,20 +1623,19 @@ def fill_diagonal[N: IntVar, DType](
1358
1623
  wrap: py_bool = False,
1359
1624
  ) -> None: ...
1360
1625
  @overload
1361
- def diag[N: IntVar, DType, K: Flag[int] = 0](
1626
+ def diag[N: IntVar, DType, K: Int = 0](
1362
1627
  v: ndarray[[N], DType], k: K = 0
1363
1628
  ) -> ndarray[[diag_extent(Int[N], K), diag_extent(Int[N], K)], DType]: ...
1364
-
1365
- # TODO(stroxler): Model the shape arithmetic here; we can do better than `int`.
1366
1629
  @overload
1367
- def diag[M: IntVar, N: IntVar, DType](
1368
- v: ndarray[[M, N], DType], k: int = 0
1369
- ) -> ndarray[[int], DType]: ...
1630
+ def diag[M: IntVar, N: IntVar, DType, K: Flag[int] = 0](
1631
+ v: ndarray[[M, N], DType], k: K = 0
1632
+ ) -> ndarray[diag_matrix_shape(Int[M], Int[N], K), DType]: ...
1370
1633
 
1371
1634
  # Trailing fallback for ranks the precise overloads do not model, so their dtype survives
1372
1635
  # instead of degrading to `Any`. The parameter shape is a type variable rather than
1373
1636
  # `IntTuple`: a gradual parameter shape would also match known-rank arguments whose dtype is
1374
1637
  # gradual, and that ambiguity collapses their precise result to a gradual shape.
1638
+ # TODO(stroxler): Reject known-invalid ranks without losing unknown-rank dtype information.
1375
1639
  @overload
1376
1640
  def diag[S: _Shape, DType](
1377
1641
  v: ndarray[S, DType], k: int = 0
@@ -1609,51 +1873,34 @@ def identity[N: IntVar](
1609
1873
  n: Int[N], dtype: Any, *, like: Any = ...
1610
1874
  ) -> ndarray[[N, N]]: ...
1611
1875
 
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): ...
1876
+ bool = bool_
1877
+
1878
+ class broadcast:
1879
+ def __new__(cls, *args: ArrayLike) -> broadcast: ...
1880
+ def __init__(self, *args: ArrayLike) -> None: ...
1881
+ index: int
1882
+ iters: tuple[flatiter[Any], ...]
1883
+ nd: int
1884
+ ndim: int
1885
+ numiter: int
1886
+ shape: tuple[int, ...]
1887
+ size: int
1888
+ def __iter__(self) -> broadcast: ...
1889
+ def __next__(self) -> tuple[Any, ...]: ...
1890
+ def reset(self) -> None: ...
1617
1891
 
1618
- bool: Any
1619
- broadcast: Any
1620
- byte: Any
1621
- bytes_: Any
1622
- cdouble: Any
1623
- character: Any
1624
- clongdouble: Any
1625
- complex128: Any
1626
- complex64: Any
1627
- complexfloating: Any
1628
- csingle: Any
1629
- datetime64: Any
1630
- double: Any
1631
- flexible: Any
1632
- float16: Any
1633
- half: Any
1634
- int16: Any
1635
- int8: Any
1636
- int_: Any
1637
- intc: Any
1638
- long: Any
1639
- longdouble: Any
1640
- longlong: Any
1641
- object_: Any
1642
- short: Any
1643
- signedinteger: Any
1644
- single: Any
1645
- str_: Any
1646
- timedelta64: Any
1647
- ubyte: Any
1648
- uint: Any
1649
- uint16: Any
1650
- uint32: Any
1651
- uint64: Any
1652
- uint8: Any
1653
- uintc: Any
1654
- uintp: Any
1655
- ulong: Any
1656
- ulonglong: Any
1657
- unsignedinteger: Any
1658
- ushort: Any
1659
- void: Any
1892
+ byte = int8
1893
+ cdouble = complex128
1894
+ csingle = complex64
1895
+ double = float64
1896
+ half = float16
1897
+ int_ = intp
1898
+ intc = int32
1899
+ long = int64
1900
+ short = int16
1901
+ single = float32
1902
+ ubyte = uint8
1903
+ uint = uintp
1904
+ uintc = uint32
1905
+ ulong = uint64
1906
+ ushort = uint16
numpy-stubs/_shapes.pyi CHANGED
@@ -23,11 +23,178 @@ def int_min(a: Int, b: Int) -> Int:
23
23
  return dsl.Int.gradual()
24
24
 
25
25
  @type_shape_dsl_function
26
- def diag_extent(n: Int, k: int) -> Int:
27
- # Non-literal Flag arguments become gradual before the DSL body is evaluated.
28
- if k < 0:
29
- return n - k
30
- return n + k
26
+ def diag_extent(n: Int, k: Int) -> Int:
27
+ return n + abs(k)
28
+
29
+ @type_shape_dsl_function
30
+ def repeat_shape(shape: IntTuple, repeats: Int, axis: int | None) -> IntTuple:
31
+ if dsl.is_concrete_int(repeats) and repeats < 0:
32
+ return dsl.Invalid("repeats may not contain negative values")
33
+ if axis is None:
34
+ return dsl.IntTuple((dsl.prod(shape) * repeats,))
35
+ if dsl.is_int_value(axis):
36
+ if len(shape) == 0:
37
+ if axis == 0 or axis == -1:
38
+ return dsl.IntTuple((repeats,))
39
+ return dsl.Invalid("axis is out of bounds")
40
+ rank = len(shape)
41
+ if axis < 0 - rank or axis >= rank:
42
+ return dsl.Invalid("axis is out of bounds")
43
+ if axis < 0:
44
+ normalized_axis = axis + rank
45
+ else:
46
+ normalized_axis = axis + 0
47
+ return dsl.IntTuple(
48
+ (
49
+ shape[index] * repeats if index == normalized_axis else shape[index]
50
+ for index in range(rank)
51
+ )
52
+ )
53
+ return dsl.Invalid("axis must be an integer or None")
54
+
55
+ @type_shape_dsl_function
56
+ def repeat_sequence_shape(
57
+ shape: IntTuple, repeats: IntTuple, axis: int | None
58
+ ) -> IntTuple:
59
+ if axis is None:
60
+ source = dsl.IntTuple((dsl.prod(shape),))
61
+ normalized_axis = 0
62
+ elif dsl.is_int_value(axis):
63
+ if len(shape) == 0:
64
+ if axis != 0 and axis != -1:
65
+ return dsl.Invalid("axis is out of bounds")
66
+ source = dsl.IntTuple((1,))
67
+ else:
68
+ source = shape
69
+ rank = len(source)
70
+ if axis < 0 - rank or axis >= rank:
71
+ return dsl.Invalid("axis is out of bounds")
72
+ if axis < 0:
73
+ normalized_axis = axis + rank
74
+ else:
75
+ normalized_axis = axis + 0
76
+ else:
77
+ return dsl.Invalid("axis must be an integer or None")
78
+ extent = source[normalized_axis]
79
+ if any(dsl.is_concrete_int(value) and value < 0 for value in repeats):
80
+ return dsl.Invalid("repeats may not contain negative values")
81
+ if len(repeats) == 1:
82
+ repeated = extent * repeats[0]
83
+ else:
84
+ if dsl.is_concrete_int(extent) and len(repeats) != extent:
85
+ return dsl.Invalid("repeats must match the selected axis")
86
+ repeated = dsl.sum(repeats)
87
+ return dsl.IntTuple(
88
+ (
89
+ repeated if index == normalized_axis else source[index]
90
+ for index in range(len(source))
91
+ )
92
+ )
93
+
94
+ @type_shape_dsl_function
95
+ def take_shape(shape: IntTuple, indices: IntTuple, axis: int | None) -> IntTuple:
96
+ if axis is None:
97
+ return indices
98
+ if dsl.is_int_value(axis):
99
+ rank = len(shape)
100
+ if axis < 0 - rank or axis >= rank:
101
+ return dsl.Invalid("axis out of bounds")
102
+ if axis < 0:
103
+ normalized_axis = axis + rank
104
+ else:
105
+ normalized_axis = axis + 0
106
+ return dsl.concat(
107
+ dsl.concat(shape[:normalized_axis], indices),
108
+ shape[normalized_axis + 1 :],
109
+ )
110
+ return dsl.Invalid("axis must be an integer or None")
111
+
112
+ @type_shape_dsl_function
113
+ def compress_shape(shape: IntTuple, axis: int | None) -> IntTuple:
114
+ indices = dsl.IntTuple((dsl.Int.gradual(),))
115
+ if axis is None:
116
+ return indices
117
+ if dsl.is_int_value(axis):
118
+ if len(shape) == 0 and (axis == 0 or axis == -1):
119
+ return indices
120
+ return take_shape(shape, indices, axis)
121
+
122
+ @type_shape_dsl_function
123
+ def diagonal_shape(
124
+ shape: IntTuple, axis1: int, axis2: int, keep_diagonal: bool, offset: int
125
+ ) -> IntTuple:
126
+ if len(shape) < 2:
127
+ return dsl.Invalid("diagonal requires at least two dimensions")
128
+ if (
129
+ axis1 < 0 - len(shape)
130
+ or axis1 >= len(shape)
131
+ or axis2 < 0 - len(shape)
132
+ or axis2 >= len(shape)
133
+ ):
134
+ return dsl.Invalid("diagonal axis out of bounds")
135
+ if axis1 < 0:
136
+ first = axis1 + len(shape)
137
+ else:
138
+ first = axis1 + 0
139
+ if axis2 < 0:
140
+ second = axis2 + len(shape)
141
+ else:
142
+ second = axis2 + 0
143
+ if first == second:
144
+ return dsl.Invalid("diagonal axes must be distinct")
145
+ outer = dsl.IntTuple(
146
+ (
147
+ shape[index]
148
+ for index in range(len(shape))
149
+ if index != first and index != second
150
+ )
151
+ )
152
+ if not keep_diagonal:
153
+ return outer
154
+
155
+ first_extent = shape[first]
156
+ second_extent = shape[second]
157
+ zero_tuple = dsl.IntTuple((0,))
158
+ zero = zero_tuple[0]
159
+ offset_tuple = dsl.IntTuple((offset + 0,))
160
+ offset_extent = offset_tuple[0]
161
+ if offset == 0:
162
+ if first_extent == second_extent:
163
+ return dsl.concat(outer, dsl.IntTuple((first_extent,)))
164
+ if dsl.is_concrete_int(first_extent) and dsl.is_concrete_int(second_extent):
165
+ if first_extent < second_extent:
166
+ return dsl.concat(outer, dsl.IntTuple((first_extent,)))
167
+ return dsl.concat(outer, dsl.IntTuple((second_extent,)))
168
+ return dsl.concat(outer, dsl.IntTuple((dsl.Int.gradual(),)))
169
+ if offset > 0:
170
+ limit = second_extent - offset_extent
171
+ if first_extent == limit:
172
+ return dsl.concat(outer, dsl.IntTuple((first_extent,)))
173
+ if dsl.is_concrete_int(first_extent) and dsl.is_concrete_int(limit):
174
+ if limit < zero:
175
+ return dsl.concat(outer, dsl.IntTuple((zero,)))
176
+ if first_extent < limit:
177
+ return dsl.concat(outer, dsl.IntTuple((first_extent,)))
178
+ return dsl.concat(outer, dsl.IntTuple((limit,)))
179
+ return dsl.concat(outer, dsl.IntTuple((dsl.Int.gradual(),)))
180
+ limit = first_extent + offset_extent
181
+ if limit == second_extent:
182
+ return dsl.concat(outer, dsl.IntTuple((second_extent,)))
183
+ if dsl.is_concrete_int(limit) and dsl.is_concrete_int(second_extent):
184
+ if limit < zero:
185
+ return dsl.concat(outer, dsl.IntTuple((zero,)))
186
+ if limit < second_extent:
187
+ return dsl.concat(outer, dsl.IntTuple((limit,)))
188
+ return dsl.concat(outer, dsl.IntTuple((second_extent,)))
189
+ return dsl.concat(outer, dsl.IntTuple((dsl.Int.gradual(),)))
190
+
191
+ @type_shape_dsl_function
192
+ def diag_matrix_shape(m: Int, n: Int, k: int) -> IntTuple:
193
+ shape = dsl.IntTuple((m, n))
194
+ axis1 = 0
195
+ axis2 = 1
196
+ keep_diagonal = True
197
+ return diagonal_shape(shape, axis1, axis2, keep_diagonal, k)
31
198
 
32
199
  @type_shape_dsl_function
33
200
  def matmul_shape(left: IntTuple, right: IntTuple) -> IntTuple:
@@ -43,6 +210,20 @@ def matmul_shape(left: IntTuple, right: IntTuple) -> IntTuple:
43
210
  spec = "(m,n),(n,p)->(m,p)"
44
211
  return gufunc_broadcast(spec, operands)
45
212
 
213
+ @type_shape_dsl_function
214
+ def dot_shape(left: IntTuple, right: IntTuple) -> IntTuple:
215
+ if len(left) == 0:
216
+ return right
217
+ if len(right) == 0:
218
+ return left
219
+ if len(right) == 1:
220
+ if left[-1] != right[0]:
221
+ return dsl.Invalid("dot contraction dimensions must agree")
222
+ return left[:-1]
223
+ if left[-1] != right[-2]:
224
+ return dsl.Invalid("dot contraction dimensions must agree")
225
+ return dsl.concat(dsl.concat(left[:-1], right[:-2]), right[-1:])
226
+
46
227
  @type_shape_dsl_function
47
228
  def matvec_shape(left: IntTuple, right: IntTuple) -> IntTuple:
48
229
  spec = "(m,n),(n)->(m)"
@@ -67,25 +248,7 @@ def reduce_shape(
67
248
  axis: int | tuple[int, ...] | None,
68
249
  keepdims: bool,
69
250
  ) -> IntTuple:
70
- if axis is None:
71
- axes = range(len(shape))
72
- elif dsl.is_int_value(axis):
73
- axes = (axis,)
74
- else:
75
- axes = axis
76
- # The DSL does not support unary negation of a Flag integer.
77
- if any(item < 0 - len(shape) or item >= len(shape) for item in axes):
78
- return dsl.Invalid("axis out of bounds")
79
- normalized = tuple(item + len(shape) if item < 0 else item for item in axes)
80
- if any(normalized.count(item) > 1 for item in normalized):
81
- return dsl.Invalid("duplicate axis")
82
- if keepdims:
83
- return dsl.IntTuple(
84
- (1 if index in normalized else shape[index] for index in range(len(shape)))
85
- )
86
- return dsl.IntTuple(
87
- (shape[index] for index in range(len(shape)) if index not in normalized)
88
- )
251
+ return dsl.axis_reduce(shape, axis, keepdims, False, True, False)
89
252
 
90
253
  @type_shape_dsl_function
91
254
  def stack_shape(shapes: IntTuples, axis: int) -> IntTuple:
@@ -142,3 +305,138 @@ def expand_dims_shape(shape: IntTuple, axis: int) -> IntTuple:
142
305
  for index in range(output_rank)
143
306
  )
144
307
  )
308
+
309
+ @type_shape_dsl_function
310
+ def swapaxes_shape(shape: IntTuple, axis1: int, axis2: int) -> IntTuple:
311
+ rank = len(shape)
312
+ if axis1 < 0 - rank or axis1 >= rank or axis2 < 0 - rank or axis2 >= rank:
313
+ return dsl.Invalid("swapaxes axis out of bounds")
314
+ if axis1 < 0:
315
+ first = axis1 + rank
316
+ else:
317
+ first = axis1 + 0
318
+ if axis2 < 0:
319
+ second = axis2 + rank
320
+ else:
321
+ second = axis2 + 0
322
+ return dsl.IntTuple(
323
+ (
324
+ shape[second]
325
+ if index == first
326
+ else shape[first]
327
+ if index == second
328
+ else shape[index]
329
+ for index in range(rank)
330
+ )
331
+ )
332
+
333
+ @type_shape_dsl_function
334
+ def reverse_shape(shape: IntTuple) -> IntTuple:
335
+ rank = len(shape)
336
+ return dsl.IntTuple((shape[rank - index - 1] for index in range(rank)))
337
+
338
+ @type_shape_dsl_function
339
+ def transpose_shape(shape: IntTuple, axes: IntTuple) -> IntTuple:
340
+ if len(axes) != len(shape):
341
+ return dsl.Invalid("transpose axes must match the array rank")
342
+ if any(axis < 0 - len(shape) or axis >= len(shape) for axis in axes):
343
+ return dsl.Invalid("transpose axis out of bounds")
344
+ normalized = dsl.IntTuple(
345
+ (axis + len(shape) if axis < 0 else axis for axis in axes)
346
+ )
347
+ duplicates = dsl.IntTuple(
348
+ (
349
+ 1
350
+ if any(normalized[index] == normalized[other] for other in range(index))
351
+ else 0
352
+ for index in range(len(shape))
353
+ )
354
+ )
355
+ if any(duplicate == 1 for duplicate in duplicates):
356
+ return dsl.Invalid("transpose axes must be unique")
357
+ return dsl.IntTuple((shape[axis] for axis in normalized))
358
+
359
+ @type_shape_dsl_function
360
+ def nonzero_shapes(shape: IntTuple) -> IntTuples:
361
+ if len(shape) == 0:
362
+ return dsl.Invalid("nonzero requires at least one dimension")
363
+ return dsl.IntTuples(
364
+ (dsl.IntTuple((dsl.Int.gradual(),)) for _ in range(len(shape)))
365
+ )
366
+
367
+ @type_shape_dsl_function
368
+ def squeeze_shape(shape: IntTuple, axis: int | tuple[int, ...] | None) -> IntTuple:
369
+ if axis is None:
370
+ return dsl.IntTuple((extent for extent in shape if extent != 1))
371
+ if dsl.is_int_value(axis):
372
+ if axis == -2:
373
+ tail = shape[-2:]
374
+ if len(tail) < 2:
375
+ return dsl.Invalid("squeeze axis out of bounds")
376
+ extent = shape[-2]
377
+ if extent == 1:
378
+ return dsl.concat(shape[:-2], shape[-1:])
379
+ if dsl.is_concrete_int(extent):
380
+ return dsl.Invalid("squeeze axis must have length 1")
381
+ if axis == 0 or axis == -1:
382
+ if len(shape) == 0:
383
+ return shape
384
+ axes = (axis,)
385
+ else:
386
+ axes = axis
387
+ if any(item < 0 - len(shape) or item >= len(shape) for item in axes):
388
+ return dsl.Invalid("squeeze axis out of bounds")
389
+ normalized = tuple(item + len(shape) if item < 0 else item for item in axes)
390
+ if any(normalized.count(item) > 1 for item in normalized):
391
+ return dsl.Invalid("squeeze axes must be unique")
392
+ if any(shape[item] != 1 for item in normalized):
393
+ return dsl.Invalid("squeeze axis must have length 1")
394
+ return dsl.IntTuple(
395
+ (shape[index] for index in range(len(shape)) if index not in normalized)
396
+ )
397
+
398
+ @type_shape_dsl_function
399
+ def reshape_shape(shape: IntTuple, target: IntTuple) -> IntTuple:
400
+ inferred = tuple(
401
+ (
402
+ 1
403
+ for dimension in target
404
+ if dsl.is_concrete_int(dimension) and dimension == -1
405
+ )
406
+ )
407
+ if len(inferred) > 1:
408
+ return dsl.Invalid("reshape allows only one inferred dimension")
409
+ if any(dsl.is_concrete_int(dimension) and dimension < -1 for dimension in target):
410
+ return dsl.Invalid("reshape dimensions must be at least -1")
411
+ known_shape = dsl.IntTuple(
412
+ (
413
+ dimension
414
+ for dimension in target
415
+ if not (dsl.is_concrete_int(dimension) and dimension == -1)
416
+ )
417
+ )
418
+ known = dsl.prod(known_shape)
419
+ total = dsl.prod(shape)
420
+ if len(inferred) == 0:
421
+ if dsl.is_concrete_int(total) and dsl.is_concrete_int(known) and total != known:
422
+ return dsl.Invalid("reshape target element count does not match the input")
423
+ return target
424
+ if dsl.is_concrete_int(known):
425
+ if known == 0:
426
+ return dsl.Invalid("reshape cannot infer a dimension from zero elements")
427
+ if dsl.is_concrete_int(total) and total % known != 0:
428
+ return dsl.Invalid("reshape cannot infer an integral dimension")
429
+ return dsl.IntTuple(
430
+ (
431
+ total // known
432
+ if dsl.is_concrete_int(dimension) and dimension == -1
433
+ else dimension
434
+ for dimension in target
435
+ )
436
+ )
437
+
438
+ @type_shape_dsl_function
439
+ def reshape_varargs_shape(shape: IntTuple, target: IntTuple) -> IntTuple:
440
+ if len(target) == 0:
441
+ return dsl.Invalid("reshape expects at least one dimension")
442
+ return reshape_shape(shape, target)
@@ -3,9 +3,10 @@
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
6
7
  from typing import Any, Literal, overload
7
8
 
8
- from numpy._shapes import int_min
9
+ from numpy._shapes import int_min, reduce_shape
9
10
 
10
11
  # Preserve NumPy's canonical re-exports before local shape-aware declarations.
11
12
  from numpy.linalg._linalg import (
@@ -37,7 +38,7 @@ from numpy.linalg._linalg import (
37
38
  vecdot as vecdot,
38
39
  vector_norm as vector_norm,
39
40
  )
40
- from shape_extensions import Int, IntVar
41
+ from shape_extensions import Flag, Int, IntTuple, IntVar
41
42
 
42
43
  from .. import floating, ndarray
43
44
 
@@ -78,13 +79,19 @@ def norm(
78
79
  **kwargs: Any,
79
80
  ) -> floating: ...
80
81
  @overload
82
+ def norm[Shape: IntTuple, Axis: Flag[int | tuple[int, ...] | None], Keep: Flag[bool]](
83
+ x: ndarray[Shape],
84
+ ord: int | float | Literal["fro", "nuc"] | None = None,
85
+ axis: Axis = None,
86
+ keepdims: Keep = False,
87
+ ) -> ndarray[reduce_shape(Shape, Axis, Keep)]: ...
88
+ @overload
81
89
  def norm(
82
- x: Any,
83
- ord: Any = None,
84
- axis: Any = None,
85
- keepdims: bool = False,
86
- **kwargs: Any,
87
- ) -> Any: ...
90
+ x: Sequence[int | float | complex],
91
+ ord: int | float | None = None,
92
+ axis: None = None,
93
+ keepdims: Literal[False] = False,
94
+ ) -> floating: ...
88
95
  def eigh[N: IntVar, DType](
89
96
  a: ndarray[[N, N], DType],
90
97
  ) -> tuple[ndarray[[N], DType], ndarray[[N, N], DType]]: ...
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pyrefly-numpy-stubs
3
- Version: 1.4.0.dev2
3
+ Version: 1.4.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,8 @@ 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.4.0.dev2
36
+ Requires-Dist: numpy
37
+ Requires-Dist: pyrefly-shape-extensions==1.4.0.dev4
37
38
  Description-Content-Type: text/markdown
38
39
 
39
40
  # pyrefly-numpy-stubs
@@ -45,7 +46,9 @@ This package is a PEP 561 stub-only distribution. It installs the
45
46
  runtime `numpy` package without replacing or shadowing NumPy itself.
46
47
 
47
48
  The package is versioned in lockstep with Pyrefly and depends on the matching
48
- `pyrefly-shape-extensions` package.
49
+ `pyrefly-shape-extensions` package. Installing it also installs `numpy`, which
50
+ provides upstream modules used by these partial stubs. The `numpy` dependency is
51
+ unpinned, so projects can choose their own NumPy version.
49
52
 
50
53
  Static checks use NumPy's installed stubs for APIs that this package re-exports:
51
54
 
@@ -0,0 +1,9 @@
1
+ numpy-stubs/__init__.pyi,sha256=O672-YTGCq-_op8giQBoGwBGCZXftLuMHeUjW_Lr-mw,58059
2
+ numpy-stubs/_shapes.pyi,sha256=2dORqXAzegcAD7kVRY95xB2yLqHShyTCzNeboeEjb18,15725
3
+ numpy-stubs/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
4
+ numpy-stubs/linalg/__init__.pyi,sha256=GAkX_RbUy4GQBjPxlI0HlqbVFdHH2q2P1bkvsqV9Qh0,3123
5
+ numpy-stubs/random/__init__.pyi,sha256=rZT47dwZw1ev6xXQsngIZQL8SnkI2vEUBbK2p-vSi7w,2404
6
+ pyrefly_numpy_stubs-1.4.0.dev4.dist-info/METADATA,sha256=YUR2cMuu_dTWHn_qpN59fs4DmGYxebGHjht6V6oX8l0,2865
7
+ pyrefly_numpy_stubs-1.4.0.dev4.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
8
+ pyrefly_numpy_stubs-1.4.0.dev4.dist-info/licenses/LICENSE,sha256=2m03A-0Ry-Qr0hLHJZV8mNojy_8ZmMBfpLPZdtGljpM,1088
9
+ pyrefly_numpy_stubs-1.4.0.dev4.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- numpy-stubs/__init__.pyi,sha256=hQWmGEAgvHTVwqs_-YIoSFyB6wGhvHWUoJWe07YE-tA,48601
2
- numpy-stubs/_shapes.pyi,sha256=kvbytJygme4DluXr_v2iO_kAnWrZaRTUCb0MNgi-6Oo,4802
3
- numpy-stubs/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
4
- numpy-stubs/linalg/__init__.pyi,sha256=HqAg4mSr0csSnsIRougqxHP7dzUww7wa6USyN51PPAQ,2734
5
- numpy-stubs/random/__init__.pyi,sha256=rZT47dwZw1ev6xXQsngIZQL8SnkI2vEUBbK2p-vSi7w,2404
6
- pyrefly_numpy_stubs-1.4.0.dev2.dist-info/METADATA,sha256=12Q53aX_k9HEN2PsBsHq1tXxHSTuzQtbF_p908sVC9U,2662
7
- pyrefly_numpy_stubs-1.4.0.dev2.dist-info/WHEEL,sha256=W3fkpkm7-wf9vBI5Z-7s0eWkeM-spu78I8Neb98DeEg,87
8
- pyrefly_numpy_stubs-1.4.0.dev2.dist-info/licenses/LICENSE,sha256=2m03A-0Ry-Qr0hLHJZV8mNojy_8ZmMBfpLPZdtGljpM,1088
9
- pyrefly_numpy_stubs-1.4.0.dev2.dist-info/RECORD,,