Skip to content

Reject non-finite numbers in JSON CLI output - #155

Merged
dpranke merged 1 commit into
dpranke:mainfrom
jwalith4418-Mj:fix/as-json-nonfinite
Nov 2, 2026
Merged

dpranke merged 1 commit into
dpranke:mainfrom
jwalith4418-Mj:fix/as-json-nonfinite

Conversation

@jwalith4418-Mj

Copy link
Copy Markdown
Contributor

Fixes #151.

--as-json currently emits NaN and Infinity tokens despite promising JSON output. Pass allow_nan=False in that mode, report the encoder's ValueError to stderr and exit with status 1 before printing output. Default JSON5 output retains non-finite support. Update the CLI help accordingly.

The regression runs through both the hosted test helper and a real CLI subprocess. It fails against the original source. Existing encoder tests already cover the individual non-finite values.

All 97 tests pass on Windows/Python 3.14 and Ubuntu/Python 3.10; Windows line coverage is 100%. Ruff, pylint, mypy, formatting and package builds pass. Other supported interpreters and hosted CI have not been verified locally.

Prepared with AI assistance. The contributor confirmed reviewing and vetting this commit before upload.

Pass allow_nan=False for --as-json and report encoding errors without emitting invalid JSON. Keep default JSON5 non-finite output supported.

Prepared with AI assistance; contributor review is required before upload.

@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.

I think this is fine and probably an improvement over the code as it stands.

However, please see my comment on the linked bug, as this isn't actually the
way I want things to work; dump should just take an actual as_json flag.

And, even then, I don't want to guarantee that the output will exactly match
whatever JSON does, as long as it is producing a legal JSON document that
will evaluate to the same value.

But, all that's really a separate patch than this PR, so I'll land this for now.

@dpranke
dpranke merged commit 7211e18 into dpranke:main Nov 2, 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.

--as-json can produce output that isn't valid JSON (NaN/Infinity literals)

2 participants