python / python/cpython

Self-documenting expressions conversion + format-spec

未關閉
#131,176 13 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

interpreter-core topic-parser type-feature
主要語言
Python
星號
77.2k
分支
35.9k
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. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

在 Python 3.13 上重現報告中的 f-string 轉換、填補和對齊案例,然後追蹤 CPython 中 f-string 解析和格式化的進入點。新增涵蓋所提供預期結果的回歸測試;完成標準是,包含冒號的自我說明運算式應與無冒號轉換相符,並一致地格式化完整運算式。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
compilers
Issue 類型
缺陷
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。