Skip to content

Narrow more sequence parents#21327

Merged
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:narrowseqparent
Apr 25, 2026
Merged

Narrow more sequence parents#21327
hauntsaninja merged 2 commits intopython:masterfrom
hauntsaninja:narrowseqparent

Conversation

@hauntsaninja
Copy link
Copy Markdown
Collaborator

@hauntsaninja hauntsaninja commented Apr 25, 2026

Fixes #11436

@hauntsaninja
Copy link
Copy Markdown
Collaborator Author

This looks great on primer!

@github-actions

This comment has been minimized.


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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it won't (from the pre-existing logic). I can add a test case for it though

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks!

@github-actions
Copy link
Copy Markdown
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]

@hauntsaninja hauntsaninja merged commit 273e8c8 into python:master Apr 25, 2026
24 checks passed
@hauntsaninja hauntsaninja deleted the narrowseqparent branch April 25, 2026 20:25
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.

narrowing union of lists by type of an item in the list doesn't work

2 participants