pycorrectness


Overview

The **`pycorrectness`** script is a comprehensive JSON parser correctness testing utility. It systematically verifies the ability of multiple JSON parsing libraries—specifically `orjson` and Python's built-in `json`—to correctly accept or reject a diverse collection of JSON fixtures. These fixtures include both valid and invalid JSON documents sourced from standardized test sets.

The script reads test fixtures from compressed and uncompressed files, applies each library's JSON loading function, and records whether the library correctly parses or rejects each document. It distinguishes between expected passes and failures based on filename conventions and whitelists, tabulates results, and reports mistaken acceptance or rejection counts for each library.

This tool is critical for validating the correctness and robustness of JSON parsers, ensuring that they conform to expected JSON standards and behave consistently across edge cases.


Detailed Explanation of Components

Constants and Global Variables


Functions

read_fixture_bytes(filename: str, subdir: Optional[str] = None) -> bytes

Reads a fixture file as raw bytes.


test_passed(libname: str, fixture: bytes) -> bool

Checks if the given JSON fixture passes parsing tests with the specified library.


test_failed(libname: str, fixture: bytes) -> bool

Checks if the given JSON fixture fails parsing (i.e., is rejected) by the specified library as expected.


should_pass(filename: str) -> bool

Determines if a fixture file is expected to be valid JSON and should parse successfully.


should_fail(filename: str) -> bool

Determines if a fixture file is expected to be invalid JSON and should fail parsing.


Main Execution Flow

  1. Fixture Loading
    Loads all test fixtures from data/parsing and data/jsonchecker directories into PARSING and JSONCHECKER.

  2. Iterate Over Libraries and Fixtures
    For each library and each fixture set (PARSING and JSONCHECKER), iterates through all files:

    • Valid fixtures (should_pass):
      Runs test_passed() and records result.
      Increments MISTAKEN_PASSES if the test unexpectedly fails.

    • Invalid fixtures (should_fail):
      Runs test_failed() and records result.
      Increments MISTAKEN_FAILS if the test unexpectedly passes.

    • Intermediate fixtures (filenames starting with "i_"):
      Skips testing.

    • Unknown fixtures:
      Raises NotImplementedError.

  3. Result Tabulation
    Compiles a markdown-formatted table showing each fixture's pass/fail status per library, printed to stdout.

  4. Mistaken Acceptance/Rejection Summary
    Prints a summary table showing, per library:

    • Number of invalid documents erroneously accepted.

    • Number of valid documents erroneously rejected.

  5. Total Tests Count
    Prints the total number of documents tested.


Important Implementation Details


Usage Example

Assuming the `data/parsing` and `data/jsonchecker` directories contain JSON fixtures:

$ python pycorrectness

The script will print two markdown tables:

  1. Detailed test results per fixture and library (with "ok" or "fail").

  2. Summary of mistaken accepts/fails per library.

Finally, it prints how many documents were tested in total.


Interaction with Other System Components


Mermaid Class Diagram

classDiagram
    class pycorrectness {
        - dirname: str
        - LIBRARIES: list
        - LIBRARY_FUNC_MAP: dict
        - PARSING: dict
        - JSONCHECKER: dict
        - RESULTS: defaultdict(dict)
        - MISTAKEN_PASSES: dict
        - MISTAKEN_FAILS: dict
        - PASS_WHITELIST: tuple

        + read_fixture_bytes(filename, subdir=None) bytes
        + test_passed(libname, fixture) bool
        + test_failed(libname, fixture) bool
        + should_pass(filename) bool
        + should_fail(filename) bool
        + main() void
    }

Summary

The **`pycorrectness`** script is a specialized correctness validation tool for JSON parsers. It automates the process of verifying that JSON libraries correctly accept valid JSON and reject invalid JSON across extensive standardized test suites. By systematically comparing results, tabulating outcomes, and highlighting mistaken acceptances or rejections, it serves as a critical quality assurance component within the broader JSON benchmarking and testing ecosystem.