Typechecking

class jsonlogic.typechecking.Diagnostic(message: str, category: DiagnosticCategory, type: DiagnosticType, operator: Operator)

A diagnostic emitted during typechecking.

message: str

The message of the diagnostic.

category: DiagnosticCategory

The category of the diagnostic.

type: DiagnosticType

The type of the diagnostic.

operator: Operator

The operator that emitted the diagnostic.

class jsonlogic.typechecking.DiagnosticsConfig(general: 'DiagnosticType | None' = 'error', argument_type: 'DiagnosticType | None' = 'error', operator: 'DiagnosticType | None' = 'error', unresolvable_variable: 'DiagnosticType | None' = 'error')
general: DiagnosticType | None = 'error'

A general diagnostic.

Default: "error".

argument_type: DiagnosticType | None = 'error'

An argument has the wrong type.

Default: "error".

operator: DiagnosticType | None = 'error'

Operator not supported for type(s).

Default: "error".

unresolvable_variable: DiagnosticType | None = 'error'

Variable in unresolvable.

Default: "error".

class jsonlogic.typechecking.TypecheckContext(root_data_schema: dict[str, Any], settings: TypecheckSettingsDict | None = None)

A context object used when typechecking operators.

When typechecking an Operator, an instance of this class should be used.

>>> expr = ...
>>> root_op = expr.as_operator_tree(operator_registry)
>>> context = TypecheckContext(
...     root_data_schema={"type": "object", "properties": ...},
...     settings={
...         "diagnostics": {"argument_type": "warning"},
...     },
... )
>>> root_op.typecheck(context)
>>> context.diagnostics
[Diagnostic(message="...", ...)]
Parameters:
  • root_data_schema – The root JSON Schema describing the available data when the operator will be evaluated.

  • settings – Settings to be used when typechecking an Operator. See TypecheckSettings for the available settings and default values.

property current_schema: dict[str, Any]

The data JSON Schema in the current evaluation scope.

add_diagnostic(message: str, category: DiagnosticCategory, operator: Operator, type: TypeAliasForwardRef('DiagnosticType') | None = None) None

Add a diagnostic during typechecking.

Parameters:
  • message – The message of the diagnostic.

  • category – The category of the diagnostic.

  • operator – The operator that emitted the diagnostic.

  • type – The type of the diagnostic. If not provided (the default), the type will be deduced from the settings.

class jsonlogic.typechecking.TypecheckSettings(reference_parser: ~jsonlogic.resolving.ReferenceParser = <factory>, variable_casts: dict[str, type[~jsonlogic.json_schema.types.JSONSchemaType]] = <factory>, literal_casts: dict[~collections.abc.Callable[[str], ~typing.Any], type[~jsonlogic.json_schema.types.JSONSchemaType]] = <factory>, diagnostics: ~jsonlogic.typechecking.typecheck_settings.DiagnosticsConfig = <factory>)

Settings used when typechecking an Operator.

reference_parser: ReferenceParser

A reference parser instance to use when resolving variables.

Default: PointerReferenceParser.

variable_casts: dict[str, type[JSONSchemaType]]

A mapping between JSON Schema formats and their corresponding JSONSchemaType.

When an operator makes use of the provided data JSON Schema to read variables (such as the "var" operator), such variables with a type of “string” might have a format provided. To allow for features specific to these formats, such strings can be inferred as a specific JSON JSONSchemaType.

This setting is analogous to the variable_casts configuration of the EvaluationSettings class.

Default: {"date": DateType, "date-time": DatetimeType}.

literal_casts: dict[Callable[[str], Any], type[JSONSchemaType]]

A mapping between conversion callables and their corresponding JSONSchemaType.

When a literal string value is encountered in a JSON Logic expression, it might be beneficial to infer the JSONSchemaType from the format of the string. The callable must take a single string argument and raise any exception if the format is invalid.

This setting is analogous to the literal_casts configuration of the EvaluationSettings class.

Default: {} (no cast).

Warning

The order in which the conversion callables are defined matters. Each callable will be applied one after the other until no exception is raised.

diagnostics: DiagnosticsConfig

Configuration of type for diagnostics.

This is a mapping between the emitted diagnostic categories and the corresponding type (e.g. "error" or "warning").

Default: see DiagnosticsConfig.

jsonlogic.typechecking.get_type(obj: OperatorArgument, context: TypecheckContext) JSONSchemaType

Get the JSON Schema type of an operator argument.

Parameters:
  • obj – the object to typecheck. If this is an Operator, it is typechecked and the type is returned. Otherwise, it must be a JSONLogicPrimitive, and the type is inferred from the actual value according to the literal_casts setting.

  • context – The typecheck context.

jsonlogic.typechecking.typecheck(operator: Operator, data_schema: dict[str, Any], settings: TypecheckSettingsDict | None = None) tuple[JSONSchemaType, list[Diagnostic]]

Helper function to typecheck an Operator.

Parameters:
  • operator – The operator to typecheck.

  • data_schema – The JSON Schema of the data that will be provided to the operator during evaluation.

  • settings – The typechecking settings to use. See TypecheckSettings for the available settings and default values.

Returns:

A two-tuple, the first element is the JSON Schema type of the operator, the second one is a list of generated diagnostics.