python / python/mypy

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

Offen
#9,785 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-disallow-any
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Führe zunächst mypy 0.790 mit --disallow-any-generics für den Reproducer in t.py aus und untersuche den offengelegten defaultdict-Typ. Verfolge die Behandlung von collections.defaultdict(list) und die generische Typinferenz; abgeschlossen ist die Aufgabe, wenn der Befehl die beabsichtigte Diagnose meldet oder den Schlüsseltyp konsistent mit dem erwarteten Verhalten des Issues ableitet.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.