docs: fix tensor shapes syntax to match shipped stubs - #4562
docs: fix tensor shapes syntax to match shipped stubs#4562Vishwaspatel2401 wants to merge 2 commits into
Conversation
The tensor shapes docs taught a shape syntax (unbounded type parameters, flat Tensor[D1, D2, ...] argument lists) that the shipped pyrefly-torch-stubs no longer accept. Code written the way the docs showed it did not check. Applies the current stubs' convention throughout: IntVar-bounded type parameters and a single shape-list argument to Tensor (Tensor[[D1, D2]]). Also updates the variadic-dimension convention from a bare TypeVarTuple to the Bs: IntTuple + Tensor[[*Elements[Bs], ...]] form the stubs actually use. Fixes facebook#4560
|
Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
I filed #4560, so I ran your branch against the stubs to check the examples. Environment: pyrefly 1.2.0 and pyrefly-torch-stubs 1.2.0 from PyPI, Python 3.12, Linux. Checks clean:
Your One thing I think is worth fixing: the new names have no stated origin. After this change, five pages use This isn't a regression you introduced by accident so much as a side effect of the correct fix: the old spellings needed no import, since Two smaller notes, take or leave: In the concepts section of The two heading renames change their anchors ( Scope: I didn't reassemble every incremental fragment on all seven pages. The architectures, advanced, and loops examples I only checked structurally, not by running them. Disclosure, per the AI Usage section of CONTRIBUTING.md: these checks and this comment were produced by an AI agent (Claude Code) working in my checkout. I reviewed them before posting. |
…in prose ting-hong-shieh's review on the PR found two gaps: the reference page never said where IntVar, IntTuple, and Elements come from (only Int was sourced), so copying the variadic snippet as printed hit unknown-name errors. And tensor-shapes.mdx used Bs in prose before the variadic mechanism is ever explained on that page. Extends the reference page's existing Int[X] sourcing sentence to cover all four names. Rewrites the tensor-shapes.mdx mention in plain words instead and points to the reference page's variadic section for the actual syntax.
|
Thanks for running the branch against the stubs — good catch on the missing sourcing. Fixed the import gap: extended the reference page's existing Int[X] sentence to also cover IntVar, IntTuple, and Elements, so there's one place that says where all four names come from. Didn't add per-snippet import lines since that would break from how every other fragment on these pages is shown (none of them show imports, including Int before this change) — the reference page's sourcing sentence is the one place meant to answer that question, so extending it felt like the right fix rather than adding inconsistent per-snippet imports. Also fixed the Bs-with-no-antecedent issue on tensor-shapes.mdx — rewrote that sentence in plain words instead of using the Elements[Bs] syntax before it's ever explained, and linked to the reference page's variadic section for the actual syntax. Agreed on the anchor renames — confirmed the same thing, nothing in the repo links to either old anchor, so leaving the new headings as-is. Pushed as a follow-up commit on the same branch. |
|
This pull request has been imported. If you are a Meta employee, you can view this in D116250396. (Because this pull request was imported automatically, there will not be any future comments.) |
Summary
The tensor shapes docs taught a shape syntax that the shipped
pyrefly-torch-stubs no longer accept: unbounded type parameters
(
class Model[D]) and a flat multi-argumentTensor[D1, D2, ...]form.Code written the way the docs showed it did not check.
This rewrites the affected pages to match what the stubs actually require:
IntVar-bounded type parameters and a single shape-list argument toTensor(Tensor[[D1, D2]]), matching the pattern already used intorch-stubs/nn/__init__.pyi.One page needed more than a bracket rewrap: the variadic-dimension
convention. The docs taught a bare
TypeVarTuple(def forward[*Bs](..., x: Tensor[*Bs, D])), which doesn't just need brackets — it fails outrightunder the current stubs (
Unpacked type in IntTuple must use Elements[...], got Bs). Replaced with the conventionnn.Linear.forwardactually uses:a type parameter bound to
IntTuple, spliced into the shape list with*Elements[...](def forward[Bs: IntTuple](..., x: Tensor[[*Elements[Bs], D]])).tensor-shapes-setup.mdxis intentionally not touched here — it's alreadybeing fixed by #4559.
Two things came up during this pass that are real but out of scope for this
issue, noted here rather than silently fixed:
tensor-shapes-tutorial-advanced.mdx's DCGAN section claimsDCGAN.ngf * 8resolves toLiteral[512]; it actually widens to plainint. Unrelated to the shape-syntax bug — a separate literal-arithmeticquestion.
tensor-shapes-tutorial-architectures.mdx(
UNet.recurse/_decode,Generator._chain) hit genuine algebraic-gapsolver limitations even with the corrected syntax — the same class of gap
the page's own "Algebraic gaps" section already documents and prescribes
type: ignorefor.Fixes #4560
Test Plan
Environment:
pyrefly==1.2.0andpyrefly-torch-stubs==1.2.0from PyPI in aclean venv, matching the issue's own reproduction environment.
Confirmed the single-file default preset (
pyrefly check file.pywith nopyrefly.toml) silently uses the lenientbasicpreset, which disablesassignment/call-shape validation and would falsely show 0 errors even on
broken code — all checks below use
pyrefly check -p defaultto get realvalidation.
Re-ran both of the issue's own reproductions, extracted verbatim from the
now-fixed files:
BaselineActorexample fromtensor-shapes-tutorial-basics.mdx.(the one that produced the issue's 8 errors) — now
0 errors.Attention/Int[X] | Noneexample fromtensor-shapes-reference.mdx(
Expected 1 type argument for Tensor, got 3) — that error is gone; theonly remaining diagnostic is an unrelated
missing-returnfrom thefragment's own
...placeholder body, present in the original doc too.For every other changed code block, reconstructed it with minimal synthetic
scaffolding (stand-in modules for fragments that reference outer context)
and ran
pyrefly check -p default, confirmingassert_typecalls pass andsignatures unify. Swept all 7 files afterward for any remaining flat
Tensor[...]form — none found.Did not run the Docusaurus build (
website/'snode_modulesisn'tinstalled in this environment); checked code-fence and bracket balance
across all 7 files as a structural proxy instead.
AI usage disclosure
Per the AI Usage section of CONTRIBUTING.md: this fix, its verification
against the shipped stubs, and this description were produced by an AI
agent (Claude Code) working in my checkout. I reviewed them before
submitting.