๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.python.announce
Article #13

[Python-announce] ANN: pytest-queryguard 1.0.0 - fail pytest tests on N+1 queries

#13
From: maciej zielinski
Date: Sun, 15 Nov 2026 22:22
36 lines
10827 bytes
pytest-queryguard 1.0.0 is out.

It is a pytest plugin that fails a test when the code it runs sends one
query per row (N+1), and enforces query budgets per test. It works with
SQLAlchemy 2 (sync and async sessions, FastAPI, Flask-SQLAlchemy, SQLModel,
Litestar) and needs no code changes: install it and run your tests. The
failure names the line, the SQL and the fix, for example a joinedload on the
parent query.

What it checks by default: a relationship lazy-loaded for 2 or more parents,
the same SELECT run 5 or more times (ORM, Core or text()), columns reloaded
one object at a time, and cartesian joinedloads (as a warning). Opt-in:
row-by-row writes and missing indexes (PostgreSQL 16+, SQLite, MySQL,
MariaDB). An N+1 that runs only in test code (conftest.py, factories,
helpers under tests/) is counted at the end of the run instead of failing
the test. A baseline file lets an existing suite adopt it without fixing
everything first, and a GitHub Action uploads findings to code scanning and
compares query counts with the base branch on a pull request.

What 1.0 means: the options, markers, finding kinds, report files and the
Action change incompatibly only in a major release, and deprecations last
at least two minor releases. Django ORM support (next to pytest-django), the
static check and the MCP server are still experimental and may change in a
minor release.

Accuracy and overhead are measured and published, misses included: on 278
labelled scenarios the defaults flag 117 of 135 with a problem and 2 of 143
without one. The corpus is written by me, so read those numbers with that
in mind; the comparison page has the method and the other tools' results.

Python 3.10+, pytest 8+, SQLAlchemy 2.0.13+. MIT licence.

Install:   pip install pytest-queryguard
Docs:      https://maciejziel.github.io/pytest-queryguard/
Changes:
https://github.com/MaciejZiel/pytest-queryguard/blob/main/CHANGELOG.md
Source:    https://github.com/MaciejZiel/pytest-queryguard

Maciej Zieliล„ski
Technical Details
Message-ID: <CAOh_6nAJJK0ApOdVu1ib5Q-dp16GZ6Gnc6cvSWoj002SfNOsiQ@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: maciej zielinski <zielinski.macio@gmail.com>
Newsgroups: comp.lang.python.announce
Subject: [Python-announce] ANN: pytest-queryguard 1.0.0 - fail pytest tests on N+1 queries
Date: Sun, 15 Nov 2026 23:22:43 +0100
Lines: 36
Approved: python-announce-list@python.org
Message-ID: <CAOh_6nAJJK0ApOdVu1ib5Q-dp16GZ6Gnc6cvSWoj002SfNOsiQ@mail.gmail.com>
Reply-To: python-list@python.org
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: base64
X-Trace: news.uni-berlin.de ZXigq1RhHjJCqjPGviFOqgmWVU7Hs++GX9mFiYIuC5eQ==
Cancel-Lock: sha1:br09/+fK2A5KAmDtHU4JKGuDUoE= sha256:qJIDAPAAk7FjjsZauUodP3oZqih9OzEF3HOdv38X1nc=
Delivered-To: python-announce-list@x.python.org
Authentication-Results: mail.python.org; dkim=pass
	reason="2048-bit key; unprotected key"
	header.d=gmail.com header.i=@gmail.com header.b=XVfwmVuw;
	dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.005
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'pip': 0.04; 'pytest':
    0.05; 'row': 0.05; 'django': 0.07; 'lets': 0.07; 'parent': 0.07;
    'received:mail-ej1-x62e.google.com': 0.07; 'comparison': 0.09;
    'fails': 0.09; 'options,': 0.09; 'overhead': 0.09;
    'subject:pytest': 0.09; 'to:addr:python-announce-list': 0.09;
    'url-ip:140/8': 0.15; 'budgets': 0.16; 'changes:': 0.16;
    'columns': 0.16; 'defaults': 0.16; 'flag': 0.16; 'parents,': 0.16;
    'query.': 0.16; 'scanning': 0.16; 'static': 0.16; 'subject:ANN':
    0.16; 'subject:fail': 0.16; 'uploads': 0.16; 'writes': 0.16;
    'problem': 0.16; 'python': 0.16; 'github': 0.17; 'pull': 0.17;
    'url-ip:140.82.114/24': 0.17; 'url-ip:140.82/16': 0.17; 'instead':
    0.17; 'written': 0.22; 'first,': 0.22; 'install': 0.23;
    'url:github': 0.23; 'run': 0.23; 'code': 0.23; '135': 0.26;
    'object': 0.26; 'request.': 0.28; 'message-id:@mail.gmail.com':
    0.31; '(as': 0.32; 'checks': 0.32; 'server': 0.33;
    'received:google.com': 0.34; 'same': 0.34; 'core': 0.34;
    'from:addr:gmail.com': 0.35; 'one.': 0.35; 'runs': 0.35; 'files':
    0.36; 'change': 0.36; 'those': 0.36; 'missing': 0.37; 'example':
    0.37; 'file': 0.38; 'read': 0.38; 'two': 0.39; 'least': 0.39;
    'url-ip:185.199.108.153/32': 0.39; 'url-ip:185.199.108/24': 0.39;
    'url-ip:185.199/16': 0.39; 'branch': 0.39; 'finding': 0.39;
    'still': 0.40; 'base': 0.40; 'method': 0.61; 'everything': 0.63;
    'your': 0.64; 'numbers': 0.67; 'time,': 0.67; 'per': 0.68;
    'findings': 0.69; 'fixing': 0.69; 'times': 0.69; 'relationship':
    0.71; 'suite': 0.71; 'accuracy': 0.74; 'mit': 0.76; 'counts':
    0.78; 'major': 0.78; 'out.': 0.80; '1.0': 0.81; '278': 0.84;
    'adopt': 0.84; 'default:': 0.84; 'experimental': 0.84; 'indexes':
    0.84; 'minor': 0.84; 'mysql,': 0.84; 'sessions,': 0.84; 'line,':
    0.93
