Skip to content

param-count-mismatch

Default severity: error

What it does

Compares the arguments a call passes with the placeholders of its query. It applies to calls of a marked helper whose parameters are spelled out (a params argument holding a tuple, list or dict literal, *args, or **params), and reports:

  • a different number of positional arguments than the query has placeholders (%s, $1, ?; the highest $N counts);
  • names of the query (%(name)s, :name) the call does not pass. A name used twice is one parameter. Keys the query does not use are extra-param.

Driver calls are checked too: execute and executemany (each row) on a psycopg, sqlite3 or aiosqlite connection or cursor, execute_fetchall and execute_insert on an aiosqlite connection, and execute, fetch, fetchrow, fetchval, executemany and fetchmany (each row) on an asyncpg connection, when the file binds the receiver to psycopg.connect(...), sqlite3.connect(...), aiosqlite.connect(...), asyncpg.connect(...), <connection>.cursor() or a driver class annotation, and the execute and scalar helpers of sqlty.drivers.psycopg. The query is a literal, SQL[T]("...") of one, or a name assigned once to one.

Positional arguments for named placeholders, or the reverse, and calls that do not spell their arguments out (params as a variable, *rest, **kwargs), are not compared. A driver call with no parameters against a query that has placeholders is reported.

Why is this bad?

The driver raises at run time, or the query silently runs with the wrong values, and only on the path that runs it.

Example

[tool.sqlty]
schema = "schema.sql"

schema.sql:

CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
from typing import Annotated, LiteralString

from sqlty import SQL


def execute(query: Annotated[LiteralString, SQL], params: tuple[object, ...] | dict[str, object]) -> None: ...


# error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
execute("SELECT id FROM users WHERE id = %s AND name = %s", (1,))
# error: [param-count-mismatch] "call is missing parameter 'name'"
execute("SELECT id FROM users WHERE id = %(id)s AND name = %(name)s", {"id": 1})
Open in playground

Fix

Pass one argument per placeholder, or name each placeholder in the call. Turn the rule off with param-count-mismatch = "ignore" in [tool.sqlty.rules].