python / python/mypy

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

Aperta
#16,094 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

feature topic-runtime-semantics
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'esempio Annotated[int, 2 + "oops"] con Python 3.11 e --strict, quindi traccia la gestione dei metadati di Annotated da parte di mypy. Il lavoro è completato quando mypy segnala un errore di tipo per l'espressione dei metadati, preservando al contempo l'interpretazione typing di int.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
devtools
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.