Narrow more sequence parents#21327
Merged
hauntsaninja merged 2 commits intopython:masterfrom Apr 25, 2026
Merged
Conversation
Collaborator
Author
|
This looks great on primer! |
This comment has been minimized.
This comment has been minimized.
ilevkivskyi
reviewed
Apr 25, 2026
|
|
||
| def f4(x: list[A] | list[B]): | ||
| # Technically this is unsound in the presence of multiple inheritance, | ||
| # but we already do this kind of narrowing for tuples |
Member
There was a problem hiding this comment.
What about a situation like this:
class Super: ...
class Sub(Super): ...
def foo(x: list[Sub] | list[Super]) -> None:
if isinstance(x[0], Sub):
...Will this cause a narrowing here? I would say we should not narrow. Same question about list[str] | list[str | None] with x[0] is not None.
Collaborator
Author
There was a problem hiding this comment.
No it won't (from the pre-existing logic). I can add a test case for it though
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: spark (https://github.com/apache/spark)
+ python/pyspark/sql/connect/plan.py:1151: error: Redundant cast to "Column" [redundant-cast]
+ python/pyspark/sql/connect/plan.py:1233: error: Redundant cast to "list[str]" [redundant-cast]
+ python/pyspark/sql/connect/plan.py:1236: error: Redundant cast to "Column" [redundant-cast]
zulip (https://github.com/zulip/zulip)
+ zerver/views/streams.py:131: error: Unused "type: ignore" comment [unused-ignore]
+ zerver/views/streams.py:141: error: Unused "type: ignore" comment [unused-ignore]
sympy (https://github.com/sympy/sympy)
+ sympy/matrices/matrixbase.py:2554: error: Unused "type: ignore" comment [unused-ignore]
jax (https://github.com/google/jax)
- jax/_src/pallas/core.py:1190: error: Argument 1 to "unzip2" has incompatible type "tuple[tuple[Hashable, int], ...] | tuple[int | Array, ...]"; expected "Iterable[tuple[Hashable, int]]" [arg-type]
ibis (https://github.com/ibis-project/ibis)
- ibis/backends/materialize/api.py:166: error: Incompatible types in assignment (expression has type "list[tuple[str | tuple[str, bool], bool]]", variable has type "list[str] | str | list[tuple[str, bool]]") [assignment]
paasta (https://github.com/yelp/paasta)
+ paasta_tools/kubernetes_tools.py:683: error: Redundant cast to "list[str]" [redundant-cast]
|
ilevkivskyi
approved these changes
Apr 25, 2026
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 #11436