Skip to content

Preserve duck-typed mappings without testing their truth value - #147

Merged
dpranke merged 1 commit into
dpranke:mainfrom
jwalith4418-Mj:fix/duck-mapping-encoding
Oct 31, 2026
Merged

dpranke merged 1 commit into
dpranke:mainfrom
jwalith4418-Mj:fix/duck-mapping-encoding

Conversation

@jwalith4418-Mj

Copy link
Copy Markdown
Contributor

The follow-up noted in #145 still affects duck-typed mappings: a false-valued mapping with an answer key serializes as {}, and a mapping whose truth value raises cannot be encoded at all.

Remove the early truth-value test in _encode_dict. Its existing key-iteration path already returns {} when no encodable keys remain, so no additional buffering or mapping methods are needed. Regression tests cover false-valued mappings and mappings with an undefined truth value, including empty input, iterator-only keys, sorting, indentation and trailing commas. Remove the now-unused __len__ from an existing protocol test fixture.

Before the fix, the regressions produce one failing assertion and 32 error subcases. Afterward, all 95 tests pass on Windows/Python 3.14.3 and Ubuntu/Python 3.10.12. Windows line coverage is 100%; library/test Ruff, formatting, pylint, mypy, doctests and package builds pass. The runner interpreter fix in #146 is separate and is not included here; checks were invoked with the environment's explicit interpreter.

Developed and tested with OpenAI Codex assistance. This follows the maintainer's observation in #145, not a claimed production incident or independent human review.

Use the existing key-iteration empty-object path to avoid dropping false-valued mappings or raising on ambiguous truth values. Add iterator-only mapping controls for formatting and sorting. Prepared with OpenAI Codex assistance.

@dpranke dpranke left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good, thanks!

@dpranke

dpranke commented Oct 31, 2026

Copy link
Copy Markdown
Owner

Ah, the fix was easier than I thought it would be, because of the trailing first_key check. That helps :).

@dpranke
dpranke merged commit 0009534 into dpranke:main Oct 31, 2026
9 checks 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.

2 participants