python / python/mypy

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

オープン
#7,479 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug false-positive priority-2-low
主要言語
Python
スター
20.6k
フォーク
3.3k
平均マージ
1日 18時間
マージ済み PR(30日)
54

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ジェネリックな foo と str.format の例を使って、テストケース testFormatCallGenerics の動作を再現します。mypy による str.format 呼び出しのチェックと外側のコンテキストの扱いを追跡します。完了条件は、有効な float のフォーマット呼び出しで誤検出が発生しなくなり、意図したフォーマット指定子のエラーが引き続きカバーされることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。