Skip to main content

Fassung

CI codecov

Fassung combines asyncpg, pydantic and the template strings to provide an ergonomic, type- and SQL-injection-safe interface for working with PostgreSQL databases. It also allows safe nested query composition.

Fassung is very similar to asyncpg. The main difference is that fassung does neither accept positional parameters nor normal python string ("" or f"") as arguments for it's query methods. Instead it relies on python's new template string literals (t"") for query construction. This has several benefits:

  • It is SQL-injection-safe: you can't pass a string with SQL code as a parameter
  • It is ergonomic: you can pass variables directly into the query, your IDE will provide autocompletion and type checking
  • It is composable: you can compose queries by concatenating template strings

Additionally, fassung also uses pydantic to map the query results to python objects, like dataclasses or pydantic models.

It is currently just a proof of concept and not ready for production use. Some features like COPY are not implemented yet.

Documentation

The documentation can be found at https://smorokin.github.io/fassung/.

Installation

Fassung requires python 3.14 because it relies on template string literals (PEP 750). You can install it with

pip install fassung

or

uv add fassung

Usage

The central class is the Pool class, which is a context manager for a connection pool. You can use the Pool class to create connections. Each connection can execute queries and create transactions.

Basic example:

import asyncio
from datetime import date
from string.templatelib import Template
from pydantic import BaseModel

from fassung import Pool, Transaction


class Student(BaseModel):
    id: int
    name: str
    birth_date: date


async def fetch_all(
    transaction: Transaction, limit: int | None = None, offset: int | None = None, where_clause: Template = t""
) -> tuple[int, list[Student]]:

    limit_query = t""
    if limit is not None:
        limit_query = t"LIMIT {limit}"

    offset_query = t""
    if offset is not None:
        offset_query = t"OFFSET {offset}"

    count_query = t"SELECT COUNT(*) FROM students {where_clause}"
    count = await transaction.fetchval(int, count_query)

    query = t"SELECT * FROM students {limit_query} {offset_query} {where_clause}"
    students = await transaction.fetch(Student, query)

    return count, students


async def main():
    pool = Pool.from_connection_string("postgresql://user:password@localhost:5432/testdb")
    async with pool.acquire() as connection:
        async with connection as transaction:
            age = 18
            students: list[Student] = await transaction.fetch(
                Student, t"SELECT * FROM students WHERE age = {age}"
            )
            for student in students:
                print(student.name)

            since = date(2002, 5, 14)
            count, students = await fetch_all(transaction, where_clause=t"WHERE birth_date = {since}")

if __name__ == "__main__":
    asyncio.run(main())

More examples can be found in the tests/examples directory.

Contributing

We use uv for dependency management, ruff for formatting & linting, pyright for type checking, and pytest for testing. For information on how to set up the development environment and contribute to fassung, please see the Contributing Guide.

License

Fassung is licensed under the MIT license.

Why is it called fassung?

Fassung is the german word for "frame" or "socket". Since it assembles several great python libraries and features (asyncpg, pydantic and template strings) into a single package, it seemed appropriate.

Influences

The idea for the integration of pydantic with asyncpg came from the onlymaps project.

Metadata

Release files for fassung 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fassung 0.3.0
File Size Uploaded
fassung-0.3.0.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fassung 0.3.0
File Interpreter ABI Platform
fassung-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.0 kB

Release files / fassung-0.3.0.tar.gz

Download URL fassung-0.3.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cbe6063578494d60833b88e776fa9d2655d4d437f1356c4f74ed4be0c9bd597e
BLAKE2b-256 checksum
How to use checksums
537042581dc0f91797e0d5ab3401eb00ceb0a53b6a42200f135ddad3504f7f2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 1, 2026.

Transparency log

Release files / fassung-0.3.0-py3-none-any.whl

Download URL fassung-0.3.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34191a744e5f5c1acf1c7e0867b6c679ae6ef18bfa414e85e2fde1a7b0ff39d7
BLAKE2b-256 checksum
How to use checksums
dc2d0cbd1abbe77d0323448e2808f3754e46ab4507e732487ce48bad4ae02552
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page