python / python/mypy

Explicit Any check for TypeGuard

Offen
#12,049 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

Explicit Any check does not examine the type argument of a typeguard. An example is,

def foo(x: object) -> TypeGuard[Any]:
  ...

when run with mypy --disallow-explicit-any raises no error.

Expected Behavior

I would expect 1 type error on the function definition line.

Your Environment

Using master mypy.

I'm debugging this as the issue was brought up in this pr, that TypeGuardType currently does not implement accept. If it did check TypeGuard type for explicit Any it would end up crashing. So there's two fixes needed here, 1. Check typeguard argument, and 2. Add an accept.

The function signature is checked for Any, but it happens after normalization of typeguard to bool. By the time the Any check occurs on this line while the function signature looks like this,

def (_mypy_marker: Union[builtins.int, builtins.str]) -> TypeGuard[Any]

but if you check ret_type of typ object it shows bool.

My guess is a fix could be done around here, https://github.com/python/mypy/blob/619d9bdf7ae70ce953f1a10aebccfdc3526b7200/mypy/typeanal.py#L591 when typeguard argument get analyzed. Alternatively normalization to bool could be avoided, but removing normalization is probably a bigger change.

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

Beginne in mypy/typeanal.py bei der TypeGuard-Analyse in der Nähe der referenzierten Zeilen und prüfe, wie TypeGuardType normalisiert wird und ob es accept implementiert. Reproduziere das Beispiel mit --disallow-explicit-any; fertig ist es, wenn TypeGuard[Any] einen Typfehler bei der Funktionsdefinition erzeugt, ohne während der Analyse abzustürzen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
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.