Add shorthand and test IntTuple tparam defaults (#4896) - #4896
Open
stroxler wants to merge 1 commit into
Open
Conversation
Contributor
|
@stroxler has exported this pull request. If you are a Meta employee, you can view the originating Diff in D119566820. |
This comment has been minimized.
This comment has been minimized.
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 11, 2026
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 11, 2026
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 11, 2026
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 11, 2026
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
meta-codesync
Bot
force-pushed
the
export-D119566820
branch
from
September 11, 2026 23:21
fc9652e to
e97895e
Compare
meta-codesync Bot
pushed a commit
that referenced
this pull request
Sep 11, 2026
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
```
def add[S0: IntTuple, S1: IntTuple](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
and we want something like `f(3.141, y)` to produce the same shape as `y`.
The problem is that this doesn't actually work: if we pass a `float` for `x`, we wind up failing
to constrain `S0`, and it solves to gradual `IntTuple`, which means we drop the shape.
The workaround is to use a default:
```
def add[S0: IntTuple = [], S1: IntTuple = []](
x: Array[S0] | float, y: Array[S1] | float
) -> Array[broadcast(S0, S1)]
```
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to `[]` by the default.
This diff adds support for the `[]` shorthand in this syntactic position for `IntTuple`-constrained type parameters, and verifies that it works as expected
Differential Revision: D119566820
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
In jax and other array libraries, it's not unusual to auto-promote scalars to empty-shape
arrays so that broadcasting works. For example, we can have
and we want something like
f(3.141, y)to produce the same shape asy.The problem is that this doesn't actually work: if we pass a
floatforx, we wind up failingto constrain
S0, and it solves to gradualIntTuple, which means we drop the shape.The workaround is to use a default:
so that taking the scalar branch of the union leads to the unconstrained shape parameter being solved to
[]by the default.This diff adds support for the
[]shorthand in this syntactic position forIntTuple-constrained type parameters, and verifies that it works as expectedDifferential Revision: D119566820