Incorrect constraint inference for unions with nested generics
まだ誰も着手していません。
評価
調査の方向性
main.py で最小限の再現から始め、mypy で func([1]) と func([[1]]) の動作が異なることを確認してください。Union[List[_T], List[List[_T]]] の制約推論の経路を追跡し、関連する issue #6417 およびリンクされている Tornado の pull request にある overload の回避策と比較してください。両方の呼び出しがエラーなしで型チェックを通れば完了です。
索引モデルが issue の本文から書いたものです。
説明
🐛 Bug Report
From my search, it looks like an issue similar to #6417, but happening to unions of generic and nested generic.
mypy has troubles with inferring a proper constraint and passing a nested generic to function that should accept such nested generic fails.
The issue can be worked around with @overloads.
To Reproduce
Minimum reproduction:
from typing import List, TypeVar, Union
_T = TypeVar("_T")
def func(param: Union[List[_T], List[List[_T]]]) -> _T:
...
func([1])
func([[1]])
A more real-live example of where this is an issue is tornado's gen function (overloads there are the part of workaround for this issue):
https://github.com/tornadoweb/tornado/pull/2909/files
Expected Behavior
No errors.
Actual Behavior
main.py:10: error: Argument 1 to "func" has incompatible type "List[List[int]]"; expected "Union[List[<nothing>], List[List[<nothing>]]]"
Your Environment
- Mypy version used: 0.781
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.8.5
- Operating system and version: Windows 10
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- 平均マージ
- 1日 18時間
- マージ済み PR(30日)
- 54
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python/mypy のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
bug topic-configuration topic-error-reporting
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
似ている issue
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
bancolombia/sentinel#23 ·
-
test md オープンCI
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
langchain-ai/deepagents#6450 ·
-
bug client
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100