Skip to content

Fix const compilation without detailed exceptions - #230

Open
CLCK0622 wants to merge 1 commit into
horejsek:masterfrom
CLCK0622:fix/const-compact-exceptions
Open

CLCK0622 wants to merge 1 commit into
horejsek:masterfrom
CLCK0622:fix/const-compact-exceptions

Conversation

@CLCK0622

Copy link
Copy Markdown

Compiling a schema with const and detailed_exceptions=False raises KeyError('definition_rule') before a validator is created. Read and escape the current rule value inside the compact exception branch and pass it to both compact exception paths. This avoids resolving references or traversing annotation data in that mode, while leaving the detailed exception branch unchanged. The regression tests cover draft-06/07, both exception modes, both fast_fail values, eight constant types, matching and nonmatching inputs, executable compile_to_code output, and annotation data containing an unresolved $ref.

Reproducer (Python 3.10+):

python3 - <<'PY'
import fastjsonschema

schema = {'$schema': 'http://json-schema.org/draft-07/schema#', 'const': 1}
try:
    validate = fastjsonschema.compile(schema, detailed_exceptions=False)
except Exception as exc:
    print(type(exc).__name__ + ': ' + str(exc))
else:
    print('match:', validate(1))
    try:
        validate(2)
    except fastjsonschema.JsonSchemaValueException as exc:
        print(type(exc).__name__ + ': ' + exc.message)
PY

Before:

KeyError: 'definition_rule'

After:

match: 1
JsonSchemaValueException: data must be same as const definition: 1

Validation on Python 3.12.13: python -m pytest -m 'not benchmark' reports 4675 passed (4539 before), with the same 435 xfailed and 950 xpassed. The added regression cases fail 66 times on the original implementation. python -m pylint fastjsonschema reports the same nine existing diagnostics and 9.90/10 before and after.

AI disclosure: Codex assisted with part of the code and this description.

Copilot AI balanced review requested due to automatic review settings October 23, 2026 16:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

This branch has not been deployed

No deployments
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.

2 participants