python-humanize / python-humanize/humanize
`naturalsize()` raises `ValueError` for custom format strings with text
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 756
- フォーク
- 148
- 平均マージ
- 6日 1時間
- マージ済み PR(30日)
- 12
説明
Environment
- humanize 4.16.0 and current
mainat42b4a1db - Python 3.11.15
Reproduction
import humanize
print(humanize.naturalsize(999_999, gnu=True, format="Size: %.1f"))
humanize 4.15.0 returns:
Size: 976.6K
humanize 4.16.0 and current main raise:
ValueError: could not convert string to float: 'Size: 976.6'
Expected behavior
The documented custom formatter should continue to accept text around the numeric conversion.
Since this behavior worked in 4.15.0, preserving it would avoid a backward compatibility regression.
Cause
PR #329 added unit rollover handling. The new condition formats the mantissa with the
user-provided format and passes the entire result to float():
float(format % (abs_bytes / (base**exp)))
This works for a plain numeric format such as "%.1f", but not for a general formatter
such as "Size: %.1f".
Suggested fix
It might be preferable not to require the complete user-formatted result to be parseable as a float. One
backward-compatible option is to apply rollover only when the formatted result is a
plain numeric string, and otherwise preserve the pre-4.16 behavior. A more complete
solution could derive the rounded numeric mantissa independently from surrounding text.
It might also be worth adding a regression test covering both a prefix and a suffix.
How this was found
This was found by change-directed differential testing: reviewing the recent rollover change, mapping it to the public naturalsize() API, and constructing a documented custom formatter that reaches the newly added float(format % value) expression.
Related: #329, which fixes rollover but does not discuss custom format strings.
Thanks for taking the time to review this report! I appreciate all the work that goes into maintaining this project.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
naturalsize() のエントリーポイントから始め、PR #329 で導入された単位の繰り上げ処理を調べてください。特に、報告で説明されているカスタム形式のパスを確認します。プレフィックスとサフィックスの両方を含むフォーマット文字列の回帰テストを追加し、その後、関連するテストスイートを実行してください。これらの形式が 4.16 より前の動作を維持し、通常の数値形式では繰り上げが引き続き機能すれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- api
- issue の種類
- バグ
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 静か
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 75/100