python / python/mypy

Use empty outer context when checking str.format() calls

Aperta
#7,479 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug false-positive priority-2-low
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

This is a follow up for https://github.com/python/mypy/pull/7418

Because mypy is still too eager about outer context and str.format() has object type for its arguments, passing a call to generic function as an argument to str.format() may cause false positives. For example:

'{:.3f}'.format(sum(list_of_floats))

Fails with

Incompatible types in string interpolation (expression has type "object", placeholder has type "Union[int, float]")

A more rectified test case:

[case testFormatCallGenerics]
from typing import TypeVar, Union

T = TypeVar('T')
def foo(x: T) -> Union[T, int]: ...

x: str
y: float

"{:.3f}".format(foo(x))  # E: Incompatible types in string interpolation (expression has type "str", placeholder has type "Union[int, float]")
"{:.3f}".format(foo(y))
[builtins fixtures/primitives.pyi]

A potential simple fix is to re-accept the expression in empty context before checking the format specifier type.

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

Riproduci il comportamento del test case testFormatCallGenerics usando gli esempi generici di foo e str.format. Traccia il controllo delle chiamate a str.format di mypy e la gestione del contesto esterno; il lavoro è completato quando la chiamata valida di formattazione di float non produce più un falso positivo, mentre l’errore previsto dello specificatore di formato rimane coperto.

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

Valutazione

Stack tecnologico
python
Ambito
compilers
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.