Skip to main content

Deephaven tables now support Python type hints

· 3 min read
DALL·E prompt: A python holding a magnifying glass looking at neatly arranged colorful boxes in stacked columns, digital art
JJ Brosnan
New, simpler query strings

Deephaven's v0.19 release rolled out some awesome features, particularly for Python programmers - table slicing, autocomplete, and others. For me, support for type hints in tables is an unsung hero. I've always had so many explicit typecasts in my table operations. Now I can reduce how many I need, which results in cleaner code. Let me show you what I mean.

Type hints work in table operations

I write a lot of Deephaven queries. I also write a lot of Python functions. Sometimes, I wish I didn't always have to cast my Python functions in query strings to the data type I want:

from deephaven import empty_table
import numpy as np

def sinc(x):
return np.sinc(x)

sinc_function = empty_table(100).update(["X = 0.1 * i", "SincX = (double)sinc(X)"])

With Deephaven's v0.19 release, I no longer have to. I can just use Python's type hints:

from deephaven import empty_table
import numpy as np

def sinc(x) -> np.double:
return np.sinc(x)

sinc_function = empty_table(100).update(["X = 0.1 * i", "SincX = sinc(X)"])

Type hints are great since they allow Python to infer the correct input and/or output data types from functions. Now that they're supported by Deephaven tables, you can simplify your queries.

Why does this matter?

In the code above, a type hint doesn't actually provide a lot of benefit over a typecast in the query string itself. But this is a very simple example. Deephaven queries in production workflows are always more complex. In fact, they're typically much more complex. A single table operation can have dozens of query strings. When the verbosity of tightly packed query strings increases, the table operation quickly gets ugly. The use of type hints reduces the bloat associated with complex table operations in Python. This leads to cleaner code, which is always beneficial. It also lets you focus on the logic of your query, instead of writing boilerplate code.


The new query language type inference improvements do have some limitations. They only work for simple Deephaven query strings, like sinc(x). Type declarations won't work if there are additional operations in a query string. Say, for instance, we actually want sinc(X) + 1. This query string still requires an explicit type cast and should read SincX = (double)sinc(X) + 1. If the double typecast is missing, + 1 attempts to add an object and a primitive integer and will raise an error.

Instead, encapsulate additional operations in the functions themselves. Not only does this work properly, but it leads to succinct, readable code.

from deephaven import empty_table
import numpy as np

def sinc_plus_one(x) -> np.double:
return np.sinc(x) + 1

sinc_function = empty_table(100).update(["X = 0.1 * i", "SincX = sinc_plus_one(X)"])

Looking ahead

Stay tuned. Future Deephaven releases will have more improvements to the query language. These improvements will include automatic type inference and performance vectorization on complex functions.

Reach out to us on Slack with any questions or feedback!