Skip to content

Resolve Self to the TypeVar when subscripting a class-bounded TypeVar - #5258

Open
kinto0 wants to merge 2 commits into
facebook:mainfrom
kinto0:export-D125829160
Open

kinto0 wants to merge 2 commits into
facebook:mainfrom
kinto0:export-D125829160

Conversation

@kinto0

@kinto0 kinto0 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Summary:
Subscripting a value whose type is a TypeVar bounded by a class (seq[0:1] with S: SupportsSlice) inferred the bound instead of S when __getitem__ returns Self. Calling the method explicitly (seq.__getitem__(slice(0, 1))) already inferred S.

The subscript path handled a bounded TypeVar by subscripting its bound, so __getitem__ was looked up and called with the bound as the receiver, and Self resolved to the bound. Binary operators don't have this problem because they call the dunder on the TypeVar itself.

For a TypeVar bounded by a class whose subscripts use the plain __getitem__ call, we now call __getitem__ on the TypeVar, the same way an explicit method call does. Attribute lookup on a bounded TypeVar binds Self to the TypeVar. Bounds with builtin subscript handling (lists, tuples, shaped arrays, nn.ModuleDict) still subscript the bound, so their precise indexing is kept.

fixes #5257

Differential Revision: D125829160

…em__` returns `Self`

Summary:
Subscripting a value whose type is a TypeVar bounded by a class (`seq[0:1]` with `S: SupportsSlice`) infers the bound instead of `S` when `__getitem__` returns `Self`. Calling `seq.__getitem__(slice(0, 1))` directly infers `S` as expected. This adds a test, marked `bug`, that records the current behavior. The next diff fixes it.

reproduces facebook#5257

Differential Revision: D125829599
Summary:
Subscripting a value whose type is a TypeVar bounded by a class (`seq[0:1]` with `S: SupportsSlice`) inferred the bound instead of `S` when `__getitem__` returns `Self`. Calling the method explicitly (`seq.__getitem__(slice(0, 1))`) already inferred `S`.

The subscript path handled a bounded TypeVar by subscripting its bound, so `__getitem__` was looked up and called with the bound as the receiver, and `Self` resolved to the bound. Binary operators don't have this problem because they call the dunder on the TypeVar itself.

For a TypeVar bounded by a class whose subscripts use the plain `__getitem__` call, we now call `__getitem__` on the TypeVar, the same way an explicit method call does. Attribute lookup on a bounded TypeVar binds `Self` to the TypeVar. Bounds with builtin subscript handling (lists, tuples, shaped arrays, `nn.ModuleDict`) still subscript the bound, so their precise indexing is kept.

fixes facebook#5257

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

meta-codesync Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown

Diff from mypy_primer, showing the effect of this PR on open source code:

============================================================
SUMMARY
============================================================
Total: +1 new errors, -2 fixed errors
By preset: +1/-2 (default), +1/-2 (strict)

Projects with changes (2):
  hydpy: +1 -1
  pandas: +0 -1
============================================================

FULL DIFF DETAILS
------------------------------------------------------------

hydpy (https://github.com/hydpy-dev/hydpy)
- ERROR hydpy/auxs/iuhtools.py:516:13-20: Cannot index into `float` [bad-index]
+ ERROR hydpy/auxs/iuhtools.py:516:13-20: Cannot index into `VectorFloatFlex` [bad-index]

pandas (https://github.com/pandas-dev/pandas)
- ERROR pandas/core/dtypes/missing.py:655:16-43: Returned type `ndarray | Unknown` is not assignable to declared return type `_ArrLikeT` [bad-return]

This branch has not been deployed

No deployments
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.

Subscripting a bounded or constrained TypeVar resolves Self in __getitem__ to the bound instead of the TypeVar

1 participant