ARC-Seal: i=1; a=rsa-sha256; t=1794781376; cv=none;
        d=google.com; s=arc-20260327;
        b=mdY6mMQBl9HrkPVFHY1sZbPGCETAm4Qz7Q82j5EouBeYgbx/H9yySsiZkWoK+b4umg
         lrDENYyTIScPR/qfycREKgk6gqIqcPEoD4cPYYB/oOfB1v70pkbUIfHNb3lFLsyJOHEb
         cLW99bnBm1HMUQFbk7L2/MCax951ZcnIf1L4dN4wxvpTmKc02dt6m8ZpXtePOQe4L5NK
         crUsMP5JFEf2x7Q3AVh62P6HEs1e71IdGbxwQFccBHO+IUOY/0UYDpgKgNRMGfTqgks9
         JiF4BQlu1Z2MHejUkSJQ4ccEPtiKOJxFtXy0f7mWBcvygzzWp+gHzZ83tkEcKx10IjVZ
         q3ig==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327;
        h=to:subject:message-id:date:from:mime-version:dkim-signature;
        bh=bWjpKKyh3/4CMUkP53wfIM10P+HElDNTuijV+P+vf5Y=;
        fh=uuYTS3oLusgIYHEAt3/9vG6Sx7RBNT5DQ4Olr+vIDHk=;
        b=CKdo/dvzIhcr4Y1YeQgMqJsCcAqpZnemvUB+TbY9OqvqHALuUbkhOUteKlkewUS75Y
         1/Ie0QainUmky0I816prfgQgYPOfn+OGnIvBm5T03C511h4BtDrh/5mIYFLj2dE+4fsi
         YDZ477xoM3ZrumYBn2/LrRX873fbIvwdz2YyshVQUCNwz1LewIa8ak64l1AvmVKwwCk7
         sVOeVFjv3TdUJzLBof+Swq4GOhyQV9gp3BAjlXWtbNbGcWHIgvQ32DH5aoMutcuJJVlS
         /daC06IBJjKk/mP8daRd23uHrj/OrSPd0lQBL68KsfeUnkwUzp5F5kC713BJCXY3OoY/
         i2kw==;
        darn=python.org
