Skip to content

Commit 96af5a7

Browse files
stroxlermeta-codesync[bot]
authored andcommitted
Use compact shape comments throughout the numerical corpus
Summary: Replace the long `type =` and `.args[N]` spellings in the NumPy and SciPy demos with `= payload` and `.N = payload`. Keep all visible type annotations and shape contracts unchanged while making array carriers and selectors easier to read. Update the corpus's notation notes and retain the earlier spellings as supported parser aliases. Differential Revision: D127615303
1 parent 04edf01 commit 96af5a7

9 files changed

Lines changed: 62 additions & 55 deletions

File tree

‎tensor-shapes/comment-shape-corpus/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ Start with [the SciPy demo](scipy/README.md):
2222
- [Shape manipulation](numpy/shape_base.pyi): repetition tuples, symbolic
2323
multiplication, and an existing DSL-backed return transformation.
2424

25+
## Compact notation
26+
27+
`# shaped: payload` uses compatibility semantics; `# shaped: = payload` replaces
28+
the attached type component. `.0 = payload` selects its first written type
29+
argument, and paths such as `.0.1` reach nested arguments. The older `type =`
30+
and `.args[N]` forms remain accepted aliases, but the examples use the shorter
31+
spellings. Placement, shape contracts, and supported API cases are unchanged.
32+
2533
## Run
2634

2735
Use a Pyrefly build containing shape-comment support. Only NumPy's canonical

‎tensor-shapes/comment-shape-corpus/numpy/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ not every rejection indicates that NumPy itself would raise an exception.
5454
- `roll` demonstrates one whole-shape variable and unchanged dtype typing.
5555
- `diag` uses independent shape declarations on each overload.
5656
- Matrix contractions connect the dimensions of separate arguments.
57-
- Comments directly on tuple carriers keep dtype arguments readable.
57+
- Comments directly on tuple carriers use `# shaped: = payload` and keep dtype
58+
arguments readable. A whole-array comment can use `.0 = payload` instead.
5859
- `tile` turns an ordinary `tuple[int, int]` argument into an `IntTuple` and
5960
multiplies symbolic dimensions without changing the visible parameter type.
6061
- `expand_dims` demonstrates a DSL call inside the return carrier comment. Its

‎tensor-shapes/comment-shape-corpus/numpy/numeric.pyi‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,45 @@ _Scalar = TypeVar("_Scalar", bound=np.generic)
1111
# shapes: M, N
1212
def outer(
1313
a: np.ndarray[
14-
tuple[int], # shaped: type = [M]
14+
tuple[int], # shaped: = [M]
1515
np.dtype[np.float64],
1616
],
1717
b: np.ndarray[
18-
tuple[int], # shaped: type = [N]
18+
tuple[int], # shaped: = [N]
1919
np.dtype[np.float64],
2020
],
2121
out: (
2222
np.ndarray[
23-
tuple[int, int], # shaped: type = [M, N]
23+
tuple[int, int], # shaped: = [M, N]
2424
np.dtype[np.float64],
2525
]
2626
| None
2727
) = None,
2828
) -> np.ndarray[
29-
tuple[int, int], # shaped: type = [M, N]
29+
tuple[int, int], # shaped: = [M, N]
3030
np.dtype[np.float64],
3131
]: ...
3232

3333
# shapes: M, N, P
3434
def tensordot(
3535
a: np.ndarray[
36-
tuple[int, int], # shaped: type = [M, N]
36+
tuple[int, int], # shaped: = [M, N]
3737
np.dtype[np.float64],
3838
],
3939
b: np.ndarray[
40-
tuple[int, int], # shaped: type = [N, P]
40+
tuple[int, int], # shaped: = [N, P]
4141
np.dtype[np.float64],
4242
],
4343
axes: Literal[1],
4444
) -> np.ndarray[
45-
tuple[int, int], # shaped: type = [M, P]
45+
tuple[int, int], # shaped: = [M, P]
4646
np.dtype[np.float64],
4747
]: ...
4848

