Type-safe SQL queries for Python with automatic stub generation
Project description
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.
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
.pyifiles automatically - 🎯 SQL Type Casts: Use PostgreSQL-style
::TYPEcasts 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
Quick Links
- Installation Guide - Setup instructions for pip, uv, poetry, and more
- User Guide - Complete guide with basic and advanced usage
- API Reference - Detailed API documentation for all classes and methods
- Examples Guide - Step-by-step tutorials from basic to advanced
Key Topics
- Basic Usage - Creating registries, writing queries, generating stubs
- Multi-Registry Pattern - Organizing queries for large applications
- Type Inference - How SQLTy infers Python types from SQL
- Internals - Architecture and implementation details
Contributing
See DEVELOPMENT.md for development setup, workflows, and contribution guidelines.
License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlty-0.0.1.dev1.tar.gz.
File metadata
- Download URL: sqlty-0.0.1.dev1.tar.gz
- Upload date:
- Size: 22.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8de42c16c9fe871e62ac90ad89fec2fbef3ff8c86be047766240cc81df0a5710
|
|
| MD5 |
e2ad9e3833dd5ae097a7470051366fa7
|
|
| BLAKE2b-256 |
4de125fc4d9178767e05a5551a266467c5a7d3df18197fd4f44e81016a9a199f
|
File details
Details for the file sqlty-0.0.1.dev1-py3-none-any.whl.
File metadata
- Download URL: sqlty-0.0.1.dev1-py3-none-any.whl
- Upload date:
- Size: 28.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
113b70ae0e2157261e0136e72c3d6fd664feda17418462db9b8648f3a9676b42
|
|
| MD5 |
024b0764a87182f34d6355f1f7cc9652
|
|
| BLAKE2b-256 |
b9174d56240da9c6879c63e08958cf1730d83d2f4a22d6e2ac47be0fe072785d
|