python / python/cpython

Self-documenting expressions conversion + format-spec

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

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

interpreter-core topic-parser type-feature
主要言語
Python
スター
77.2k
フォーク
36k
PR マージ指標
PR 指標を取得中

説明

Bug report

Bug description:
# initialisation
foo = "hello!"
bar = "hello, world!"
long_name = bar
print(f"{foo = }")
print(f"{bar = }")
print()

# these work as I expected
# they default to !r value conversion
print(f"{f"{foo=}" = }")
print(f"{f"{bar=}" = }")
print(f"{(f"{foo=}" == f"{foo=!r}") = }")
print()

# these do not work as I expected
# they default to !s value conversion, instead of !r
print(f"{f"{foo=:}" = }")
print(f"{f"{bar=:}" = }")
print(f"{(f"{foo=:}" == f"{foo=!r}") = }")
print(f"{(f"{foo=:}" == f"{foo=!s}") = }")
print()

# these do not work as I expected
# they apply padding to the expression value
# the expression itself is not taken into account
# as a result, the true string length is dependent on the expression length
print(f"{f"{bar=!r:20}." = }")
print(f"{len(f"{bar=!r:20}.") = }")
print(f"{f"{long_name=!r:20}." = }")
print(f"{len(f"{long_name=!r:20}.") = }")
print()

# these do not work as I expected
# they apply alignment only to the expression value
# the expression itself is always left-aligned
print(f"{f"{bar=!r:>20}" = }")
print(f"{len(f"{bar=!r:>20}") = }")
print(f"{f"{long_name=!r:>20}" = }")
print(f"{len(f"{long_name=!r:>20}") = }")
Expected results
  • Default value conversion mode should be consistent regardless of whether : is present
  • The whole self-documenting expression should be padded / aligned
import unittest
from secrets import token_urlsafe as random_string


class ExpectedResults(unittest.TestCase):
    def test_consistent_default_value_conversion_mode(self):
        my_value = random_string(16)
        first_way = f"{my_value=}"
        second_way = f"{my_value=:}"
        self.assertEqual(first_way, second_way)

    def test_pad_to_length(self):
        my_value = random_string(16)
        assert len(my_value) == 22
        value_only = f"{my_value!r:35}."
        self_documenting = f"{my_value=!r:35}."
        self.assertEqual(len(value_only), len(self_documenting))

    def test_alignment(self):
        my_value = random_string(16)
        assert len(my_value) == 22
        faux_self_documenting = f"{f"my_value={my_value!r}":>35}"
        assert not faux_self_documenting.startswith("my_value=")
        assert len(faux_self_documenting) == 35
        self_documenting = f"{my_value=!r:>35}"
        self.assertEqual(faux_self_documenting, self_documenting)

    def test_consistent_default_value_conversion_mode_exact(self):
        my_value = "report poet ethics"
        expected = "my_value='report poet ethics'"
        actual = f"{my_value=:}"
        self.assertEqual(expected, actual)

    def test_pad_to_length_exact(self):
        my_value = 'sugar lemon dilemma'
        expected = "my_value='sugar lemon dilemma'      "
        assert len(expected) == 36
        actual = f"{my_value=!r:36}"
        self.assertEqual(expected, actual)
        # or alternatively
        self.assertEqual(36, len(actual))

    def test_alignment_exact(self):
        my_value = 'bonus thought skull'
        expected = "   my_value='bonus thought skull'   "
        assert len(expected) == 36
        actual = f"{my_value=!r:^36}"
        self.assertEqual(expected, actual)

if __name__ == "__main__":
    unittest.main()
CPython versions tested on:

3.13

Operating systems tested on:

Linux

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

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

はじめの一歩

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

調査の方向性

Python 3.13 で報告された f-string の変換、パディング、アライメントのケースを再現し、その後 CPython における f-string のパースおよびフォーマット処理のエントリポイントを追跡します。提示された期待結果を網羅する回帰テストを追加します。コロンを含む自己文書化式がコロンなしの変換と一致し、式全体を一貫してフォーマットできれば完了です。

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

評価

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

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

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