Return integer byte values from StringToSize.to_bytes()#2997
Open
officialasishkumar wants to merge 1 commit into
Open
Return integer byte values from StringToSize.to_bytes()#2997officialasishkumar wants to merge 1 commit into
officialasishkumar wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
kiwi.utils.size.StringToSize.to_bytes()was documented to return anint, but usedmath.pow(1024, n)to scale the unit suffix, which always returns afloat. Every call with anm/gsuffix returned afloatand that float propagated to callers whose return type was annotated-> int(e.g.XMLState.get_build_type_unpartitioned_bytes).DiskFormatBase.resize_raw_disk(..., append=True), which formats the value directly into theqemu-img resizeargument ('+{0}'.format(size_bytes)), producing arguments with a trailing.0(e.g.+104857600.0). The log output ofimage_resizehad the same issue.math.pow(1024, n)with the integer expression1024 ** n, adds proper type annotations (size_value: str->int), and assertsisinstance(..., 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 passedpytest 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 passedmypy kiwi→ no issues found in 213 source filesStringToSize.to_bytes('100m')now returns104857600(int) and the formatted append argument is+104857600rather than+104857600.0