Empty Collection Handling with `next`
オープン
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 5.1k
- フォーク
- 2.1k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 82
説明
There are some issues when using next when providing an empty collection as a default, let's take this example:
from collections.abc import Iterable
def foo(iter: Iterable[list[int]]) -> None:
next((item for item in iter if len(item) > 5), [])
This leads to errors in Pyright, as [] is treated as list[unknown]. Pyright Playground.
Here are the current next overloads:
@overload
def next(i: SupportsNext[_T], /) -> _T: ...
@overload
def next(i: SupportsNext[_T], default: _VT, /) -> _T | _VT: ...
I would propose changing the second one to:
def next(i: SupportsNext[_T], default: _VT | _T, /) -> _T | _VT: ...
This would fix the above issue, as in the case an empty list or othe rcollection Pyright could just infer the type as _T, while not disrupting other cases in which the types differ.
With this change in Pyright:
from typing import reveal_type
def foo(iter: Iterable[list[int]]) -> None:
result = next((item for item in iter if len(item) > 5), [])
reveal_type(result) # Revealed type is `list[int]`.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Issue に示されている現在の 2 つの next オーバーロードから始め、リンクされている Pyright Playground で例を再現します。推論された型を reveal_type の期待値と比較します。空のデフォルト値が受け入れられ、結果が list[int] として推論され、デフォルト型が異なるケースに影響を与えなければ完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- tooling
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 52/100