这个周末在抠 partial-json-parser


周五想到一种扁平的、2-pass 的实现,结果一直在 debug

扁平化的结果就是,要分类讨论很多很多情况,对我这种注意力不集中的人极其不友好🥲

我这种人大概是看不得 if-else 三层以上的算法的了😂


hypothesis 真是好啊,我准备把 js 版的也用 pythonmonkey 的 js runtime 接到 python 里,然后也用 hypothesis 来测一下。

欣赏一下 hypothesis 给出的报错:

  + Exception Group Traceback (most recent call last):
  |   File "<string>", line 1, in <module>
  |   File "D:\dev\partial-json-parser\tests\test_hypotheses.py", line 42, in main
  |     test_partial_json()
  |   File "D:\dev\partial-json-parser\tests\test_hypotheses.py", line 28, in test_partial_json
  |     @given(json)
  |
  |   File "D:\dev\partial-json-parser\.venv\Lib\site-packages\hypothesis\core.py", line 1638, in wrapped_test
  |     raise the_error_hypothesis_found
  | ExceptionGroup: Hypothesis found 3 distinct failures. (3 sub-exceptions)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "D:\dev\partial-json-parser\tests\test_hypotheses.py", line 34, in test_partial_json
    |     parse_json(json_string[:i])
    |   File "D:\dev\partial-json-parser\src\partial_json_parser\core\api.py", line 15, in parse_json
    |     return parser(ensure_json(json_string, allow_partial, use_fast_fix))
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
    |     return _default_decoder.decode(s)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 340, in decode
    |     raise JSONDecodeError("Extra data", s, end)
    | json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2)
    | Falsifying example: test_partial_json(
    |     anything=[['']],
    | )
    | Explanation:
    |     These lines were always and only run by failing examples:
    |         C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py:340
    |         D:\dev\partial-json-parser\src\partial_json_parser\core\myelin.py:124
    +---------------- 2 ----------------
    | Traceback (most recent call last):
    |   File "D:\dev\partial-json-parser\tests\test_hypotheses.py", line 34, in test_partial_json
    |     parse_json(json_string[:i])
    |   File "D:\dev\partial-json-parser\src\partial_json_parser\core\api.py", line 15, in parse_json
    |     return parser(ensure_json(json_string, allow_partial, use_fast_fix))
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "D:\dev\partial-json-parser\src\partial_json_parser\core\api.py", line 22, in ensure_json
    |     head, tail = fix_fast(json_string, allow_partial)
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "D:\dev\partial-json-parser\src\partial_json_parser\core\myelin.py", line 139, in fix_fast
    |     assert last_colon < last_comma, f"\n\n  {last_colon = } {last_comma = } {json_string = }"
    |            ^^^^^^^^^^^^^^^^^^^^^^^
    | AssertionError:

  last_colon = 3 last_comma = -1 json_string = '{"": ""'
    | Falsifying example: test_partial_json(
    |     anything={'': ''},
    | )
    +---------------- 3 ----------------
    | Traceback (most recent call last):
    |   File "D:\dev\partial-json-parser\tests\test_hypotheses.py", line 34, in test_partial_json
    |     parse_json(json_string[:i])
    |   File "D:\dev\partial-json-parser\src\partial_json_parser\core\api.py", line 15, in parse_json
    |     return parser(ensure_json(json_string, allow_partial, use_fast_fix))
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\__init__.py", line 346, in loads
    |     return _default_decoder.decode(s)
    |            ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 337, in decode
    |     obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |   File "C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py", line 355, in raw_decode
    |     raise JSONDecodeError("Expecting value", s, err.value) from None
    | json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
    | Falsifying example: test_partial_json(
    |     anything=[''],
    | )
    | Explanation:
    |     These lines were always and only run by failing examples:
    |         C:\Users\17979\AppData\Local\Programs\Python\Python312\Lib\json\decoder.py:354
    |         D:\dev\partial-json-parser\src\partial_json_parser\core\myelin.py:124
    +------------------------------------