Syntax errors (Python)

Python syntax errors occur when code violates the language's structural rules, such as incorrect indentation, mismatched quotes, invalid tokens, or referencing undefined variables. These errors prevent your Deephaven queries from executing and must be fixed before your code can run successfully.

Identifying and fixing syntax errors is a fundamental skill for developing Deephaven Persistent Queries, which are programs written in either Python or Groovy. Within these queries, operations like select, update, update_view, view, and where use formulas that must also follow syntax rules.

The key to resolving syntax errors is understanding how to read a Python stack trace. For example, the following code assigns a value to the variable named val but then incorrectly references value:

When executed from a Code Studio or Notebook, the snippet produces the following traceback:

The type of the Python exception is a NameError occuring on line 3 (where value is first used). Similarly, the else statement may be incorrect, as in the following snippet:

This produces a traceback referencing that line number:

When executed as part of a Persistent Query, the traceback is the same, but the Java stack trace that wraps it is slightly different:

Use the line number and Python's error message to examine your script and find the syntax error. Using an editor that offers syntax highlighting (such as the Deephaven Code Studio, Visual Studio Code, or PyCharm) is often effective in identifying incorrect syntax, such as missing braces and quotes or other simple errors.

Formula syntax errors

Deephaven formulas and filters use a Java-like syntax to express operations on table columns, variables from your session, and globally imported functions. As in any programming language, proper syntax is necessary. In the following example, the Python syntax is correct, but the closing parenthesis inside the formula is missing:

This results in a traceback of:

The summary information is on lines 3 and 4, indicating an error with the formula 2 * 3 ( + 4.

The traceback and stack traces indicate where the error occurred, but the relevant details begin on line 41 and continue until line 81:

In this case, the message indicates that the <EOF> or end of file token was encountered at column 10, but another token was expected. Many options are listed, including the correct closing parenthesis on line 55. In practice, with so many options, you must carefully examine the formula around the indicated position to determine what syntax is incorrect.

Invalid operator overloads

You may also have errors related to operators. The Deephaven formula parser converts operators to method calls to permit users to naturally express mathematic operations between various types unsupported by Java (for example, Deephaven permits you to add two BigIntegers together using the plus (+) operator). This is similar to operator overloading in many programming languages. In the example below, the formula attempts to multiply an integer and a string, which is not supported by Deephaven formulas:

The following error is produced:

The relevant detail is that no method is named multiply for int and Strings. The following operators may also be present in your stack trace, corresponding to an operator used in your formula:

OperatorMethod
+plus
-minus
*multiply
/divide
%remainder
= or ==eq
<less
<=lessEquals
>greater
>=greaterEquals
^xor
|binaryOr
^binaryAnd
||or
&&and

Invalid use of in

The in keyword can only be used in a standalone expression and cannot be combined with other operators or formula elements (even if surrounded in parenthesis). For example, this is valid syntax:

However, in this example in is combined with another expression:

This results in an error:

When the in operator begins the combined expression, then an alternative error is produced indicating that the expression is not a valid long value: