Skip to content

Preserve empty Bits across binary and byte conversions - #468

Merged
mahmoud merged 2 commits into
mahmoud:masterfrom
codewithfourtix:fix-empty-bits-roundtrip
Sep 7, 2026
Merged

mahmoud merged 2 commits into
mahmoud:masterfrom
codewithfourtix:fix-empty-bits-roundtrip

Conversation

@codewithfourtix

Copy link
Copy Markdown
Contributor

An empty Bits value serializes as a one-bit zero, while byte conversion raises an odd-length hex error. Preserve the empty representation and allow empty hex and byte inputs to round-trip.

Adds coverage for empty constructors, slices, and all conversion paths.

Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
Copilot AI lite review requested due to automatic review settings September 5, 2026 17:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Bits.from_hex() currently accepts any falsy value (e.g., None, 0) as “empty hex,” which is an API behavior change that can hide caller bugs and should be restricted to empty str/bytes only.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes Bits conversions so an empty Bits value round-trips cleanly across binary, hex, and byte representations (instead of serializing to a one-bit "0" / raising odd-length hex errors).

Changes:

  • Return '' from Bits.as_bin() and Bits.as_hex() when len == 0.
  • Treat empty hex input as an empty Bits in Bits.from_hex().
  • Add tests covering empty constructors, slicing-to-empty, and conversion round-trips.
File summaries
File Description
boltons/mathutils.py Adjusts Bits bin/hex rendering for zero-length values and adds empty-hex handling in from_hex().
tests/test_mathutils.py Adds a test ensuring empty Bits values round-trip through all conversion APIs.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread boltons/mathutils.py Resolved
Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
@mahmoud
mahmoud merged commit efff866 into mahmoud:master Sep 7, 2026
1 check passed
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.

3 participants