Type refinement does not work with `None in [a, b, c]`, `any()`, or `all()` checks
まだ誰も着手していません。
評価
調査の方向性
None のメンバーシップチェックと all() チェックの再現例から始め、次に、これらの条件式に対する mypy の型絞り込み処理を追跡します。チェック後に、報告された変数が Optional[str] と Optional[int] から絞り込まれ、示されているエラーが発生しなくなれば完了です。
索引モデルが issue の本文から書いたものです。
説明
When checking if multiple variables are not None using the syntax None in [a, b, c], mypy fails to refine the types of these variables to non-Optional types after this check. This issue persists even when using any() or all() for similar checks, preventing mypy from correctly inferring that the variables cannot be None past these checks, despite the code logically ensuring that these variables are not None.
Code to Reproduce:
from typing import Optional
def init_vars(
a: Optional[str] = None,
b: Optional[int] = None,
c: Optional[int] = None,
) -> None:
if None in [a, b, c]:
raise ValueError("a, b, and c cannot be None")
# Expected: a, b, c should not be Optional types beyond this point
# Actual: mypy still treats a, b, c as Optional types
print(a.upper()) # mypy error: Item "None" of "Optional[str]" has no attribute "upper"
print(b + 1) # mypy error: Unsupported operand type(s) for +: 'int' and 'NoneType'
print(c + 1) # mypy error: Unsupported operand type(s) for +: 'int' and 'NoneType'
And here is another example with the all function (with any it is quite similar):
def init_vars_with_all(
a: Optional[str] = None,
b: Optional[int] = None,
c: Optional[int] = None,
) -> None:
if not all(x is not None for x in [a, b, c]):
raise ValueError("a, b, and c cannot be None")
# Expected: a, b, c should not be Optional types beyond this point
# Actual: mypy still treats a, b, c as Optional types
print(a.upper()) # mypy error: same as above
print(b + 1) # mypy error: same as above
print(c + 1) # mypy error: same as above
Expected Behavior:
After the checks if None in [a, b, c]: or using all(), mypy should refine the types of a, b, and c to str, int, and int respectively, acknowledging that they cannot be None.
Actual Behavior:
mypy does not refine the types and continues to treat a, b, and c as Optional[str], Optional[int], and Optional[int] respectively. This results in type errors when attempting to use these variables in a context that does not allow None.
Additional Information:
mypy version: 1.9.0 compiled
Python version: 3.10
No flags or configurations are altering the default behavior of mypy in this instance.
This behavior suggests a limitation in mypy's type inference system when it comes to using collective None checks with list membership or aggregate functions like any() and all(). An enhancement to allow type refinement in such cases would significantly improve usability in scenarios involving initialization checks for multiple variables.
- 主要言語
- 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