Repository navigation
Conversation
…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
Contributor
|
@kinto0 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D125829160. |
|
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
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:
Subscripting a value whose type is a TypeVar bounded by a class (
seq[0:1]withS: SupportsSlice) inferred the bound instead ofSwhen__getitem__returnsSelf. Calling the method explicitly (seq.__getitem__(slice(0, 1))) already inferredS.The subscript path handled a bounded TypeVar by subscripting its bound, so
__getitem__was looked up and called with the bound as the receiver, andSelfresolved 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 bindsSelfto 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