Common errors and how to resolve them¶
literalizer raises a specific, typed exception whenever it cannot turn your data into valid code for the target language, rather than emitting source the compiler would reject.
Every exception lives in literalizer.exceptions; this page groups them by the stage at which they are raised and renders each one’s meaning and the recommended fix directly from literalizer.exceptions, so the guidance here cannot drift from the API reference.
All exceptions are importable from literalizer.exceptions, so a name such as HeterogeneousScalarCollectionError below refers to literalizer.exceptions.HeterogeneousScalarCollectionError.
Parsing the input¶
Raised while reading source in the declared input_format, before any language-specific rendering.
- exception literalizer.exceptions.ParseError
Raised when input cannot be parsed into a data structure.
Base class for every input-parsing failure; catch it to handle any malformed input uniformly. To resolve, validate the source against its declared
input_format.
- exception literalizer.exceptions.JSONParseError
Raised when a JSON string cannot be parsed.
To resolve, fix the JSON syntax, or pass the correct
input_formatif the data is really JSON5, YAML, or TOML.
- exception literalizer.exceptions.JSON5ParseError
Raised when a JSON5 string cannot be parsed.
To resolve, fix the JSON5 syntax, or choose the matching
input_format.
- exception literalizer.exceptions.YAMLParseError
Raised when a YAML string cannot be parsed.
To resolve, fix the YAML syntax, or choose the matching
input_format.
- exception literalizer.exceptions.TOMLParseError
Raised when a TOML string cannot be parsed.
To resolve, fix the TOML syntax, or choose the matching
input_format.
Mixed-type collections¶
Raised when the data mixes types within one collection but the target language requires that collection to be homogeneous.
Each of these subclasses HeterogeneousCollectionError, so you can catch the base class to skip any input a strict-typed language cannot hold.
The usual fix is to pick a non-default heterogeneous_strategy; see Heterogeneous strategies for the full discussion and JSON value types for the runtime JSON value alternative.
- exception literalizer.exceptions.HeterogeneousCollectionError
Base class for errors raised when data is incompatible with the target language’s collection-shape constraints.
To resolve, pick a non-default
heterogeneous_strategy(see Heterogeneous strategies) or render the value through ajson_type(see JSON value types); catch this base class to skip any heterogeneous input a strict-typed language cannot hold.
- exception literalizer.exceptions.HeterogeneousScalarCollectionError
Raised when a collection contains scalars of multiple types and the target language requires homogeneous scalar collections.
For example,
[1, "two", 3.0]for a language that needs a single element type. To resolve, setheterogeneous_strategytoTAGGED_ENUM(or the language’s equivalent) to wrap each scalar, or toTUPLEfor a fixed-length array.
- exception literalizer.exceptions.HeterogeneousSiblingListsError
Raised when sibling sub-lists contain scalars that, combined, span multiple types and the target language requires homogeneous scalar collections.
To resolve, use a
TAGGED_ENUMstrategy, or render the value through ajson_type.
- exception literalizer.exceptions.MixedListValuesError
Raised when a list has elements spanning multiple type families and the target language requires homogeneous list elements.
To resolve, use a richer
heterogeneous_strategyor ajson_type.
- exception literalizer.exceptions.MixedDictValuesError
Raised when a dict has values spanning multiple type families and the target language requires homogeneous dict values.
To resolve, use the
RECORDstrategy if the dict is conceptually a record, or ajson_typefor arbitrary mappings.
- exception literalizer.exceptions.MixedDictKeysError
Raised when a dict has keys spanning multiple type families and the target language requires homogeneous dict keys.
To resolve, use a single key type, or render through a
json_type.
- exception literalizer.exceptions.MixedDictShapesError
Raised when a list contains dicts with different key sets and the target language requires uniform record shapes (e.g. Dhall).
To resolve, make every dict share the same keys, or use a
json_type.
- exception literalizer.exceptions.HeterogeneousSetError
Raised when a set contains scalars of multiple types and the target language requires homogeneous set elements.
To resolve, use one scalar type, or a richer
heterogeneous_strategy.
- exception literalizer.exceptions.TupleArityNotRepresentableError(*, arity: int)
Raised when the
TUPLEheterogeneous strategy meets a tuple-eligible heterogeneous scalar array whose length the target language has no native fixed-size tuple for.Kotlin only has
Pair(two elements) andTriple(three elements); an array of any other length cannot be represented as a typed tuple without losing the per-position types, soliteralizeraises rather than falling back to a homogeneous list (which would re-trip the heterogeneity checks the strategy exists to satisfy). SubclassesHeterogeneousCollectionErrorso the same callers that already skip a heterogeneous input the language cannot represent also skip this one.To resolve, shorten the array to a length the language supports, choose a language without the tuple-length cap (Rust, Scala), or switch to a
TAGGED_ENUMstrategy.
Values the target language cannot represent¶
Raised at the formatting boundary when an individual value has no faithful representation in the target language.
- exception literalizer.exceptions.UnrepresentableInputError
Raised when an input value cannot be represented in the target language.
Used as the centralized error for shape-level rejections at the formatting boundary, e.g. when a dict carries a non-string key for a language whose surface syntax only admits string-typed keys. To resolve, adjust the data to a representable shape, or pick a language that supports it.
- exception literalizer.exceptions.InvalidDictKeyError
Raised when a dict key cannot be represented in the target language.
This includes empty-string keys and keys containing characters that the language’s label syntax does not support (e.g. control characters in Dhall backtick-quoted labels). To resolve, remove or rename the offending key.
- exception literalizer.exceptions.NullInCollectionError
Raised when a collection contains null elements and the chosen format does not support them (e.g. Java’s
List.of()).To resolve, remove the nulls, or select a collection format that admits them.
- exception literalizer.exceptions.UnrepresentableIntegerError
Raised when an integer value exceeds the range the target language can represent natively.
Used by languages whose fixed-width integer types cannot hold values outside the signed 64-bit range (e.g. Fortran default
integer, CobolPIC S9(18), PureScriptInt) when no external arbitrary-precision integer library is assumed to be available.To resolve, keep integers within range, or target a language with wider or arbitrary-precision integers.
- exception literalizer.exceptions.UnrepresentableEmptyDictError
Raised when an empty dict is passed to a target language whose runtime cannot distinguish an empty mapping from an empty sequence.
Lua, PHP, and R encode both empty arrays and empty objects with the same surface syntax (
{},[], andlist()respectively), so the JSON encoders in their ecosystems serialize an empty mapping as[]. The literalizer refuses to emit a literal that cannot round-trip rather than silently lose the mapping/sequence distinction.To resolve, drop the empty mapping, or target a language that distinguishes the two.
- exception literalizer.exceptions.UnrepresentableSpecialFloatError
Raised when a non-finite float (
inf,-inf, ornan) is passed to a target language whose runtime cannot produce IEEE 754 special float values.Used by Gleam on the Erlang target, which has no expression that evaluates to a non-finite float.
To resolve, replace the non-finite floats with finite values, or pick another language.
Identifiers and variable wrapping¶
Raised when variable_form or ref_case asks for output the target language cannot produce.
- exception literalizer.exceptions.UnsupportedIdentifierCaseError(*, language_name: str, case_name: str)
Raised when
literalizeorliteralize_callis passed aref_casethat is not in the target language’ssupported_ref_cases– i.e. one that would not produce a syntactically legal identifier in the target language.To resolve, choose a
ref_casefrom the language’ssupported_ref_cases.
- exception literalizer.exceptions.VariableNameNotSupportedError(*, language_name: str, variable_name: str)
Raised when
literalizeis given avariable_formbut the target language does not support variable-name wrapping.To resolve, omit
variable_formfor that language, or target one that supports it.
- exception literalizer.exceptions.WrapInFileWithoutVariableNotSupportedError(*, language_name: str)
Raised when
literalizeis called withwrap_in_file=Trueandvariable_form=Nonefor a target language that cannot represent a bare value at file-statement scope.Most strict-typed compiled languages (Rust, C, C++, Haskell, Swift, OCaml, Ada, D, Dart, C#, Elm, Mojo, Nim, Objective-C, Odin, SML, V, Zig, etc.) require every top-level item to be a declaration; a bare expression at file scope is a syntax error. Such languages opt out of the shape by setting
supports_no_variable_wrap_in_filetoFalse, andliteralizerejects the combination at the boundary instead of silently emitting invalid code.To resolve, supply a
variable_formso the value becomes a top-level declaration.
- exception literalizer.exceptions.InvalidRecordNameError
Raised when
record_struct_name_prefixor a value inrecord_shape_namesis not a valid PascalCase identifier, collides with a reserved keyword of the target language, collides withheterogeneous_value_enum_name, or duplicates another mapped struct name.To resolve, use distinct, valid PascalCase names that avoid reserved keywords.
Function calls¶
Raised by literalize_call() and its arguments (parameter_names, call_transform, zip_source, comment_source, target_function).
See Rendering function calls.
- exception literalizer.exceptions.CallsNotSupportedByLanguageError(*, language_name: str)
Raised when the target language itself has no function call syntax (e.g. pure data/markup formats like YAML, TOML, JSON5, Norg).
To resolve, use
literalize()instead, or target a programming language.
- exception literalizer.exceptions.CallsNotSupportedByToolError(*, language_name: str)
Raised when literalizer has not yet implemented function call rendering for the target language, even though the language itself has function call syntax.
To resolve, target a language whose call rendering is implemented.
- exception literalizer.exceptions.PerElementNotListError
Raised when
per_element=Truebut the parsed data is not a list.To resolve, pass a list, or drop
per_element.
- exception literalizer.exceptions.ParameterCountMismatchError(*, expected: int, got: int)
Raised when the number of
parameter_namesdoes not match the number of argument values in a function-call row.To resolve, make
parameter_namesthe same length as each row of values.
- exception literalizer.exceptions.CallArgNotSupportedError(*, language_name: str, reason: str)
Raised when a call argument value cannot be expressed as a positional argument in the target language’s call syntax.
Used by languages whose call syntax does not accept compound literals as arguments, e.g. Bash, where
cmd (1 2 3)parses as a nested(...)child-process invocation rather than an inline array value.To resolve, bind the value to a name first, or target a language that accepts the argument inline.
- exception literalizer.exceptions.UnsupportedCallShapeError(*, language_name: str, reason: str)
Raised when
literalize_callis given a call shape the target language cannot represent.Distinct from
CallArgNotSupportedError(which concerns individual argument values): this covers structural properties of the call itself, e.g. zero-parameter calls in languages whose syntax requires at least one operand.To resolve, adjust the call shape, or target a language that supports it.
- exception literalizer.exceptions.DottedCallTargetNotSupportedError(*, language_name: str, target_function: str)
Raised when
literalize_callis given a dottedtarget_functionbut the target language does not support dotted call expressions.To resolve, use a target name without dots, or target a language with dotted calls.
- exception literalizer.exceptions.ZipSourceWithoutInputFormatError
Raised when
literalize_callis given azip_sourcebut nozip_input_formatdescribing how to parse it.The companion source can only be parsed once its serialization format is known, so the two arguments must be supplied together.
To resolve, pass
zip_input_formatalongsidezip_source.
- exception literalizer.exceptions.ZipValuesWithoutCallTransformError
Raised when
literalize_callis given azip_sourcebut nocall_transformto consume the paired values.The paired values are only reachable through
zipped, so supplying them without a transform would silently discard them.To resolve, supply a
call_transformthat readszipped, or dropzip_source.
- exception literalizer.exceptions.ZipValuesLengthMismatchError(*, call_count: int, zip_count: int)
Raised when
literalize_callis given azip_sourcewhose parsed top-level elements differ in number from the generated calls.To resolve, make
zip_sourcehold one element per generated call.
- exception literalizer.exceptions.CommentSourceLengthMismatchError(*, call_count: int, comment_count: int)
Raised when
literalize_callis given acomment_sourcewhose entry count differs from the number of generated calls.Each comment is paired positionally with one call, so the two sequences must be the same length.
To resolve, provide one comment per generated call.
- exception literalizer.exceptions.CommentSourceMultilineError(*, index: int)
Raised when a
comment_sourceentry contains a newline.A trailing comment is emitted on the statement’s last line; a newline would push the remainder onto a line with no comment leader and, in languages whose line comment runs to end of line, produce invalid source. Comments must be single-line.
To resolve, remove the newline, keeping each comment on one line.
Format option combinations¶
Raised when otherwise valid options combine into something the language cannot emit.
- exception literalizer.exceptions.IncompatibleFormatsError
Raised when a combination of format options produces invalid code.
For example, Rust
CONSTandSTATICdeclaration styles require constant-expression initializers, but theVECsequence format producesvec![…]which is not a constant expression.To resolve, change one of the conflicting options so they agree.
- exception literalizer.exceptions.WrapCombinedInFileNotSupportedError
Raised when a language does not support
wrap_combined_in_file.Languages that raise this error do not support
BothVariableForms;literalize()rejects that form before reachingwrap_combined_in_file, but each language implementation still raises this typed exception as a safety net.To resolve, use a single variable form, or target a language that supports the combined form.
See also
Heterogeneous strategies for resolving mixed-type-collection errors.
JSON value types for the runtime JSON value alternative.
API Reference for the generated literalizer.exceptions reference.