Skip to content

json.loads ignores explicitly supplied false-valued parse_float callables #158729

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: json.loads docstring: "parse_float, if specified, will be called with the string of every JSON float to be decoded."

Expected: Call hook('1.5') exactly once and return ''.

Actual: Returns 1.5 without calling the hook.

import json, math, re

class FalseBool:
    def __init__(self):
        self.calls = []
    def __bool__(self):
        return False
    def __call__(self, text):
        self.calls.append(text)
        return "<sentinel>"

document, hook = "1.5", FalseBool()
if not (re.fullmatch(r"-?(?:0|[1-9][0-9]*)\.[0-9]+", document)
        and math.isfinite(float(document)) and callable(hook) and not bool(hook)):
    print("REFUTATION REJECTED:", "invalid input or hook")
else:
    expected = {"result": "<sentinel>", "calls": [document]}
    try:
        result = json.loads(document, parse_float=hook)
        actual = {"result": result, "calls": hook.calls}
    except Exception as error:
        actual = {"exception": repr(error), "calls": hook.calls}
    if actual != expected:
        print("REFUTATION CONFIRMED:",
              {"document": document, "parse_float": "FalseBool"},
              "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED:", "actual matches documented expectation")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library json:

REFUTATION CONFIRMED: {'document': '1.5', 'parse_float': 'FalseBool'} actual: {'result': 1.5, 'calls': []} expected: {'result': '<sentinel>', 'calls': ['1.5']}

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-051249-c4

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions