Skip to content

Keep SciPy shape-comment overloads compact - #5400

Closed
stroxler wants to merge 12 commits into
mainfrom
export-D127646905
Closed

stroxler wants to merge 12 commits into
mainfrom
export-D127646905

Conversation

@stroxler

Copy link
Copy Markdown
Contributor

Summary:
Put SciPy overload shape declarations between overload and def so Ruff preserves adjacent overloads, matching scipy-stubs formatting conventions.

Leave NumPy examples, shape contracts, and the comment parser unchanged. Document the chosen placement in the SciPy demo.

Differential Revision: D127646905

Summary:
Provide a source-preserving frontend for comment-only shape annotations in shared stubs. An explicit module header enables `# shapes:` declarations and trailing `# shaped:` payloads with optional type-argument selectors.

Attach directives using the original syntax and canonical comment ranges, including definitions in unreachable branches. Reuse the expression depth limit before passing separately parsed payloads to later phases.

This diff supplies parsing and attachment; later diffs connect declarations and annotation semantics.

Differential Revision: D127441552
Summary:
Allow shared stubs to declare shape variables with `# shapes: M, N, *Batch` instead of a visible decorator. Reuse `shape_vars` parsing and quantification so scalar and variadic variables, class defaults, and declaration scopes remain consistent across both spellings.

This diff activates declaration comments; annotation transformations follow in the next diff.

Differential Revision: D127441629
Summary:
Make the compatibility transformation accept an already-interpreted base type and its deferred diagnostics. Comment annotations can then reuse the transformation without reinferring their own root and recursively applying themselves.

Keep the existing native `Shaped` behavior, including tuple-carrier checks, and normalize registered array bases through their class arguments.

Differential Revision: D127488651
Summary:
Bind trailing `# shaped:` payloads in the enclosing annotation scope and apply them through a side table, leaving the source AST unchanged. Reuse compatibility-mode interpretation for scalar dimensions, class arguments, tuple carriers, and DSL calls.

Support concise comments, explicit whole-type replacement, and selectors into written type arguments. Preserve genuine diagnostics on replaced annotations and collect legacy type variables appearing only in payloads.

Qualifier preservation and additional edge-case validation follow in the next diff.

Differential Revision: D127488673
Summary:
Keep annotation qualifiers outside the type supplied by a shape comment, including nested `Annotated`, `Final`, and TypedDict qualifiers. Reject comments on bare qualifiers and existing shape wrappers rather than silently ignoring them.

Add regression coverage for declaration scopes, overloads, annotated assignments, invalid targets and placement, and suppressed diagnostics. Document the opt-in syntax and initial supported positions.

The shared `Shaped` refactor and core comment application now live in the two preceding diffs. This split preserves the combined source and restacks the later corpus and multiline enhancements without behavior changes.

Differential Revision: D127441649
Summary:
Provide small, independent API slices for evaluating the shape-comment syntax on real library interfaces. The first examples cover FFT helpers, Cholesky routines, and spatial distances using only NumPy annotations, with pinned provenance and supported-case notes.

Keep each project in its own directory and discover documented demos automatically. Check the module slices before their callers, integrate them into static tensor-shape CI, and retain expected shape rejections. These are syntax experiments, not a replacement scipy-stubs distribution or runtime tests.

Differential Revision: D127467146
Summary:
Allow trailing shape comments on complete components within multiline annotations. This lets shared stubs refine tuple carriers and union branches locally, without repeating the whole type or writing long selectors from its root.

Record component paths in the original AST and resolve them after forward-reference parsing. Multiple comments in one annotation must select disjoint final targets. Preserve whole-annotation comments, shape-variable scoping, legacy type-variable binding, and compatibility-mode payload semantics. Scalar type-argument comments use the dimension parser; unsupported starred or metadata targets diagnose rather than silently disappear.

Differential Revision: D127471227
Summary:
Rewrite the Cholesky and distance examples to demonstrate multiline type-component annotations. Shape comments now sit beside the array's tuple carrier, leaving optional `None` branches separate and refining both elements of `cho_factor` without a verbose whole-tuple replacement.

Keep the existing shape contracts and call-site expectations unchanged. Update syntax observations to describe the new readable form and the remaining NDArray-alias replacement limitation.

Differential Revision: D127471246
Summary:
Extend the independent comment-only annotation corpus with NumPy numeric operations, two-dimensional helpers, and shape manipulation. The examples cover dimension relationships across arguments, dtype-preserving generics, rank-changing overloads, batched shapes, repetition tuples, optional output buffers, and an existing DSL-backed return transformation.

Keep the excerpts deliberately narrow and self-contained, with pinned NumPy provenance and explicit supported-case notes. Project discovery automatically includes the new NumPy directory; the existing SciPy demos and type-checker implementation are unchanged.

Differential Revision: D127490314
Summary:
Allow shape-comment replacements to start with `=` and use `.N` paths for written type arguments. This makes NumPy carrier annotations compact without changing their attachment, binding, or shape interpretation.

Retain `type =` and `.args[N]` as aliases while making the shorter forms canonical. Parse selectors independently of payload expressions so chained decimal indices do not become Python floats and `=` inside a DSL call does not become a replacement marker.

Differential Revision: D127615239
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
Summary:
Put SciPy overload shape declarations between `overload` and `def` so Ruff preserves adjacent overloads, matching scipy-stubs formatting conventions.

Leave NumPy examples, shape contracts, and the comment parser unchanged. Document the chosen placement in the SciPy demo.

Differential Revision: D127646905
@meta-cla meta-cla Bot added the cla signed label Oct 26, 2026
@meta-codesync

meta-codesync Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

@stroxler has exported this pull request. If you are a Meta employee, you can view the originating Diff in D127646905.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@meta-codesync meta-codesync Bot closed this in bfcb94f Oct 26, 2026
@meta-codesync meta-codesync Bot added the Merged label Oct 26, 2026
@meta-codesync

meta-codesync Bot commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in bfcb94f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant