Skip to content

TYP: unique_{all,inverse} improved shape-typing - #32727

Merged
charris merged 3 commits into
numpy:mainfrom
jorenham:typing/unique_inverse/shape-typing
Sep 22, 2026
Merged

charris merged 3 commits into
numpy:mainfrom
jorenham:typing/unique_inverse/shape-typing

Conversation

@jorenham

Copy link
Copy Markdown
Member

The result type's inverse_indices array of np.unique_all and np.unique_inverses is now shape-typed. I had to revert to using legacy TypeVar's because it turns out that Pyright has a bug in it's "variance" (which is actually called "polarity" in the literature) inference algorithm, causing Pyright to incorrectly infer the generic scalar-type and shape-type parameters as invariant rather than covariant (NamedTuple is immutable, dtype is covariant in scalar-type, and ndarray is covariant in shape-type, so it should be covariant here as well).
The corresponding type-tests for these functions, as well as those for np.unique_counts, are now also more representative of real-world usage (instead of asserting against a nominal private-API named tuple type with type-check-only generic params).


Pair programmed with AI.

@jorenham jorenham added this to the 2.6.0 Release milestone Sep 21, 2026
@charris
charris merged commit b7085b5 into numpy:main Sep 22, 2026
16 checks passed
@charris

charris commented Sep 22, 2026

Copy link
Copy Markdown
Member

Thanks Joren.

@jorenham
jorenham deleted the typing/unique_inverse/shape-typing branch September 22, 2026 16:54
Riaz1729 pushed a commit to Riaz1729/numpy that referenced this pull request Sep 23, 2026
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.

2 participants