See Ash Code logo See Ash Code

The type checker objected to my migration script

Ashlynn Antrobus
• • 3 min read

I wanted a small bootstrap script for my homelab databases. The SQL files were already in version control, and the tables used CREATE TABLE IF NOT EXISTS. I didn’t need another system for tracking migration versions.

I did need the Python wrapper to execute those files and create the right databases. Pylance kept objecting to the strings I passed to Psycopg.

The problem wasn’t solved by renaming a parameter or moving the same string through another method. There were different kinds of SQL inputs mixed together.

A diagram labels the clauses, expressions and predicate of an example SQL UPDATE statement.

A separate SQL statement illustrates different parts of a query: SQL statement anatomy by SqlPac, CC BY-SA 3.0; unmodified.

An identifier isn’t a value

A database name belongs in the SQL statement as an identifier. It can’t be supplied as a value parameter to CREATE DATABASE.

The helper discussion arrived at this construction:

query = SQL("CREATE DATABASE {}").format(Identifier(database))

The template is fixed SQL. Identifier handles the database name as a name, with the quoting rules that requires. This snippet builds the statement; executing database creation also needs an appropriate connection and transaction setting.

For an ordinary data value, Psycopg parameters are the right boundary. Here is a teaching example that combines a dynamic table name with a value:

query = SQL("SELECT count(*) FROM {} WHERE category = %s").format(
    Identifier(table_name)
)
cursor.execute(query, ("articles",))

The table name is composed into the statement. The category stays a separate parameter. Psycopg’s SQL composition documentation explains that distinction.

A complete migration file is another input

My migration text came from a checked-in .sql file. Reading it with Path.read_text returns str. The type annotations at the Psycopg boundary expected types including LiteralString, SQL, and Composed.

Pylance can’t establish that arbitrary file contents came from the migration files I reviewed. Adding “trusted SQL” in a comment doesn’t change the expression’s type.

Nor does calling SQL(script) automatically escape the script. SQL describes SQL syntax; it isn’t a sanitizer for untrusted text.

A narrow trust assertion can make the boundary explicit. For illustration, assuming migration_path identifies a reviewed, version-controlled migration file:

from typing import LiteralString, cast

migration_text = migration_path.read_text(encoding="utf-8")
trusted_sql = cast(LiteralString, migration_text)
connection.execute(trusted_sql)

The cast tells the type checker what I’m asserting. It performs no validation and doesn’t make an uploaded file or user-supplied query safe. The reason to trust this input has to exist outside the cast.

Silencing the error can change the query

One workaround discussed in the exchange was:

SQL("{}").format(script)

That isn’t equivalent to marking the complete script as SQL syntax. Psycopg wraps ordinary non-composable objects passed to format as SQL literals. A script treated as a literal string is data, not the sequence of statements I meant to execute. Getting a Composed result doesn’t establish that I built the intended query.

That is why the type error was useful, even while the repeated attempts to get past it were frustrating. I needed to preserve the meaning of the input while satisfying the interface.

The helper can run the reviewed SQL file as a script, quote a database name as an identifier, and pass a value as a parameter. All three look like strings in Python.

AA

Written by Ashlynn Antrobus

Software developer, author, and builder. Sharing technical lessons learned from building tools like Ebook2Text, IndexedDict, and rag-mcp, and navigating life as a programmer with dyslexia.