Self-documenting expressions conversion + format-spec
Đang mở
Chưa có ai nhận issue này.
interpreter-core
topic-parser
type-feature
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Tái hiện các trường hợp chuyển đổi, padding và căn chỉnh f-string đã được báo cáo trên Python 3.13, sau đó lần theo các điểm vào của quá trình phân tích cú pháp và định dạng f-string trong CPython. Thêm các bài kiểm thử hồi quy bao quát những kết quả mong đợi đã cung cấp; hoàn thành có nghĩa là các biểu thức tự mô tả chứa dấu hai chấm khớp với phép chuyển đổi không có dấu hai chấm và định dạng nhất quán toàn bộ biểu thức.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- compilers
- Loại issue
- Lỗi
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100