fractions: Should we support unicode in width/precision formatting fields?
未關閉
還沒有人認領這個 Issue。
stdlib
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
Currently, specification allows only [0-9] digits. Though, actual implementation permits unicode symbols for float/Decimal's, but not Fraction's:
>>> f"{decimal.Decimal('123'):.١١f}" # arabic 11 in precision
'123.00000000000'
>>> f"{fractions.Fraction('123'):.١١f}"
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
f"{fractions.Fraction('123'):.١١f}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sk/src/cpython/Lib/fractions.py", line 600, in __format__
raise ValueError(
...<2 lines>...
)
ValueError: Invalid format specifier '.١١f' for object of type 'Fraction'
>>> f"{float(fractions.Fraction('123')):.١١f}"
'123.00000000000'
Quick tests shows no measurable performance penalty with unicode support:
$ python -m timeit -s 'from fractions import Fraction as F' 'format(F(123), ".11f")'
10000 loops, best of 5: 39.2 usec per loop
$ python -m timeit -s 'from fractions import Fraction as F' 'format(F(123), ".١١f")' # with patch
5000 loops, best of 5: 40.2 usec per loop
a patch
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 063f28478c..b4120b2beb 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -170,7 +170,7 @@ def _round_to_figures(n, d, figures):
(?P<zeropad>0(?=[0-9]))?
(?P<minimumwidth>0|[1-9][0-9]*)?
(?P<thousands_sep>[,_])?
- (?:\.(?P<precision>0|[1-9][0-9]*))?
+ (?:\.(?P<precision>0|\d*))?
(?P<presentation_type>[eEfFgG%])
""", re.DOTALL | re.VERBOSE).fullmatch
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response
Linked PRs
- gh-140654
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先檢查 Lib/fractions.py 中的格式規範正規表示式以及連結的 PR gh-140654。重現 Fraction、Decimal 和 float 使用阿拉伯數字精度的範例,然後將結果與文件記載的格式規範進行比較。完成的標準是 Fraction 一致地接受預期的 Unicode 精度語法。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- backend
- Issue 類型
- 缺陷
- 難度
- 1/5
- 預估耗時
- 1 小時以內
- 活躍度
- 停滯
- 描述清晰度
- 描述清楚
- 新手友好度
- 25/100