python / python/mypy

coverage report produces too many Anys

Offen
#10,325 0 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

When generating a coverage report there are too many Anys reported for lines. I've figured out how it's counting Anys and I may be able to help with a patch.

To Reproduce

  1. def test() -> str:
      value: str = getattr(test, '__name__')
      return value
    
  2. mypy --html-report . test.py
  3. View generated html/test.py.html

Expected Behavior

As mypy reported a success, I would expect no Anys, but maximally I would expect 1.

Actual Behavior

Mypy reports 4 explicit anys on the getattr line.

When stepping through the StatisticsVisitor visit I noticed this is because when visiting a call expression:

https://github.com/python/mypy/blob/d089891198ef470c8bec9bd7d7b50a02757c5b68/mypy/stats.py#L217-L225

  1. The call expression itself is processed, which first starts with the node itself. Because the result of getattr is Any the visitor has Any in its typemap for that node. This is arguably the only Any that I would expect, and it probably should still not be reported because there is an explicit type on the assignment (I do note that assignment could be wrong, and maybe this should be reported as a partial cover).
  2. The callee is then visited which will be processing the node with type def (Any, name: builtins.str, Any =) -> Any, in StatisticsVisitor.type it calls get_proper_types(collect_all_inner_types(t)) + [t] which is [Any, builtins.str, Any, Any, def (Any, name: builtins.str, Any =) -> Any] which will add 3 more Anys to the line. Now that I'm looking at it closer, if I had added Anys to the type definition of test they would increase the number of Anys on the line when visiting the arguments to getattr.

Your Environment

  • Mypy version used: mypy 0.812
  • Mypy command-line flags: --html-report .
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.8.8
  • Operating system and version: N/A

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

Beginnen Sie mit mypy/stats.py, insbesondere mit der im Bericht referenzierten Behandlung von Aufrufausdrücken durch StatisticsVisitor, und reproduzieren Sie den Fall mit mypy --html-report . test.py. Verfolgen Sie, wie die Typen von Aufruf, Callee und Argument zum HTML-Coverage-Bericht beitragen; als erledigt gilt die Aufgabe, wenn das Beispiel die verschachtelten Any-Typen nicht mehr wiederholt zählt und die gemeldete Gesamtsumme dem erwarteten Verhalten entspricht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
testing-qa, tooling
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.