ARC-Authentication-Results: i=1; mx.google.com; arc=none
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1794781376; x=1795386176; darn=python.org;
        h=content-type:to:subject:message-id:date:from:mime-version:from:to
         :cc:subject:date:message-id:reply-to:content-type;
        bh=bWjpKKyh3/4CMUkP53wfIM10P+HElDNTuijV+P+vf5Y=;
        b=XVfwmVuwLgol0G23eHlJIejC9zD0dJVt+qzNc0pWS1gWHroJ+GSfB4QCJ7KFkJtoWh
         MjLesxMQQ1Gb0P0yyW/OmPDHpI9kTb1MjG3+VN5LbEFa7oWMB3M1TRQ8EJZnhXy1rfvl
         YqSFJeDp83KEBe0/nb1uHb6jGBTuiB4tbKZsLq0hIO+otdBhtsz3zAwSD1t2EX3EeJ4y
         8oVLZu1y5VheiWWPHsi4FNVoBEAZ6fFwk+N/QsAWWU/PO53CSwEh/xRhd1pEyJarzsjn
         K8CooKwVFqPDG3kk+YEP9LjVvmlhybVR3caiYHAyixBsmatEmqP3N80WheOqHIrMzKKO
         38aQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1794781376; x=1795386176;
        h=content-type:to:subject:message-id:date:from:mime-version:x-gm-gg
         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
         :content-type;
        bh=bWjpKKyh3/4CMUkP53wfIM10P+HElDNTuijV+P+vf5Y=;
        b=p+C6kUrVkdlRodI3v2PA8TSeFd1gLU9tdcb91lhDW75GbWf8JFi4kQsOcu8ccwHq+T
         9FXqxsr5M0p7bQ1/LFEOMX8v5wNNcw7BscGT2GkaSeWJ/EQ76bD2nZtfNwBFmj03/yfN
         fqK2BDuan24jPoaAQZ6x/BWtVRdYTXpTkdOLl5TSMVtWISvaCzW5lnRuthE86QPN6hHf
         8QH6YmZOh2p5fhEKSH8TGO8Y5bF3xwM1LuOmNOX5EbZHeruvOyVT3IB2T40XfGWNW5Ql
         DN2PtXshMqVw+uiXYS/sBkgY6ykeuaxVbYwKQ3LqNaXBriHDnGw6j+8JafA+vxC8tZEL
         sw6w==
X-Gm-Message-State: APuU4Z93y1RVIuCMfmvp2k4uxEN9MqZC4xA8O23rM4rEaRByIN/ct/K0
	ta0OGFjPFAdKglg6Cpr+0MxOSWEWDaJy44I+hkda+a88zcHuItQPgJEetw0wYQVOkeYjETHvKL0
	+cn6w7CFnHuCaF+TF+zExhHACYFTDZUu4dw==
X-Gm-Gg: AYBFou3KVrhcTdey4bzG5zNCTdqeMFkGRS8/o6KyRll4K0TJVm6jlnPKfE5u2n+7FYk
	1KRMK9z1iyF9JQsl2Wz4X4hLFA0vAtwTvsudNQCUghy0lICrdblMReuYTPElOthlIAMom26v1dd
	tRIZAWysFGto49fFLOiA+kGqb2AqL+KKB4tmjujkps+HwWx0jCQ3mCyqJsqyuNQmBsTiOuiX96K
	Ij/YpVqit2LZGBIaSfZVALcvzq9lrtkUnO/plYoqrQJfgfv+lvcvzUGS2iA8tR+AimZynQPE8ul
	KL2/lCq8tgYjL7mYUzD4Tgafdkpqx0nKlDAJ8cCIZTrMx/IvwT4Lh/3JBg7gy+7LhJ4HUmFT3Pl
	FA9TOsyqDMdRPdmp12bDyqGbkOvRtEd0ryvJ9iE7v7hzG+8Manx01eo7pzc5Mhgub8LEiT7vBYJ
	AJjW3En8ra8fxMqAs=
X-Received: by 2002:a17:907:3e1f:b0:c3e:f39b:2286 with SMTP id
 a640c23a62f3a-c5095db45d8mr697079966b.0.1794781375558; Sun, 15 Nov 2026
 14:22:55 -0800 (PST)
X-Gm-Features: AclHuK8D34S_jDjh1yIirfO8SQPPkSGuOqsYu9UHIwUR1PFbrv_qhNXLshlY33w
X-MailFrom: zielinski.macio@gmail.com
X-Mailman-Rule-Hits: emergency
X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; header-match-python-announce-list.python.org-0; header-match-python-announce-list.python.org-1; header-match-python-announce-list.python.org-2; header-match-python-announce-list.python.org-3; header-match-python-announce-list.python.org-4
Message-ID-Hash: 7K3GJUX7VFQZFCUVC7AZJE56JMCL4BRY
X-Message-ID-Hash: 7K3GJUX7VFQZFCUVC7AZJE56JMCL4BRY
X-Mailman-Approved-At: Sun, 15 Nov 2026 22:20:17 -0500
X-Content-Filtered-By: Mailman/MimeDel 3.3.11b1
X-Mailman-Version: 3.3.11b1
Precedence: list
List-Id: Announcement-only list for the Python programming language <python-announce-list.python.org>
Archived-At: <https://mail.python.org/archives/list/python-announce-list@python.org/message/7K3GJUX7VFQZFCUVC7AZJE56JMCL4BRY/>
List-Archive: <https://mail.python.org/archives/list/python-announce-list@python.org/>
List-Help: <mailto:python-announce-list-request@python.org?subject=help>
List-Owner: <mailto:python-announce-list-owner@python.org>
List-Post: <mailto:python-announce-list@python.org>
List-Subscribe: <mailto:python-announce-list-join@python.org>
List-Unsubscribe: <mailto:python-announce-list-leave@python.org>