TypeIs and TypeGuard fail to narrow type[C]
オープン
まだ誰も着手していません。
bug
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
説明
Bug Report
A TypeIs/TypeGuard function fails to narrow a type[A] expression. A plain if guard block treats the branch as unreachable instead. A comprehension filter keeps the unnarrowed type instead.
To Reproduce
from typing_extensions import TypeIs
class A: ...
class M: ...
class C(M, A): ...
def is_m(t: type) -> TypeIs[type[M]]:
return issubclass(t, M)
cls: type[A] = C
if is_m(cls):
reveal_type(cls)
Expected Behavior
reveal_type(cls) reports type[<subclass of "A" and "M">].
Actual Behavior
Success: no issues found in 1 source file
No reveal_type note at all -- the branch is silently treated as unreachable.
Second repro (comprehension)
from typing import Type
from typing_extensions import TypeIs
class A: ...
class M: ...
class B(A): ...
class C(M, A): ...
def is_m(t: type) -> TypeIs[Type[M]]:
return issubclass(t, M)
alist: list[Type[A]] = [B, C]
mlist: list[Type[M]] = [cls for cls in alist if is_m(cls)]
reveal_type(mlist)
Expected Behavior
No error, mlist is list[type[M]].
Actual Behavior
error: List comprehension has incompatible type List[type[A]]; expected List[type[M]] [misc]
Environment
- Mypy version used: mypy 2.2.0+dev (main branch)
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): nothing special - Python version used: 3.12
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
プロジェクトファイルもテストも指定されていません。まず、型の絞り込みに関するテストと実装から、TypeIs、TypeGuard、条件分岐、内包表記のフィルターを検索し、その後、mypy で両方の再現を実行してください。1 つ目の reveal_type が期待される交差型を報告し、2 つ目がエラーなしで list[type[M]] を推論すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers, devtools
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 静か
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 55/100