pyrefly-numpy-stubs 1.3.0__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.
@@ -0,0 +1,125 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ import shape_extensions.dsl as dsl
7
+ from shape_extensions import (
8
+ gufunc_broadcast,
9
+ Int,
10
+ IntTuple,
11
+ IntTuples,
12
+ type_shape_dsl_function,
13
+ )
14
+
15
+ @type_shape_dsl_function
16
+ def int_min(a: Int, b: Int) -> Int:
17
+ if a == b:
18
+ return a
19
+ if dsl.is_concrete_int(a) and dsl.is_concrete_int(b):
20
+ if a < b:
21
+ return a
22
+ return b
23
+ return dsl.Int.gradual()
24
+
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
31
+
32
+ @type_shape_dsl_function
33
+ def matmul_shape(left: IntTuple, right: IntTuple) -> IntTuple:
34
+ if len(left) == 0 or len(right) == 0:
35
+ return dsl.Invalid("matmul expects at least 1-D arrays")
36
+ operands = dsl.IntTuples((left, right))
37
+ if len(right) == 1:
38
+ spec = "(n),(n)->()"
39
+ return gufunc_broadcast(spec, operands)
40
+ if len(left) == 1:
41
+ spec = "(n),(n,p)->(p)"
42
+ return gufunc_broadcast(spec, operands)
43
+ spec = "(m,n),(n,p)->(m,p)"
44
+ return gufunc_broadcast(spec, operands)
45
+
46
+ @type_shape_dsl_function
47
+ def matvec_shape(left: IntTuple, right: IntTuple) -> IntTuple:
48
+ spec = "(m,n),(n)->(m)"
49
+ operands = dsl.IntTuples((left, right))
50
+ return gufunc_broadcast(spec, operands)
51
+
52
+ @type_shape_dsl_function
53
+ def vecdot_shape(left: IntTuple, right: IntTuple) -> IntTuple:
54
+ spec = "(n),(n)->()"
55
+ operands = dsl.IntTuples((left, right))
56
+ return gufunc_broadcast(spec, operands)
57
+
58
+ @type_shape_dsl_function
59
+ def vecmat_shape(left: IntTuple, right: IntTuple) -> IntTuple:
60
+ spec = "(n),(n,m)->(m)"
61
+ operands = dsl.IntTuples((left, right))
62
+ return gufunc_broadcast(spec, operands)
63
+
64
+ @type_shape_dsl_function
65
+ def reduce_shape(
66
+ shape: IntTuple,
67
+ axis: int | tuple[int, ...] | None,
68
+ keepdims: bool,
69
+ ) -> 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
+ )
89
+
90
+ @type_shape_dsl_function
91
+ def stack_shape(shapes: IntTuples, axis: int) -> IntTuple:
92
+ if len(shapes) == 0:
93
+ return dsl.Invalid("stack expects a non-empty sequence of arrays")
94
+ first = shapes[0]
95
+ output_rank = len(first) + 1
96
+ # Unary minus is not supported by the type-level DSL.
97
+ if axis < 0 - output_rank or axis >= output_rank:
98
+ return dsl.Invalid("stack axis out of range")
99
+ if axis < 0:
100
+ norm_axis = axis + output_rank
101
+ else:
102
+ norm_axis = axis + 0
103
+ rank_mismatches = dsl.IntTuple(
104
+ (0 if len(shape) == len(first) else 1 for shape in shapes)
105
+ )
106
+ if any(mismatch == 1 for mismatch in rank_mismatches):
107
+ return dsl.Invalid("stack expects all arrays to have the same rank")
108
+ # Equal ranks are established above, so indexing every member by an index of
109
+ # `first` is in bounds.
110
+ mismatches = dsl.IntTuple(
111
+ (
112
+ 1 if any(shape[index] != first[index] for index in range(len(first))) else 0
113
+ for shape in shapes
114
+ )
115
+ )
116
+ if any(mismatch == 1 for mismatch in mismatches):
117
+ return dsl.Invalid("stack expects all arrays to have the same shape")
118
+ return dsl.IntTuple(
119
+ (
120
+ len(shapes)
121
+ if index == norm_axis
122
+ else first[index if index < norm_axis else index - 1]
123
+ for index in range(output_rank)
124
+ )
125
+ )
@@ -0,0 +1,94 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ from typing import Any, Literal, overload
7
+
8
+ from numpy._shapes import int_min
9
+
10
+ # Preserve NumPy's canonical re-exports before local shape-aware declarations.
11
+ from numpy.linalg._linalg import (
12
+ cholesky as cholesky,
13
+ cond as cond,
14
+ cross as cross,
15
+ det as det,
16
+ diagonal as diagonal,
17
+ eig as eig,
18
+ eigvals as eigvals,
19
+ eigvalsh as eigvalsh,
20
+ inv as inv,
21
+ lstsq as lstsq,
22
+ matmul as matmul,
23
+ matrix_norm as matrix_norm,
24
+ matrix_power as matrix_power,
25
+ matrix_rank as matrix_rank,
26
+ matrix_transpose as matrix_transpose,
27
+ multi_dot as multi_dot,
28
+ outer as outer,
29
+ pinv as pinv,
30
+ qr as qr,
31
+ slogdet as slogdet,
32
+ svdvals as svdvals,
33
+ tensordot as tensordot,
34
+ tensorinv as tensorinv,
35
+ tensorsolve as tensorsolve,
36
+ trace as trace,
37
+ vecdot as vecdot,
38
+ vector_norm as vector_norm,
39
+ )
40
+ from shape_extensions import Int, IntVar
41
+
42
+ from .. import ndarray
43
+
44
+ class LinAlgError(ValueError): ...
45
+
46
+ # MVP shape surface only; NumPy dtype promotion is intentionally not modeled.
47
+ @overload
48
+ def solve[N: IntVar, DType](
49
+ a: ndarray[[N, N], DType],
50
+ b: ndarray[[N]],
51
+ ) -> ndarray[[N], DType]: ...
52
+ @overload
53
+ def solve[N: IntVar, K: IntVar, DType](
54
+ a: ndarray[[N, N], DType],
55
+ b: ndarray[[N, K]],
56
+ ) -> ndarray[[N, K], DType]: ...
57
+ @overload
58
+ def norm[N: IntVar, M: IntVar, DType](
59
+ x: ndarray[[N, M, 3], DType],
60
+ ord: None,
61
+ axis: Literal[-1],
62
+ keepdims: Literal[True],
63
+ ) -> ndarray[[N, M, 1], DType]: ...
64
+ @overload
65
+ def norm[N: IntVar, M: IntVar, DType](
66
+ x: ndarray[[N, M, 3], DType],
67
+ ord: None = None,
68
+ *,
69
+ axis: Literal[-1],
70
+ keepdims: Literal[True],
71
+ ) -> ndarray[[N, M, 1], DType]: ...
72
+ @overload
73
+ def norm(
74
+ x: Any,
75
+ ord: Any = None,
76
+ axis: Any = None,
77
+ keepdims: bool = False,
78
+ **kwargs: Any,
79
+ ) -> Any: ...
80
+ def eigh[N: IntVar, DType](
81
+ a: ndarray[[N, N], DType],
82
+ ) -> tuple[ndarray[[N], DType], ndarray[[N, N], DType]]: ...
83
+ def svd[M: IntVar, N: IntVar, DType](
84
+ a: ndarray[[M, N], DType],
85
+ # NumPy defaults to full SVD; this MVP accepts only the reduced form needed
86
+ # by PCA-style demos.
87
+ full_matrices: Literal[False],
88
+ compute_uv: Literal[True] = True,
89
+ hermitian: Literal[False] = False,
90
+ ) -> tuple[
91
+ ndarray[[M, int_min(Int[M], Int[N])], DType],
92
+ ndarray[[int_min(Int[M], Int[N])], DType],
93
+ ndarray[[int_min(Int[M], Int[N]), N], DType],
94
+ ]: ...
numpy-stubs/py.typed ADDED
@@ -0,0 +1 @@
1
+ partial
@@ -0,0 +1,91 @@
1
+ # Copyright (c) Meta Platforms, Inc. and affiliates.
2
+ #
3
+ # This source code is licensed under the MIT license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ from typing import overload
7
+
8
+ # Preserve NumPy's canonical re-exports before local shape-aware declarations.
9
+ from numpy.random._generator import (
10
+ default_rng as default_rng,
11
+ Generator as Generator,
12
+ )
13
+ from numpy.random._mt19937 import (
14
+ MT19937 as MT19937,
15
+ )
16
+ from numpy.random._pcg64 import (
17
+ PCG64 as PCG64,
18
+ PCG64DXSM as PCG64DXSM,
19
+ )
20
+ from numpy.random._philox import (
21
+ Philox as Philox,
22
+ )
23
+ from numpy.random._sfc64 import (
24
+ SFC64 as SFC64,
25
+ )
26
+ from numpy.random.bit_generator import (
27
+ BitGenerator as BitGenerator,
28
+ SeedSequence as SeedSequence,
29
+ )
30
+ from numpy.random.mtrand import (
31
+ beta as beta,
32
+ binomial as binomial,
33
+ bytes as bytes,
34
+ chisquare as chisquare,
35
+ choice as choice,
36
+ dirichlet as dirichlet,
37
+ exponential as exponential,
38
+ f as f,
39
+ gamma as gamma,
40
+ geometric as geometric,
41
+ get_state as get_state,
42
+ gumbel as gumbel,
43
+ hypergeometric as hypergeometric,
44
+ laplace as laplace,
45
+ logistic as logistic,
46
+ lognormal as lognormal,
47
+ logseries as logseries,
48
+ multinomial as multinomial,
49
+ multivariate_normal as multivariate_normal,
50
+ negative_binomial as negative_binomial,
51
+ noncentral_chisquare as noncentral_chisquare,
52
+ noncentral_f as noncentral_f,
53
+ normal as normal,
54
+ pareto as pareto,
55
+ permutation as permutation,
56
+ poisson as poisson,
57
+ power as power,
58
+ rand as rand,
59
+ randint as randint,
60
+ random as random,
61
+ random_integers as random_integers,
62
+ random_sample as random_sample,
63
+ RandomState as RandomState,
64
+ ranf as ranf,
65
+ rayleigh as rayleigh,
66
+ sample as sample,
67
+ seed as seed,
68
+ set_state as set_state,
69
+ shuffle as shuffle,
70
+ standard_cauchy as standard_cauchy,
71
+ standard_exponential as standard_exponential,
72
+ standard_gamma as standard_gamma,
73
+ standard_normal as standard_normal,
74
+ standard_t as standard_t,
75
+ triangular as triangular,
76
+ uniform as uniform,
77
+ vonmises as vonmises,
78
+ wald as wald,
79
+ weibull as weibull,
80
+ zipf as zipf,
81
+ )
82
+ from shape_extensions import Int, IntVar
83
+
84
+ from .. import dtype, float64, ndarray
85
+
86
+ @overload
87
+ def randn[N: IntVar](d0: Int[N], /) -> ndarray[[N], dtype[float64]]: ...
88
+ @overload
89
+ def randn[N: IntVar, M: IntVar](
90
+ d0: Int[N], d1: Int[M], /
91
+ ) -> ndarray[[N, M], dtype[float64]]: ...
@@ -0,0 +1,58 @@
1
+ Metadata-Version: 2.5
2
+ Name: pyrefly-numpy-stubs
3
+ Version: 1.3.0
4
+ Summary: NumPy type stubs with array-shape tracking, maintained by Pyrefly
5
+ Project-URL: homepage, https://pyrefly.org
6
+ Project-URL: documentation, https://pyrefly.org/en/docs/
7
+ License: MIT License
8
+
9
+ Copyright (c) Meta Platforms, Inc. and affiliates.
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
+ SOFTWARE.
28
+ License-File: LICENSE
29
+ Keywords: numpy,stubs,typechecker,typechecking
30
+ Classifier: Development Status :: 4 - Beta
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Programming Language :: Python
33
+ Classifier: Programming Language :: Python :: 3
34
+ Classifier: Typing :: Stubs Only
35
+ Requires-Python: >=3.12
36
+ Requires-Dist: pyrefly-shape-extensions==1.3.0
37
+ Description-Content-Type: text/markdown
38
+
39
+ # pyrefly-numpy-stubs
40
+
41
+ NumPy type stubs with array shape information for Pyrefly.
42
+
43
+ This package is a PEP 561 stub-only distribution. It installs the
44
+ `numpy-stubs` stub package so Pyrefly can discover shape-aware stubs for the
45
+ runtime `numpy` package without replacing or shadowing NumPy itself.
46
+
47
+ The package is versioned in lockstep with Pyrefly and depends on the matching
48
+ `pyrefly-shape-extensions` package.
49
+
50
+ Static checks use NumPy's installed stubs for APIs that this package re-exports:
51
+
52
+ ```bash
53
+ python3 tensor-shapes/pyrefly-numpy-stubs/run_pyrefly.py
54
+ ```
55
+
56
+ The runner uses `~/.tensor-shapes-venv` by default. Set
57
+ `$TENSOR_SHAPES_VENV` to select another virtualenv, or pass `--python` with the
58
+ path to a virtualenv interpreter that has NumPy installed.
@@ -0,0 +1,9 @@
1
+ numpy-stubs/__init__.pyi,sha256=lc427b0NY_haZkzWZj7CVlaS3iLUjRSisj1cmeFYYbQ,44439
2
+ numpy-stubs/_shapes.pyi,sha256=6SWZvOMJ3L2GzcglV5b1Df4hut3XabWziTnfZZV-O0Q,4203
3
+ numpy-stubs/py.typed,sha256=la67KBlbjXN-_-DfGNcdOcjYumVpKG_Tkw-8n5dnGB4,8
4
+ numpy-stubs/linalg/__init__.pyi,sha256=a-yMKOoT-6axInoZk8HhHOqAeywcanLk0Ee5kjiGRvU,2568
5
+ numpy-stubs/random/__init__.pyi,sha256=vRqvYq0cOhRWx86DXEgGZ1fxPO4kTCl_CCx92Ud-Z14,2478
6
+ pyrefly_numpy_stubs-1.3.0.dist-info/METADATA,sha256=n2simFRmqvvTuB7I81AujQPgKKes-ZP5v3MpnPPXrnA,2652
7
+ pyrefly_numpy_stubs-1.3.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
8
+ pyrefly_numpy_stubs-1.3.0.dist-info/licenses/LICENSE,sha256=2m03A-0Ry-Qr0hLHJZV8mNojy_8ZmMBfpLPZdtGljpM,1088
9
+ pyrefly_numpy_stubs-1.3.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.32.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.