python / python/mypy

`functools.reduce` over sets: empty set not accepted without annotation

オープン
#17,693 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug
主要言語
Python
スター
20.6k
フォーク
3.3k
PR マージ指標
PR 指標を取得中

説明

Reproducer:

from typing import Collection
from functools import reduce


def _pull_out_loop_nest(
            loop_nests: list[frozenset[str]],
            inames_to_pull_out: frozenset[str]
        ) -> None:
    # OK
    emptyset: frozenset[str] = frozenset()
    assert inames_to_pull_out <= reduce(frozenset.union, loop_nests, emptyset)

    # Error
    assert inames_to_pull_out <= reduce(frozenset.union, loop_nests, frozenset())

Here is what mypy says as of 1.11.1:

mypybug.py:14: error: Argument 1 to "reduce" has incompatible type "Callable[[frozenset[_T_co], VarArg(Iterable[_S])], frozenset[_T_co | _S]]"; expected "Callable[[frozenset[Never], frozenset[str]], frozenset[Never]]"  [arg-type]

This seems spurious: It seems that something picks up the type of the empty set somewhat too eagerly. Pyright (1.1.376) also seems to like this code just fine.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

mypybug.py の reproducer から始め、mypy 1.11.1 に対して実行し、注釈付きと注釈なしの空の frozenset のケースを比較します。関係する reduce の型推論を追跡し、注釈なしの形式で報告された診断が生成されなくなったことを示す回帰テストを追加します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
tooling
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。