naturalsize() truncates non-integer byte counts below the base ("1 Bytes")

未关闭 适合新手
#459 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

  • #507 来自 @ekanshul —— 已关闭,未合并

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 naturalsize() 的实现及其测试入手;issue 指出 int() 转换和单数检查是相关行为。比较大于基数和小于基数的值所经过的格式化路径,然后为分数形式的字节数添加测试,包括 gnu=True。完成标准是:分数形式的输入保留其小数部分,而整数输入保持当前输出。

由索引模型根据 Issue 内容生成。

描述

On current main:

>>> import humanize
>>> humanize.naturalsize(1.5)
'1 Bytes'
>>> humanize.naturalsize(1.9)
'1 Bytes'
>>> humanize.naturalsize(1500)
'1.5 kB'

From 1 kB upwards the fraction is kept using format, but below the base the value is truncated with int(). The singular check also compares the untruncated value with 1, so 1.5 is shown as "1 Bytes".

In #239 you mentioned that "1.1 Bytes" would be better than "1 Bytes" for non-integer input, and that tests for non-integer input were missing.

Proposal:

  • Format non-integer values below the base with format, like larger units (e.g. "1.5 Bytes", and "1.5B" with gnu=True).
  • Keep integer input unchanged ("300 Bytes", "1 Byte").
  • Add tests for non-integer input.

Would you be happy with this? If so, we'd like to send a PR.

主要语言
Python
星标
768
派生
243
平均合并
4 天 12 小时
30 天内合并 PR
19

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-humanize/humanize 的其他 Issue

查看 python-humanize/humanize 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。