python / python/mypy

--disallow-any-generics is allowing defaultdict[Any, ...]

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

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

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

説明

Bug Report

(A clear and concise description of what the bug is.)

To Reproduce

this python file (I was working on advent of code, this is about as minimal as I could reproduce)

import collections
import re

PATTERN = re.compile('^([^ ]+ [^ ]+) bags contain (.*)$')
BAG_RE = re.compile(r'(\d+) ([^ ]+ [^ ]+)')


def compute(s: str) -> int:
    parents = collections.defaultdict(list)
    for line in s.splitlines():
        match = PATTERN.match(line)
        assert match
        k = match[1]
        targets = [(int(n), tp) for n, tp in BAG_RE.findall(match[2])]
        for _, color in targets:
            parents[color].append(k)

    total_colors = set()
    todo = parents['shiny gold']
    while todo:
        color = todo.pop()
        if color not in total_colors:
            total_colors.add(color)
            todo.extend(parents[color])

    reveal_type(parents)

    return len(total_colors)

Run with mypy --disallow-any-generics t.py

Expected Behavior

I expect an error requiring me to annotate parents = collections.defaultdict(list) because mypy has not inferred the key type (though, I think it should be able to infer that it is str from the code)

Actual Behavior

$ mypy t.py --disallow-any-generics
t.py:26: note: Revealed type is 'collections.defaultdict[Any, builtins.list[builtins.str*]]'

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: --disallow-any-generics
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.8.5
  • Operating system and version: ubuntu 20.04

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

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

はじめの一歩

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

調査の方向性

まず、t.py の再現コードに対して --disallow-any-generics を付けて mypy 0.790 を実行し、明示された defaultdict 型を確認します。collections.defaultdict(list) の処理とジェネリック型推論を追跡します。コマンドが意図した診断を報告するか、issue で期待されている動作と一貫したキー型を推論すれば完了です。

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

評価

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

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

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