Inference for Literal arguments not handled
オープン
まだ誰も着手していません。
bug
topic-literal-types
- 主要言語
- Python
- スター
- 20.6k
- フォーク
- 3.3k
- PR マージ指標
- PR 指標を取得中
説明
Bug Report
If you have a function with Literal arguments, and a variable that is set to one of the options, an error is reported.
To Reproduce
from typing import Literal
def myfun(a: Literal["a", "b", "c"]) -> str:
return a
s = "a"
myfun(s)
def myfun2(a: Literal[1, 2]) -> int:
return a
b = 2
myfun2(b)
c = 1 if s == "a" else 2
myfun2(c)
reports
literalinf.py:10: error: Argument 1 to "myfun" has incompatible type "str"; expected "Union[Literal['a'], Literal['b'], Literal['c']]"
literalinf.py:18: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
literalinf.py:21: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
Expected Behavior
No error. The values can be inferred to be of the Literal value
Actual Behavior
See above.
Your Environment
- Mypy version used: 0.910
- 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 21H1
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提供された Python 再現コードを mypy で実行し、Literal 引数をチェックするときに代入された値と条件式がどのように推論されるかを追跡します。3 つの呼び出しをカバーするテストを追加し、報告された互換性のない型エラーがそれらで発生しなくなった時点で作業完了とします。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- devtools
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 48/100