Typechecking¶
- class jsonlogic.typechecking.Diagnostic(message: str, category: DiagnosticCategory, type: DiagnosticType, operator: Operator)¶
A diagnostic emitted during typechecking.
- category: DiagnosticCategory¶
The category of the diagnostic.
- type: DiagnosticType¶
The type of the diagnostic.
- class jsonlogic.typechecking.DiagnosticsConfig(general: 'DiagnosticType | None' = 'error', argument_type: 'DiagnosticType | None' = 'error', operator: 'DiagnosticType | None' = 'error', unresolvable_variable: 'DiagnosticType | None' = '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. SeeTypecheckSettingsfor the available settings and default values.
- 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 JSONJSONSchemaType.This setting is analogous to the
variable_castsconfiguration of theEvaluationSettingsclass.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
JSONSchemaTypefrom 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_castsconfiguration of theEvaluationSettingsclass.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 aJSONLogicPrimitive, and the type is inferred from the actual value according to theliteral_castssetting.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
TypecheckSettingsfor 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.