Skip to content

Tensor shapes docs use shape syntax that no longer checks against the shipped stubs #4560

Description

@ting-hong-shieh

Describe the Bug

The tensor shapes documentation teaches a shape syntax that the shipped stubs no
longer accept. Code written the way the docs show it does not check.

The current stubs want IntVar-bounded type parameters and a single shape list
argument to Tensor:

class Linear[IN: IntVar, OUT: IntVar](Module):        # torch-stubs/nn/__init__.pyi
    weight: Tensor[[OUT, IN]]

The docs use unbounded parameters and a flat argument list — class Model[D],
Tensor[B, T, D]. Across website/docs/tensor-shapes*.mdx there are 81
occurrences of the flat Tensor[...] form, 31 declarations with unbounded shape
type parameters, and no IntVar bound on any of the pages listed below.

Reproduction. pyrefly 1.2.0 and pyrefly-torch-stubs 1.2.0, both from PyPI,
Python 3.12.

The step-by-step model from
Tutorial: the basics,
copied as the page builds it up:

class BaselineActor[S, A](nn.Module):
    def __init__(self, state_size: Int[S], action_size: Int[A]) -> None:
        super().__init__()
        self.fc1 = nn.Linear(state_size, 400)
        self.fc2 = nn.Linear(400, 400)
        self.out = nn.Linear(400, action_size)

    def forward[B](self, state: Tensor[B, S]) -> Tensor[B, A]:
        h1 = F.relu(self.fc1(state))
        assert_type(h1, Tensor[B, 400])
        h2 = F.relu(self.fc2(h1))
        return torch.tanh(self.out(h2))
ERROR `S` must be an `IntVar` to be used as a shape dimension. [invalid-annotation]
ERROR `A` must be an `IntVar` to be used as a shape dimension. [invalid-annotation]
ERROR `B` is not assignable to upper bound `IntTuple` of type variable `Shape` [bad-specialization]
ERROR Expected 1 type argument for `Tensor`, got 2 [bad-specialization]
ERROR assert_type(Tensor[[*tuple[int, ...], 400]], Tensor[B]) failed [assert-type]
 INFO 8 errors

The Int[X] | None example from
Reference fails the same
way, with Expected 1 type argument for Tensor, got 3. The same page's "Usage
patterns" table prescribes the old form directly:

Pattern Purpose
class Model[D](nn.Module) Make a dimension a class-level type parameter
def forward[B](self, x: Tensor[B, D]) Bind a per-call dimension

Rewriting either example to match the stubs — [S: IntVar, A: IntVar],
Tensor[[B, S]] — checks clean, so this looks like documentation lagging an API
change rather than a checker bug. The repository's own examples, e.g.
tensor-shapes/pyrefly-torch-stubs/examples/nanogpt.py, already use the new
form.

Scope. Counts of the flat Tensor[...] form per page:

Page Occurrences
tensor-shapes-tutorial-architectures.mdx 19
tensor-shapes-reference.mdx 17
tensor-shapes-tutorial-basics.mdx 12
tensor-shapes-tutorial-loops.mdx 12
tensor-shapes.mdx 11
tensor-shapes-tutorial-advanced.mdx 9
tensor-shapes-ai-porting.mdx 1

I verified two of these pages by running them; I have not run every example. The
examples on tensor-shapes.mdx are exposition rather than code to copy, but they
still show the old form.

The inlay-hint screenshots under website/static/img/tensor-shapes/ show the old
syntax too (def forward[B, T](self, x: Tensor[B, T, NEmbedding])), so they need
retaking rather than editing.

Question before anyone starts on it: is this the same underlying change as
#4153, and is a rewrite already underway? I'm happy to do the mechanical pass
across the tutorials and the reference page if that's useful, but not if it would
collide with work in progress.

Sandbox Link

No response

(Only applicable for extension issues) IDE Information

No response

Disclosure, per the AI Usage section of CONTRIBUTING.md: the counts, the reproductions, and this report were produced by an AI agent (Claude Code) working in my checkout. I reviewed them before posting.

Metadata

Metadata

Labels

documentationImprovements or additions to documentationtensor-shapesAnything related to tensor shape typing

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions