Skip to content

fix(utils): reject mi, ni and ui as quantity suffixes - #2748

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
ArfiArfath21:quantity-reject-invalid-binary-suffixes
Oct 18, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
ArfiArfath21:quantity-reject-invalid-binary-suffixes

Conversation

@ArfiArfath21

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

parse_quantity and format_quantity only rejected ki among the binary suffixes, so mi, ni and ui were accepted as base-1024 negative exponents (parse_quantity("1mi") returned 1/1024, format_quantity(Decimal(1), "mi") returned "1024mi"). Binary suffixes are now limited to Ki, Mi, Gi, Ti, Pi and Ei, as in apimachinery's quantity.go. A typo such as 512mi now raises ValueError instead of silently becoming a tiny number.

Which issue(s) this PR fixes:

Fixes #2746

Special notes for your reviewer:

Tests added to TestUtilsUnitTests in kubernetes/e2e_test/test_utils.py; they fail on master and pass with this change.

AI assistance disclosure: prepared with Claude Code; I reviewed the change and ran the tests locally.

Does this PR introduce a user-facing change?

`kubernetes.utils.parse_quantity` and `format_quantity` now raise `ValueError` for the invalid binary suffixes `mi`, `ni` and `ui` instead of accepting them.

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@kubernetes-prow kubernetes-prow Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels Oct 16, 2026
@kubernetes-prow
kubernetes-prow Bot requested review from roycaihw and yliaog October 16, 2026 18:16
@linux-foundation-easycla

linux-foundation-easycla Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: ArfiArfath21 / name: ArfiArfath21 (b976cf7)

@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 16, 2026
@ArfiArfath21
ArfiArfath21 force-pushed the quantity-reject-invalid-binary-suffixes branch from 694d9a8 to c831a6d Compare October 16, 2026 18:17
@ArfiArfath21

Copy link
Copy Markdown
Contributor Author

/easycla

@ArfiArfath21
ArfiArfath21 force-pushed the quantity-reject-invalid-binary-suffixes branch from c831a6d to b976cf7 Compare October 16, 2026 18:23
@ArfiArfath21

Copy link
Copy Markdown
Contributor Author

/easycla

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 16, 2026
Comment thread kubernetes/utils/quantity.py Outdated
# handle SI inconsistency
if suffix == "ki":
# handle SI inconsistency: "ki", "mi", "ni" and "ui" are not valid binary suffixes
if base == 1024 and suffix[0] not in _BINARY_PREFIXES:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need the conditiion: base == 1024 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Yes, it was needed: the check applies only to the binary (i) suffixes, and without the base == 1024 guard the valid base-1000 suffixes m, n, u and k would be rejected too, since they are not in KMGTPE.

That it reads as redundant is a fair point though, so I moved the check into the endswith("i") branch in both parse_quantity and format_quantity. The base == 1024 condition is gone, behaviour is the same, and the existing tests pass.

parse_quantity and format_quantity only rejected 'ki' among the binary
suffixes, so 'mi', 'ni' and 'ui' were accepted as base-1024 negative
exponents (e.g. parse_quantity('1mi') == 1/1024). Binary suffixes are limited
to Ki, Mi, Gi, Ti, Pi and Ei.
@ArfiArfath21
ArfiArfath21 force-pushed the quantity-reject-invalid-binary-suffixes branch from b976cf7 to ed38fb4 Compare October 18, 2026 01:28
@yliaog

yliaog commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 18, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ArfiArfath21, yliaog

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 18, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit f65a97e into kubernetes-client:master Oct 18, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parse_quantity/format_quantity accept invalid binary suffixes mi, ni and ui

2 participants