Repository navigation
Fix inline list inference against protocols with Self - #22542
Draft
ibrahimmaq14 wants to merge 1 commit into
Draft
ibrahimmaq14 wants to merge 1 commit into
ibrahimmaq14 wants to merge 1 commit into
Conversation
Ignore reflexive inference constraints introduced by binding protocol Self when matching against an expected protocol type. Fixes python#21563.
Contributor
|
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.
Fixes #21563.
A list literal is inferred through a synthetic generic call. When that call
has a protocol as its expected return type, binding the protocol's Self to
list[T] can generate constraints such as T <: T. The solver can then use T
as its own solution, causing an inline list of integers to produce a
[list-item] error.
Discard reflexive constraints on ordinary inference variables from this
direction of protocol member matching. Keep constraints that contribute
polymorphic type variables. The list elements can now determine T, and
protocol members that genuinely constrain T still guide inference.
The regression tests cover inline and preassigned lists, element type
inference through getitem, and rejection of a list item that violates a
Literal constraint.
Verification
to a clean run.
pytest -n 8 -q mypy/test/testcheck.py mypy/test/testconstraints.py mypy/test/testsolve.pyreported 8,403 passed, 16 skipped, and 7 xfailed.
AI assistance
OpenAI Codex triaged the issue, traced the cause, drafted the patch and regression tests, and ran the local checks.