TYP: broadcast_to shape-typing - #32247
Merged
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
jorenham
force-pushed
the
typing/broadcast_to/shape-typing
branch
from
August 11, 2026 10:09
f4bb765 to
800502c
Compare
|
Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code: optuna (https://github.com/optuna/optuna)
./optuna/study/_multi_objective.py:182: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
|
Member
|
Thanks Joren. |
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.
TLDR;
broadcast_toused to ignore all shape types, now it propagates it whenevershapeis a tuple.The stubs for
np.broadcast_tocompletely ignored theshapetype, even though there are many common usecases where it (viz. the shape-type) can be exactly statically known.This propages the shape-types in case it's assignable to
tuple[int, ..](so e.g.shape=[1, 2]wouldn't work, because type-checkers aren't able to statically determine the size of a list like they can for tuples -- lists are mutable and homogeneous, whereas tuples are immutable an heterogeneous).There's one annoying thing here though: some typecheckers such as pyright propagate the
Literalintergers "inside" the tuple shape-type, but others (like mypy) don't. Sobroadcast_to(a, (1, 2))will have a shape-type oftuple[int, int]according to mypy, whereas pyright will infer it astuple[Literal[1], Literal[2]]. I could've worked around this (using finite constrained type variables), but that would've harmed generality. TheseintvsLiteral[]difference also won't matter in the real word, because both are assignable toint, andbroadcast_toonly accepts positive integers (unlike e.g.reshape, which accepts negative integers, which in this case could have lead to nonsensical shape-types in case of e.g.shape=(2, -1)). So nothing to worry about, just something worth noting.Fully written by me, pre-reviewed and audited by AI (see this rank-typing tracker hist for details).