Skip to content

Fix inline list inference against protocols with Self - #22542

Draft
ibrahimmaq14 wants to merge 1 commit into
python:masterfrom
ibrahimmaq14:fix/21563-inline-list-protocol-inference
Draft

ibrahimmaq14 wants to merge 1 commit into
python:masterfrom
ibrahimmaq14:fix/21563-inline-list-protocol-inference

Conversation

@ibrahimmaq14

Copy link
Copy Markdown

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

  • The original strict-mode reproducer went from three [list-item] errors
    to a clean run.
  • pytest -n 8 -q mypy/test/testcheck.py mypy/test/testconstraints.py mypy/test/testsolve.py
    reported 8,403 passed, 16 skipped, and 7 xfailed.
  • Mypy's self-check, Black, Ruff, and git diff --check passed.

AI assistance

OpenAI Codex triaged the issue, traced the cause, drafted the patch and regression tests, and ran the local checks.

Ignore reflexive inference constraints introduced by binding protocol Self when matching against an expected protocol type.

Fixes python#21563.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inlining a list argument into call of function that accepts a protocol produces "incompatible type" error

1 participant