mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
ByteSize.human_readable() should not have decimal point with B suffix (#4777)
ByteSize can only represent whole number of bytes, so displaying a decimal would be misleading and doesn't really make sense in most cases where ByteSize is used. Outside pure mathematics, it is very rare to use non-whole number bytes. Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
+4
-1
@@ -694,7 +694,10 @@ class ByteSize(int):
|
||||
num = float(self)
|
||||
for unit in units:
|
||||
if abs(num) < divisor:
|
||||
return f'{num:0.1f}{unit}'
|
||||
if unit == 'B':
|
||||
return f'{num:0.0f}{unit}'
|
||||
else:
|
||||
return f'{num:0.1f}{unit}'
|
||||
num /= divisor
|
||||
|
||||
return f'{num:0.1f}{final_unit}'
|
||||
|
||||
+3
-3
@@ -3252,9 +3252,9 @@ def test_frozenset_field_not_convertible():
|
||||
@pytest.mark.parametrize(
|
||||
'input_value,output,human_bin,human_dec',
|
||||
(
|
||||
('1', 1, '1.0B', '1.0B'),
|
||||
('1.0', 1, '1.0B', '1.0B'),
|
||||
('1b', 1, '1.0B', '1.0B'),
|
||||
('1', 1, '1B', '1B'),
|
||||
('1.0', 1, '1B', '1B'),
|
||||
('1b', 1, '1B', '1B'),
|
||||
('1.5 KB', int(1.5e3), '1.5KiB', '1.5KB'),
|
||||
('1.5 K', int(1.5e3), '1.5KiB', '1.5KB'),
|
||||
('1.5 MB', int(1.5e6), '1.4MiB', '1.5MB'),
|
||||
|
||||
Reference in New Issue
Block a user