netutils icon indicating copy to clipboard operation
netutils copied to clipboard

Base bandwidth units convert to zero and byte formatting uses inconsistent scales

Open omrzk opened this issue 2 weeks ago • 0 comments

Environment

  • Python version: 3.13.18
  • netutils version: develop 09d6fbe (1.19.3a0)

Expected Behavior

Base units have factors of one bit/s and eight bits/s per byte/s. Byte formatting uses decimal SI prefixes at 1,000 bytes/s.

Observed Behavior

The bandwidth helpers treat the zero lower bound of the base-unit ranges as a conversion multiplier:

>>> name_to_bits("100bps")
0
>>> name_to_bits("100Bps")
0
>>> name_to_bytes("100Bps")
0.0
>>> name_to_name("1Kbps", "Bps")
'1000Bps'

These should represent 100 bits/s, 800 bits/s, 100 bytes/s, and 125 bytes/s, respectively.

The byte formatter also compares a byte input to bit-based range thresholds and scales the base output by eight:

>>> bytes_to_name(950.0)
'7600.0Bps'
>>> bytes_to_name(1000.0)
'8000.0Bps'

This contradicts the docstring's bytes/s contract and its existing bytes_to_name(10000.0) == '10.0KBps' example. Formatting then parsing 950 bytes/s through the public Jinja filters produces zero, rather than the original value.

Reproduced on current develop 09d6fbe452b392f49272e802f6e7593a1562a739, CPython 3.13.18. I have a focused fix and regression tests for base-unit parsing, bit/byte conversion, decimal SI thresholds, and the public Jinja round trip; existing prefixed and invalid-input tests remain passing.

Steps to Reproduce

Run the examples above after from netutils.bandwidth import name_to_bits, name_to_bytes, name_to_name, bytes_to_name. The existing tests currently assert the incorrect byte formatter outputs; the documented 10,000.0 bytes/s example returns the expected 10.0KBps.

omrzk avatar Nov 19 '26 15:11 omrzk