Bug report
Bug description:
Documented behaviour: json.JSONEncoder documentation: "If allow_nan is false (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification."
Expected: Consuming iterencode([x]) raises ValueError.
Actual: Iteration completes and produces '[nan]', which is invalid JSON.
import json
import math
class NaN(float):
def __eq__(self, other): return False
def __ne__(self, other): return False
x = NaN("nan")
data = [x]
if not (isinstance(x, float) and math.isnan(float(x))):
print("REFUTATION REJECTED: input is not a NaN float instance")
else:
expected = ("exception", "ValueError") if not math.isfinite(float(x)) else None
try:
actual = ("result", "".join(json.JSONEncoder(allow_nan=False).iterencode(data)))
except Exception as e:
actual = ("exception", type(e).__name__)
if actual != expected:
print("REFUTATION CONFIRMED:", data, "actual =", actual, "expected =", expected)
else:
print("REFUTATION REJECTED: documented ValueError was raised")
Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library json:
REFUTATION CONFIRMED: [nan] actual = ('result', '[nan]') expected = ('exception', 'ValueError')
This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/json-20261003-083804-c4
CPython versions tested on:
3.14
Operating systems tested on:
Windows
Bug report
Bug description:
Documented behaviour: json.JSONEncoder documentation: "If allow_nan is false (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification."
Expected: Consuming iterencode([x]) raises ValueError.
Actual: Iteration completes and produces '[nan]', which is invalid JSON.
Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library
json:This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/json-20261003-083804-c4
CPython versions tested on:
3.14
Operating systems tested on:
Windows