Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

SQLTy

⚠️ Experimental Package: This is an experimental project under active development. APIs may change without notice. Feedback and contributions are welcome!

Type-safe SQL queries for Python with automatic stub generation and runtime validation.

Python 3.14+ License: MIT

Powered by sqlglot - SQL parsing and analysis engine that makes type inference possible across multiple SQL dialects.

Overview

SQLTy provides type-safe SQL query execution with automatic type inference and stub generation. Write SQL queries with explicit type casts, and let SQLTy generate precise type stubs for full mypy integration.

from sqlty import SQLRegistry

class Query(SQLRegistry):
    pass

sql = Query.sql

# Type-safe query with automatic inference
query = sql("SELECT id::INTEGER, name::TEXT FROM users")
# Type: SQL[tuple[int, str]]

# Execute with full type safety
from sqlty.drivers.psycopg import execute
result = execute(conn, query, {})
# Type: Iterator[tuple[int, str]]

# Full mypy support
for user_id, name in result:
    print(f"User {user_id}: {name}")

Features

  • 🔒 Type Safety: Full mypy integration with precise type inference
  • 🚀 Automatic Stub Generation: Analyze code and generate .pyi files automatically
  • 🎯 SQL Type Casts: Use PostgreSQL-style ::TYPE casts for explicit typing
  • 📦 Multi-Registry Support: Organize queries into multiple registries
  • 🔍 Source Tracking: Generated stubs include source location comments
  • 🌐 Multiple SQL Dialects: Support for PostgreSQL, MySQL, SQLite, and more via sqlglot

Quick Start

Install:

pip install sqlty
# or
uv add sqlty

Write queries:

See advanced usage documentation for:

  • Multi-registry and multiple schema support
  • Configuration via pyproject.toml
  • Full package example with stub generation and usage
# queries.py
from sqlty import SQLRegistry

class Query(SQLRegistry):
    pass

sql = Query.sql
# app.py
from queries import sql
from sqlty.drivers.psycopg import execute

query = sql("SELECT id::INTEGER, name::TEXT FROM users WHERE id = :id")
# Type: SQL[tuple[int, str]]

result = execute(conn, query, {"id": 123})
# Type: Iterator[tuple[int, str]]

Generate stubs:

sqlty . --mode=full

Type check:

mypy .
# Success: no issues found

Pre-commit Hook

Automatically generate stubs on commit by adding to your .pre-commit-config.yaml:

repos:
  - repo: https://github.com/jirikuncar/sqlty
    rev: v0.1.0  # Use the latest version
    hooks:
      - id: sqlty
        # Optional: customize with args
        # args: [".", "--mode=full", "--schema=schema.sql"]

Documentation

📚 Complete Documentation

Key Topics

Contributing

See DEVELOPMENT.md for development setup, workflows, and contribution guidelines.

License

MIT License - see LICENSE file for details.

Metadata

Release files for sqlty 0.0.1.dev1

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

Source distribution (sdist)

Source distribution for sqlty 0.0.1.dev1
File Size Uploaded
sqlty-0.0.1.dev1.tar.gz 22.5 kB Details

Built distribution (wheel)

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

Total release size: 50.5 kB

Release files / sqlty-0.0.1.dev1.tar.gz

Download URL sqlty-0.0.1.dev1.tar.gz
Size 22.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8de42c16c9fe871e62ac90ad89fec2fbef3ff8c86be047766240cc81df0a5710
BLAKE2b-256 checksum
How to use checksums
4de125fc4d9178767e05a5551a266467c5a7d3df18197fd4f44e81016a9a199f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release files / sqlty-0.0.1.dev1-py3-none-any.whl

Download URL sqlty-0.0.1.dev1-py3-none-any.whl
Size 28.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
113b70ae0e2157261e0136e72c3d6fd664feda17418462db9b8648f3a9676b42
BLAKE2b-256 checksum
How to use checksums
b9174d56240da9c6879c63e08958cf1730d83d2f4a22d6e2ac47be0fe072785d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.7

Release history Release notifications | RSS feed

This release

0.0.1.dev1 This release

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