Typing narrowing fails depending on the order of if clauses
まだ誰も着手していません。
評価
調査の方向性
まず、mypy 1.4.1 を使って bug.py の再現を実行するか、リンクされている mypy-play の例を実行し、失敗する節の順序と動作する節の順序を比較します。if 文を通じて string、string_a、string_b の型の絞り込みを追跡します。報告されている arg-type エラーが、意図した動作を変更せずに解消されれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug Report
Type narrowing fails in the code below.
To Reproduce
def takes_one_str(string: str) -> str:
return string
# just the problematic case -> failing
def takes_two_optional_str0a(string: str | None, whatever: bool) -> str:
if not (whatever and (string is None)) and whatever:
return takes_one_str(string)
return ""
# reformulated by reversing the order of if clauses -> working
def takes_two_optional_str0b(string: str | None, whatever: bool) -> str:
if whatever and not (whatever and (string is None)):
return takes_one_str(string)
return ""
# what was I actually trying to achieve -> failing
def takes_two_optional_str1(string_a: str | None, string_b: str | None) -> str:
if (string_a is None) and (string_b is None):
return takes_one_str("")
if string_a is None:
# false-positive arg-type (same without return, with elif)
# string_b cannot be None
return takes_one_str(string_b)
if string_b is None:
return takes_one_str(string_a)
return takes_one_str(string_a + string_b)
# workaround variant -> working
def takes_two_optional_str2(string_a: str | None, string_b: str | None) -> str:
if (string_a is not None) and (string_b is not None):
return takes_one_str(string_a + string_b)
if string_a is not None:
# false-positive arg-type (same without return, with elif)
return takes_one_str(string_a)
if string_b is not None:
return takes_one_str(string_b)
return takes_one_str("")
https://mypy-play.net/?mypy=latest&python=3.11&gist=3f3ce6b3021c1a71a4ecf31d35f62a98
Expected Behavior
No errors
Actual Behavior
bug.py:8: error: Argument 1 to "takes_one_str" has incompatible type "str | None"; expected "str" [arg-type]
bug.py:26: error: Argument 1 to "takes_one_str" has incompatible type "str | None"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 1.4.1 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.4
- 主要言語
- 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
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
zostera/django-bootstrap4#894 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
use-agent-os/agent-os#3276 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
NousResearch/hermes-agent#117848 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
zilliztech/memsearch#759 ·