Fix list concatenation ignoring outer list type context#21746
Open
Ria-K912 wants to merge 3 commits into
Open
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Ria-K912
force-pushed
the
fix-list-concat-type-context
branch
from
July 18, 2026 07:31
c690ee7 to
d619ba7
Compare
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: prefect (https://github.com/PrefectHQ/prefect)
- src/prefect/utilities/callables/__init__.py:717: error: Incompatible types in assignment (expression has type "list[None]", variable has type "list[expr | None]") [assignment]
- src/prefect/utilities/callables/__init__.py:717: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
- src/prefect/utilities/callables/__init__.py:717: note: Consider using "Sequence" instead, which is covariant
- src/prefect/utilities/callables/__init__.py:719: error: Unsupported operand types for + ("list[None]" and "list[expr | None]") [operator]
pydantic (https://github.com/pydantic/pydantic)
- pydantic/aliases.py:28: error: Incompatible types in assignment (expression has type "list[str]", variable has type "list[int | str]") [assignment]
- pydantic/aliases.py:28: note: "list" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
- pydantic/aliases.py:28: note: Consider using "Sequence" instead, which is covariant
- pydantic/aliases.py:28: error: Argument 1 to "list" has incompatible type "tuple[str | int, ...]"; expected "Iterable[str]" [arg-type]
dedupe (https://github.com/dedupeio/dedupe)
+ dedupe/labeler.py:451: error: Unused "type: ignore" comment [unused-ignore]
+ dedupe/labeler.py:487: error: Unused "type: ignore" comment [unused-ignore]
jax (https://github.com/google/jax)
- jax/_src/numpy/lax_numpy.py:3148: error: Incompatible types in assignment (expression has type "list[int]", variable has type "list[float64]") [assignment]
+ jax/_src/numpy/lax_numpy.py:3148: error: List item 0 has incompatible type "int"; expected "float64" [list-item]
meson (https://github.com/mesonbuild/meson)
+ mesonbuild/modules/i18n.py:173:43: error: Unsupported operand types for + ("list[File | CustomTarget]" and "list[CustomTarget]") [operator]
|
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 #21710
List literals already use an outer
list[...]type context for bidirectional inference, so this works:But concatenation did not forward that context, so both operands were inferred as
list[int]and assignment failed due to invariance:This PR special-cases
+when the active type context isbuiltins.list, similar to the existing[...] * nhandling: both operands are checked under that context beforelist.__add__is applied. That covers list literals, list comprehensions, empty lists, and chained+.Added
testListAddInContext. Invalid cases still error, e.g.y: list[int] = [0] + ["x"].