liveBook · Manning

Type hints

As explained in DuckDB in Action

A Python type annotation is syntax that associates a variable, parameter, or return value with an intended type.

Type hints associate a Python variable, parameter, or return value with an intended type. In DuckDB user-defined functions, annotations let DuckDB infer SQL parameter and return types. section 6.4

Annotating a string UDF

def remove_spaces(field: str) -> str:\n if field:\n return field.lstrip().rstrip()\n return field\n\ncon.create_function('remove_spaces', remove_spaces)\n\nDuckDB registers the function as accepting and returning VARCHAR, so SQL can call remove_spaces(Region). section 6.4

Use annotations as the first choice, then specify types explicitly when inference is unreliable.

  • Annotations keep the Python definition concise and communicate the intended interface. section 6.4
  • Explicit parameter and return types improve clarity and consistency when annotations are absent or third-party code provides no hints. section 6.4

How DuckDB in Action covers it

How deep it goes
Explains annotation-based inference, explicit declarations, registration, metadata inspection, and SQL invocation.
Examples
Demonstrates string trimming and locale-formatted numeric conversion in DuckDB queries.
What it assumes
Assumes basic Python functions and SQL queries while teaching the Python-to-DuckDB type mapping.
What you can do after
Build and register Python DuckDB functions whose SQL types derive from annotations or explicit declarations.
How it connects
Connects Python, DuckDB, user-defined functions, SQL typing, and type inference.

Also explained in

Compare how 17 books explain type hints →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.