python-humanize / python-humanize/humanize

`naturalsize()` raises `ValueError` for custom format strings with text

Đang mở Phù hợp với người mới
#366 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
Python
Star
756
Fork
148
Merge trung bình
6 ngày 1 giờ
Pull request đã merge (30 ngày)
12

Mô tả

Environment

  • humanize 4.16.0 and current main at 42b4a1db
  • 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.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu từ điểm vào naturalsize() và kiểm tra phần xử lý chuyển đơn vị được đưa vào trong PR #329, đặc biệt là nhánh định dạng tùy chỉnh được mô tả trong báo cáo. Bổ sung kiểm thử hồi quy cho các chuỗi định dạng có cả tiền tố và hậu tố, sau đó chạy bộ kiểm thử liên quan; công việc được xem là hoàn tất khi các định dạng này giữ nguyên hành vi trước phiên bản 4.16, đồng thời việc chuyển đơn vị vẫn hoạt động đối với các định dạng số thuần túy.

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
api
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Ít trao đổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
75/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.