Skip to content

Fix boolean-like dictionary keys being converted to booleans#8

Closed
P6rguVyrst wants to merge 1 commit into
ZON-Format:mainfrom
P6rguVyrst:fixing-finding
Closed

Fix boolean-like dictionary keys being converted to booleans#8
P6rguVyrst wants to merge 1 commit into
ZON-Format:mainfrom
P6rguVyrst:fixing-finding

Conversation

@P6rguVyrst

@P6rguVyrst P6rguVyrst commented Apr 12, 2026

Copy link
Copy Markdown

Changes:

  • encoder.py: Quote keys matching t|f|true|false|null|none|nil
  • utils.py: Added parse_key() that preserves string keys
  • decoder.py: Use parse_key() for dictionary keys
  • tests/unit/test_boolean_keys.py: 12 regression tests

The encoder now quotes keys that match boolean/null keywords (t, f, true, false, null, none, nil) to prevent the decoder from misinterpreting them.

The decoder now uses a new parse_key() function for dictionary keys that preserves them as strings, unlike parse_value() which converts keywords.

This fixes a critical round-trip bug where {"f": 1} would decode as
{False: 1}.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed handling of dictionary keys that resemble boolean or null keywords (e.g., "true", "false", "null", "nil") to ensure they are properly preserved as strings during encoding and decoding.
  • Tests

    • Added comprehensive test suite covering boolean-like key scenarios, including nested dictionaries, case-insensitive variants, and interactions with actual boolean/null values.

The encoder now quotes keys that match boolean/null keywords (t, f, true,
false, null, none, nil) to prevent the decoder from misinterpreting them.

The decoder now uses a new parse_key() function for dictionary keys that
preserves them as strings, unlike parse_value() which converts keywords.

This fixes a critical round-trip bug where {"f": 1} would decode as
{False: 1}.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 12, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

The changes introduce specialized handling for dictionary keys that resemble boolean or null literals. A new parse_key() function treats keys as strings without converting boolean/null keywords, while the decoder uses this function for key parsing and the encoder JSON-quotes boolean-like keys to preserve their string nature.

Changes

Cohort / File(s) Summary
Key Parsing Implementation
zon/core/utils.py, zon/core/decoder.py
Added parse_key() utility function that parses quoted keys via json.loads() without converting boolean/null keywords to their literal values. Updated decoder to use parse_key() instead of parse_value() for dictionary key parsing.
Key Encoding
zon/core/encoder.py
Extended dictionary key JSON-quoting logic to include boolean-like keywords (true|false|t|f|null|none|nil, case-insensitive), not just special characters, ensuring such keys remain strings during encoding.
Test Coverage
tests/unit/test_boolean_keys.py
Added comprehensive test suite covering single-character and multi-character boolean-like keys, nested dictionaries, case-insensitive variants, interaction with actual boolean/null values, and list/table contexts with round-trip equality validation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Keys that look like booleans, oh what a sight,
We parse them as strings—no true/false to ignite!
With parse_key() helpers, we keep them pristine,
The cleanest key-handling you've ever seen! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main bug being fixed: boolean-like dictionary keys being converted to booleans instead of remaining as string keys.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@P6rguVyrst

Copy link
Copy Markdown
Author

sorry-- meant to do this to my own copy first & validate.

@P6rguVyrst P6rguVyrst closed this Apr 12, 2026
@ronibhakta1

Copy link
Copy Markdown
Contributor

@P6rguVyrst Please make sure you are running tests on your machine. There are multiple edge cases. A single black space can lead to a token increase.

Thanks for looking into internal bugs ❤️
Would like to connect you over X or LinkedIn.

@P6rguVyrst

P6rguVyrst commented Apr 14, 2026

Copy link
Copy Markdown
Author

@P6rguVyrst Please make sure you are running tests on your machine. There are multiple edge cases. A single black space can lead to a token increase.

Thanks for looking into internal bugs ❤️ Would like to connect you over X or LinkedIn.

The tests that failed on me on accuracy: #9
The 2 individual (separate commits) fixes Claude found for me: #10

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.

2 participants