python / python/mypy

Within Annotated[T, x], type errors in x are completely ignored

Offen
#16,094 2 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-runtime-semantics
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

mypy ignores the x expression in Annotated[T, x] without type-checking it. Although x has no effect on the typing interpretation of T, the expression x is still code that’s going to be executed at runtime*, so mypy should type-check x (as a separate object) before ignoring it.

(* Although it might not be executed immediately if from __future__ import annotations is in effect, it will be executed when typing.get_type_hints is used, so it’s still important for mypy to check it.)

An example of a real subtle bug that was missed because of this is zulip/zulip#26710.

To Reproduce

from typing import Annotated

def f(p: Annotated[int, 2 + "oops"]) -> None:
    pass

mypy Playground, Pyright playground

Expected Behavior

I expect a type error at 2 + "oops". (Pyright agrees.)

Actual Behavior

mypy finds no errors.

Your Environment

  • Mypy version used: 1.5.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.11.4

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 damit, das Beispiel Annotated[int, 2 + "oops"] unter Python 3.11 mit --strict zu reproduzieren, und verfolge anschließend mypys Verarbeitung von Annotated-Metadaten. Die Aufgabe ist erfüllt, wenn mypy einen Typfehler für den Metadatenausdruck meldet und dabei die Typing-Interpretation von int beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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