Skip to content

Return integer byte values from StringToSize.to_bytes()#2997

Open
officialasishkumar wants to merge 1 commit into
OSInside:mainfrom
officialasishkumar:fix_string_to_size_returns_float
Open

Return integer byte values from StringToSize.to_bytes()#2997
officialasishkumar wants to merge 1 commit into
OSInside:mainfrom
officialasishkumar:fix_string_to_size_returns_float

Conversation

@officialasishkumar
Copy link
Copy Markdown

Summary

  • kiwi.utils.size.StringToSize.to_bytes() was documented to return an int, but used math.pow(1024, n) to scale the unit suffix, which always returns a float. Every call with an m/g suffix returned a float and that float propagated to callers whose return type was annotated -> int (e.g. XMLState.get_build_type_unpartitioned_bytes).
  • The float value reached DiskFormatBase.resize_raw_disk(..., append=True), which formats the value directly into the qemu-img resize argument ('+{0}'.format(size_bytes)), producing arguments with a trailing .0 (e.g. +104857600.0). The log output of image_resize had the same issue.
  • Replaces math.pow(1024, n) with the integer expression 1024 ** n, adds proper type annotations (size_value: str -> int), and asserts isinstance(..., int) in the unit tests for both unitless and unit-suffixed inputs.

Fixes Issue#2996

Test plan

  • pytest test/unit/utils/size_test.py → 3 passed
  • pytest test/unit/xml_state_test.py test/unit/tasks/image_resize_test.py test/unit/builder/disk_test.py test/unit/runtime_config_test.py test/unit/filesystem/setup_test.py → 205 passed
  • mypy kiwi → no issues found in 213 source files
  • Manual sanity check confirms StringToSize.to_bytes('100m') now returns 104857600 (int) and the formatted append argument is +104857600 rather than +104857600.0

StringToSize.to_bytes() is documented as returning an int, and
downstream callers such as XMLState.get_build_type_unpartitioned_bytes
declare a -> int return type. The implementation, however, used
math.pow(1024, n) to apply the unit multiplier, which always returns a
float. As a result every call with an m/M/g/G suffix returned a float
rather than an int, in violation of the docstring and the annotated
return type of the callers.

The float value propagated through the codebase. In particular,
DiskBuilder.append_unpartitioned_space() forwards the value from
<size unpartitioned="..." unit="M|G"> to
DiskFormatBase.resize_raw_disk(..., append=True), which formats it
straight into the qemu-img resize argument as

    '+{0}'.format(size_bytes)

so the resulting argument carried a trailing '.0' (e.g.
'+104857600.0'). The image_resize task and any other consumer
formatting the value as a string saw the same trailing '.0'.

Replace math.pow(1024, n) with the integer expression 1024 ** n so the
function consistently returns an int for every supported input, add a
proper type annotation (size_value: str) -> int, and extend the unit
tests to assert isinstance(int) for both the unitless and unit-suffixed
inputs to guard against regressions.

Fixes OSInside#2996
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant