Self-documenting expressions conversion + format-spec
未关闭
还没有人认领这个 Issue。
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
在 Python 3.13 上复现报告中的 f-string 转换、填充和对齐案例,然后跟踪 CPython 中 f-string 解析和格式化的入口点。添加涵盖所提供预期结果的回归测试;完成标准是,带冒号的自文档表达式应与无冒号转换相匹配,并一致地格式化完整表达式。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 缺陷
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100