Inconsistent "Incompatible return type" error when `List[int]` is returned for `List[Union[str, int]]` when layered up with Tuple/Union
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
When layering up some Union/Tuple checks, at some point the type checking suddenly gets a lot more strict and starts failing, as if it's unable to fully parse the type checks anymore.
It's possibly related to this, but it doesn't explain why the behaviour changes only once it's wrapped under other things: https://github.com/python/mypy/issues/3351
To Reproduce
Here's a minimal reproducible example that shows how it works fine until it suddenly doesn't.
from typing import Union, Tuple, List
# Just to reduce the noise
S = Union[int, str]
L = List[S]
# It works with `([int],)`
def func_works1() -> Tuple[L]:
return [1],
# It works with `([int, str],)`
def func_works2() -> Tuple[L]:
return [1, 'a'],
# It works with `int | [int]`
def func_works3() -> Union[S, L]:
return [1]
# It works with `(int,) | ([int],)` if returning (int,)
def func_works4() -> Union[Tuple[S], Tuple[L]]:
return 1,
# It fails with `(int,) | ([int],)` if returning ([int],)
def func_fail1() -> Union[Tuple[S], Tuple[L]]:
return [1],
main.py:21: error: Incompatible return value type (got "tuple[list[int]]", expected "tuple[int | str] | tuple[list[int | str]]") [return-value]
# It fails with `(int,) | ([int],)` if returning ([int, str],)
def func_fail2() -> Union[Tuple[S], Tuple[L]]:
return [1, 'a'],
main.py:25: error: Incompatible return value type (got "tuple[list[object]]", expected "tuple[int | str] | tuple[list[int | str]]") [return-value]
https://mypy-play.net/?mypy=latest&python=3.12&gist=9477b2d4dab01919bef1aa6d2911f648
Expected Behavior
It passes the test.
Actual Behavior
In the code above. It complains that List[int] is not a valid List[Union[int | str]]. According to the issue linked above, this may not be wrong, but the behaviour is inconsistent as to when it flags it as wrong.
Your Environment
- Mypy version used: 1.11.2
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): I just runpy -m mypywithout any config files. - Python version used: 3.7, 3.11
Edit
This is nothing to do with the bug report, but in case anyone finds this issue with a similar use case, this is the fully working solution I've ended up with:
S = Union[int, str]
T = TypeVar('T', S, List[S])
FuncType = Tuple[T, T]
def func() -> FuncType:
return a, b
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず mypy 1.11.2 で最小の再現例を実行し、ラップされていない Union/Tuple/List のケースを func_fail1 および func_fail2 と比較します。関連する動作について issue #3351 を読み、その後、ネストされた Union および Tuple 式の型チェック経路を追跡します。報告されたケースが回帰テストとともに一貫してパスすれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 45/100