4949
# shapes: *Shape
5050
def roll(
51-
a: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .args[0] = Shape
51+
a: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .0 = Shape
5252
shift: int | tuple[int, ...],
5353
axis: int | tuple[int, ...] | None = None,
54-
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .args[0] = Shape
54+
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .0 = Shape
5555
...

‎tensor-shapes/comment-shape-corpus/numpy/shape_base.pyi‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,38 @@ _Scalar = TypeVar("_Scalar", bound=np.generic)
1212
# shapes: *Shape
1313
def expand_dims(
1414
a: np.ndarray[
15-
tuple[int, ...], # shaped: type = Shape
15+
tuple[int, ...], # shaped: = Shape
1616
np.dtype[_Scalar],
1717
],
1818
axis: Literal[0],
1919
) -> np.ndarray[
20-
tuple[int, ...], # shaped: type = expand_dims_shape(Shape, 0)
20+
tuple[int, ...], # shaped: = expand_dims_shape(Shape, 0)
2121
np.dtype[_Scalar],
2222
]: ...
2323

2424
# shapes: M, N, R, C
2525
def tile(
2626
A: np.ndarray[
27-
tuple[int, int], # shaped: type = [M, N]
27+
tuple[int, int], # shaped: = [M, N]
2828
np.dtype[_Scalar],
2929
],
30-
reps: tuple[int, int], # shaped: type = [R, C]
30+
reps: tuple[int, int], # shaped: = [R, C]
3131
) -> np.ndarray[
32-
tuple[int, int], # shaped: type = [M * R, N * C]
32+
tuple[int, int], # shaped: = [M * R, N * C]
3333
np.dtype[_Scalar],
3434
]: ...
3535

3636
# shapes: M, N, P, Q
3737
def kron(
3838
a: np.ndarray[
39-
tuple[int, int], # shaped: type = [M, N]
39+
tuple[int, int], # shaped: = [M, N]
4040
np.dtype[np.float64],
4141
],
4242
b: np.ndarray[
43-
tuple[int, int], # shaped: type = [P, Q]
43+
tuple[int, int], # shaped: = [P, Q]
4444
np.dtype[np.float64],
4545
],
4646
) -> np.ndarray[
47-
tuple[int, int], # shaped: type = [M * P, N * Q]
47+
tuple[int, int], # shaped: = [M * P, N * Q]
4848
np.dtype[np.float64],
4949
]: ...

‎tensor-shapes/comment-shape-corpus/numpy/twodim_base.pyi‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,48 @@ _Scalar = TypeVar("_Scalar", bound=np.generic)
1212
@overload
1313
def diag(
1414
v: np.ndarray[
15-
tuple[int], # shaped: type = [N]
15+
tuple[int], # shaped: = [N]
1616
np.dtype[_Scalar],
1717
],
1818
k: Literal[0] = 0,
1919
) -> np.ndarray[
20-
tuple[int, int], # shaped: type = [N, N]
20+
tuple[int, int], # shaped: = [N, N]
2121
np.dtype[_Scalar],
2222
]: ...
2323

2424
# shapes: N
2525
@overload
2626
def diag(
2727
v: np.ndarray[
28-
tuple[int, int], # shaped: type = [N, N]
28+
tuple[int, int], # shaped: = [N, N]
2929
np.dtype[_Scalar],
3030
],
3131
k: Literal[0] = 0,
3232
) -> np.ndarray[
33-
tuple[int], # shaped: type = [N]
33+
tuple[int], # shaped: = [N]
3434
np.dtype[_Scalar],
3535
]: ...
3636

3737
# shapes: *Batch, M, N
3838
def tril(
3939
v: np.ndarray[
40-
tuple[int, ...], # shaped: type = [*Batch, M, N]
40+
tuple[int, ...], # shaped: = [*Batch, M, N]
4141
np.dtype[_Scalar],
4242
],
4343
k: int = 0,
4444
) -> np.ndarray[
45-
tuple[int, ...], # shaped: type = [*Batch, M, N]
45+
tuple[int, ...], # shaped: = [*Batch, M, N]
4646
np.dtype[_Scalar],
4747
]: ...
4848

4949
# shapes: *Batch, M, N
5050
def triu(
5151
v: np.ndarray[
52-
tuple[int, ...], # shaped: type = [*Batch, M, N]
52+
tuple[int, ...], # shaped: = [*Batch, M, N]
5353
np.dtype[_Scalar],
5454
],
5555
k: int = 0,
5656
) -> np.ndarray[
57-
tuple[int, ...], # shaped: type = [*Batch, M, N]
57+
tuple[int, ...], # shaped: = [*Batch, M, N]
5858
np.dtype[_Scalar],
5959
]: ...

‎tensor-shapes/comment-shape-corpus/scipy/README.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ properties such as positive definiteness, finite values, or valid FFT axes.
4949

5050
## What to assess
5151

52-
Compare the small `int # shaped: N` case with array `.args[0]` selectors,
52+
Compare the small `int # shaped: N` case with array `.0` selectors,
5353
comments directly beside tuple shape carriers, and the whole-type replacement
5454
needed when an `NDArray` alias hides its shape argument. Try changing ordinary
5555
annotations or adding API cases: the goal is to expose syntax friction, not to
@@ -58,9 +58,9 @@ maximize SciPy coverage.
5858
## Syntax observations
5959

6060
- The scalar `n: int # shaped: N` form is compact.
61-
- `.args[0]` preserves dtype annotations. Splitting the type lets a shape comment
61+
- `.0` preserves dtype annotations. Splitting the type lets a shape comment
6262
sit directly beside `tuple[int, ...]` instead of using a long selector.
63-
- An `NDArray` alias hides the shape slot, requiring a full `type =` replacement.
63+
- An `NDArray` alias hides the shape slot, requiring a full `= payload` replacement.
6464
- `cho_factor` refines both the factor array and its array-valued lower flag.
6565
Per-component comments express both shapes without repeating the return type.
6666
- Optional `out` arrays keep their shape comment on the array's carrier line,

‎tensor-shapes/comment-shape-corpus/scipy/cholesky.pyi‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@ type _LowerFlag = bool | np.ndarray[tuple[()], np.dtype[np.bool]]
1212
# shapes: *Batch, N
1313
def cholesky(
1414
a: np.ndarray[
15-
tuple[int, ...], # shaped: type = [*Batch, N, N]
15+
tuple[int, ...], # shaped: = [*Batch, N, N]
1616
np.dtype[np.float64],
1717
],
1818
lower: bool = False,
1919
overwrite_a: bool = False,
2020
check_finite: bool = True,
2121
) -> np.ndarray[
22-
tuple[int, ...], # shaped: type = [*Batch, N, N]
22+
tuple[int, ...], # shaped: = [*Batch, N, N]
2323
np.dtype[np.float64],
2424
]: ...
2525

2626
# shapes: *Batch, N
2727
def cho_factor(
2828
a: np.ndarray[
29-
tuple[int, ...], # shaped: type = [*Batch, N, N]
29+
tuple[int, ...], # shaped: = [*Batch, N, N]
3030
np.dtype[np.float64],
3131
],
3232
lower: bool = False,
3333
overwrite_a: bool = False,
3434
check_finite: bool = True,
3535
) -> tuple[
3636
np.ndarray[
37-
tuple[int, ...], # shaped: type = [*Batch, N, N]
37+
tuple[int, ...], # shaped: = [*Batch, N, N]
3838
np.dtype[np.float64],
3939
],
4040
np.ndarray[
41-
tuple[int, ...], # shaped: type = Batch
41+
tuple[int, ...], # shaped: = Batch
4242
np.dtype[np.bool],
4343
],
4444
]: ...
@@ -47,24 +47,24 @@ def cho_factor(
4747
@overload
4848
def cho_solve(
4949
c_and_lower: tuple[
50-
np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .args[0] = [N, N]
50+
np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .0 = [N, N]
5151
_LowerFlag,
5252
],
53-
b: np.ndarray[tuple[int], np.dtype[np.float64]], # shaped: .args[0] = [N]
53+
b: np.ndarray[tuple[int], np.dtype[np.float64]], # shaped: .0 = [N]
5454
overwrite_b: bool = False,
5555
check_finite: bool = True,
56-
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .args[0] = [N]
56+
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .0 = [N]
5757
...
5858

5959
# shapes: N, K
6060
@overload
6161
def cho_solve(
6262
c_and_lower: tuple[
63-
np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .args[0] = [N, N]
63+
np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .0 = [N, N]
6464
_LowerFlag,
6565
],
66-
b: np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .args[0] = [N, K]
66+
b: np.ndarray[tuple[int, int], np.dtype[np.float64]], # shaped: .0 = [N, K]
6767
overwrite_b: bool = False,
6868
check_finite: bool = True,
69-
) -> np.ndarray[tuple[int, int], np.dtype[np.float64]]: # shaped: .args[0] = [N, K]
69+
) -> np.ndarray[tuple[int, int], np.dtype[np.float64]]: # shaped: .0 = [N, K]
7070
...

‎tensor-shapes/comment-shape-corpus/scipy/distance.pyi‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,40 @@ import numpy.typing as npt
1111
# shapes: M, P, N
1212
def cdist(
1313
XA: np.ndarray[
14-
tuple[int, int], # shaped: type = [M, N]
14+
tuple[int, int], # shaped: = [M, N]
1515
np.dtype[np.float64],
1616
],
1717
XB: np.ndarray[
18-
tuple[int, int], # shaped: type = [P, N]
18+
tuple[int, int], # shaped: = [P, N]
1919
np.dtype[np.float64],
2020
],
2121
metric: Literal["euclidean"] = "euclidean",
2222
*,
2323
out: (
2424
np.ndarray[
25-
tuple[int, int], # shaped: type = [M, P]
25+
tuple[int, int], # shaped: = [M, P]
2626
np.dtype[np.float64],
2727
]
2828
| None
2929
) = None,
3030
) -> np.ndarray[
31-
tuple[int, int], # shaped: type = [M, P]
31+
tuple[int, int], # shaped: = [M, P]
3232
np.dtype[np.float64],
3333
]: ...
3434

3535
# shapes: M, N
3636
def pdist(
37-
X: npt.NDArray[
38-
np.float64
39-
], # shaped: type = np.ndarray[[M, N], np.dtype[np.float64]]
37+
X: npt.NDArray[np.float64], # shaped: = np.ndarray[[M, N], np.dtype[np.float64]]
4038
metric: Literal["euclidean"] = "euclidean",
4139
*,
4240
out: (
4341
np.ndarray[
44-
tuple[int], # shaped: type = [M * (M - 1) // 2]
42+
tuple[int], # shaped: = [M * (M - 1) // 2]
4543
np.dtype[np.float64],
4644
]
4745
| None
4846
) = None,
4947
) -> np.ndarray[
50-
tuple[int], # shaped: type = [M * (M - 1) // 2]
48+
tuple[int], # shaped: = [M * (M - 1) // 2]
5149
np.dtype[np.float64],
5250
]: ...

‎tensor-shapes/comment-shape-corpus/scipy/fft_helper.pyi‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def fftfreq(
1616
*,
1717
xp: None = None,
1818
device: Literal["cpu"] | None = None,
19-
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .args[0] = [N]
19+
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .0 = [N]
2020
...
2121

2222
# shapes: N
@@ -26,19 +26,19 @@ def rfftfreq(
2626
*,
2727
xp: None = None,
2828
device: Literal["cpu"] | None = None,
29-
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .args[0] = [N // 2 + 1]
29+
) -> np.ndarray[tuple[int], np.dtype[np.float64]]: # shaped: .0 = [N // 2 + 1]
3030
...
3131

3232
# shapes: *Shape
3333
def fftshift(
34-
x: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .args[0] = Shape
34+
x: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .0 = Shape
3535
axes: int | tuple[int, ...] | None = None,
36-
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .args[0] = Shape
36+
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .0 = Shape
3737
...
3838

3939
# shapes: *Shape
4040
def ifftshift(
41-
x: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .args[0] = Shape
41+
x: np.ndarray[tuple[int, ...], np.dtype[_Scalar]], # shaped: .0 = Shape
4242
axes: int | tuple[int, ...] | None = None,
43-
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .args[0] = Shape
43+
) -> np.ndarray[tuple[int, ...], np.dtype[_Scalar]]: # shaped: .0 = Shape
4444
...

0 commit comments

Comments
 (0)