Skip to main content

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.

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

Quick Links

Key Topics

Contributing

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

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlty-0.0.1.dev1.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlty-0.0.1.dev1-py3-none-any.whl (28.0 kB view details)

Uploaded Python 3

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

Hashes for sqlty-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 8de42c16c9fe871e62ac90ad89fec2fbef3ff8c86be047766240cc81df0a5710
MD5 e2ad9e3833dd5ae097a7470051366fa7
BLAKE2b-256 4de125fc4d9178767e05a5551a266467c5a7d3df18197fd4f44e81016a9a199f

See more details on using hashes here.

File details

Details for the file sqlty-0.0.1.dev1-py3-none-any.whl.

File metadata

File hashes

Hashes for sqlty-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 113b70ae0e2157261e0136e72c3d6fd664feda17418462db9b8648f3a9676b42
MD5 024b0764a87182f34d6355f1f7cc9652
BLAKE2b-256 b9174d56240da9c6879c63e08958cf1730d83d2f4a22d6e2ac47be0fe072785d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page