Use empty outer context when checking str.format() calls
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Reproduziere das Verhalten im Testfall testFormatCallGenerics anhand der generischen foo- und str.format-Beispiele. Verfolge mypys Prüfung von str.format-Aufrufen und den Umgang mit dem äußeren Kontext; fertig bedeutet, dass der gültige Float-Formatierungsaufruf keinen False Positive mehr erzeugt, während der beabsichtigte Fehler beim Formatbezeichner weiterhin abgedeckt ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 45/100