Metadata-Version: 2.4
Name: rowguard
Version: 0.4.0
Summary: Validation-first SQLAlchemy queries with Pydantic row validation.
Project-URL: Homepage, https://github.com/eddiethedean/rowguard
Project-URL: Documentation, https://rowguard.readthedocs.io
Project-URL: Repository, https://github.com/eddiethedean/rowguard
Project-URL: Changelog, https://github.com/eddiethedean/rowguard/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/eddiethedean/rowguard/issues
Author: RowGuard Contributors
License: MIT
License-File: LICENSE
Keywords: pydantic,query,sqlalchemy,sqlrules,validation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Database
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: pydantic<3,>=2.7
Requires-Dist: sqlalchemy<3,>=2.0
Requires-Dist: sqlrules>=0.4.0
Provides-Extra: async
Requires-Dist: aiosqlite>=0.20; extra == 'async'
Requires-Dist: greenlet>=3.0; extra == 'async'
Provides-Extra: dev
Requires-Dist: hypothesis>=6.100; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest-benchmark>=4; extra == 'dev'
Requires-Dist: pytest-cov>=5; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: docs
Requires-Dist: furo<2026,>=2024.8.6; extra == 'docs'
Requires-Dist: myst-parser<5,>=3.0; extra == 'docs'
Requires-Dist: sphinx-copybutton<1,>=0.5; extra == 'docs'
Requires-Dist: sphinx-design<1,>=0.5; extra == 'docs'
Requires-Dist: sphinx<9,>=7.2; extra == 'docs'
Provides-Extra: postgresql
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgresql'
Description-Content-Type: text/markdown

# RowGuard

[![CI](https://github.com/eddiethedean/rowguard/actions/workflows/ci.yml/badge.svg)](https://github.com/eddiethedean/rowguard/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/rowguard.svg)](https://pypi.org/project/rowguard/)
[![Documentation](https://readthedocs.org/projects/rowguard/badge/?version=latest)](https://rowguard.readthedocs.io/en/latest/)
[![Python Versions](https://img.shields.io/pypi/pyversions/rowguard.svg)](https://pypi.org/project/rowguard/)

RowGuard makes every SQLAlchemy query return **validated Pydantic models**—or
**explicit rejected rows**. It does not silently drop bad data.

Use it when you already have SQLAlchemy Core tables/selects and need typed reads
with deterministic rejection handling. It is **not** an ORM and does not replace
SQLAlchemy or Pydantic.

## Status

Current release: **[0.4.0](https://rowguard.readthedocs.io/en/latest/project/changelog.html)**
(sync + async Core, streaming). See
[Supported vs planned](https://rowguard.readthedocs.io/en/latest/project/supported.html)
for what is shipped versus deferred (ORM 0.5, callback/quarantine 0.6).

## Install

```bash
pip install rowguard
```

Requires Python 3.10+, Pydantic v2, SQLAlchemy 2.x, and SQLRules ≥0.4. See the
[installation guide](https://rowguard.readthedocs.io/en/latest/guides/installation.html).

## Quickstart

Full walkthrough: [Quickstart](https://rowguard.readthedocs.io/en/latest/guides/quickstart.html).

```python
from typing import Annotated

from pydantic import BaseModel, Field
from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine
from sqlalchemy.orm import Session

import rowguard


class UserRead(BaseModel):
    id: int
    name: str
    age: Annotated[int, Field(ge=18)]


metadata = MetaData()
users = Table(
    "users",
    metadata,
    Column("id", Integer, primary_key=True),
    Column("name", String),
    Column("age", Integer),
)

engine = create_engine("sqlite+pysqlite:///:memory:")
metadata.create_all(engine)

with engine.begin() as connection:
    connection.execute(
        users.insert(),
        [
            {"id": 1, "name": "Ada", "age": 37},
            {"id": 2, "name": "Legacy", "age": 12},
        ],
    )

with Session(engine) as session:
    # Disable SQLRules pushdown so invalid rows reach Pydantic and appear in rejected.
    result = rowguard.select(
        session=session,
        table=users,
        model=UserRead,
        on_reject="collect",
        use_sqlrules=False,
    )
    print(result.models)
    print(result.rejected)

    with rowguard.stream(
        session=session,
        table=users,
        model=UserRead,
        on_reject="skip",
        use_sqlrules=False,
    ) as stream:
        for model in stream:
            print(model)
```

With `use_sqlrules=True` (the default), supported constraints such as `age >= 18`
are pushed into SQL, so invalid candidate rows may never be returned. See
[SQLRules pushdown](https://rowguard.readthedocs.io/en/latest/guides/sqlrules-pushdown.html)
and the [FAQ](https://rowguard.readthedocs.io/en/latest/guides/faq.html).

## Public API (0.4.0)

Full reference: [API guide](https://rowguard.readthedocs.io/en/latest/api.html) ·
[Python autodoc](https://rowguard.readthedocs.io/en/latest/reference/api.html) ·
[Error catalog](https://rowguard.readthedocs.io/en/latest/reference/errors.html).

| Function | Purpose |
| --- | --- |
| `select(...)` | Build and execute a table query with validation |
| `execute(...)` | Validate rows from an existing `Select` |
| `validate_rows(...)` | Validate mappings without SQL |
| `compile_plan(...)` | Compile an `ExecutionPlan` without executing |
| `stream(...)` | Stream validated models without buffering accepted rows |
| `aselect(...)` | Async `select` for `AsyncSession` / `AsyncConnection` |
| `aexecute(...)` | Async `execute` for `AsyncSession` / `AsyncConnection` |
| `astream(...)` | Async stream (`AsyncStreamResult`) without buffering accepted rows |

Rejection policies: `raise` (default), `collect`, `skip` — see
[rejection policies](https://rowguard.readthedocs.io/en/latest/guides/rejection-policies.html).

Optional planning knobs: `compiled_rules=` (precompiled SQLRules), `strict=`
(Pydantic), `field_map=` / `column_map=` (validated at plan time).

Streaming knobs: `yield_per=`, `observers=` (`StreamObserver` / `BaseStreamObserver`).
Observers remain sync callables in 0.4. See the
[streaming guide](https://rowguard.readthedocs.io/en/latest/guides/streaming.html).

Async note: only DB I/O is awaited. Pydantic validation runs on the event loop;
heavy models can block. Prefer `async with rowguard.astream(...)` for cleanup.
Install async extras with `pip install rowguard[async]`. Details:
[async guide](https://rowguard.readthedocs.io/en/latest/guides/async.html).

## Architecture

```text
Pydantic Model
      │
      ▼
SQLRules
      │
      ▼
SQLAlchemy Query
      │
      ▼
Database
      │
      ▼
Row Adapter
      │
      ▼
Pydantic Validation
      │
      ├── Accepted Model
      └── Rejected Row
```

More detail:
[architecture overview](https://rowguard.readthedocs.io/en/latest/architecture_overview.html)
· [design philosophy](https://rowguard.readthedocs.io/en/latest/guides/design-philosophy.html)
· [specification](https://rowguard.readthedocs.io/en/latest/spec.html).

## Documentation

- [Docs home](https://rowguard.readthedocs.io/en/latest/)
- [Start here](https://rowguard.readthedocs.io/en/latest/guides/start-here.html)
- [Quickstart](https://rowguard.readthedocs.io/en/latest/guides/quickstart.html)
- [Supported vs planned](https://rowguard.readthedocs.io/en/latest/project/supported.html)
- [Examples](https://rowguard.readthedocs.io/en/latest/examples/index.html)
- [API](https://rowguard.readthedocs.io/en/latest/api.html)
- [Changelog](https://rowguard.readthedocs.io/en/latest/project/changelog.html)
- [Roadmap](https://rowguard.readthedocs.io/en/latest/project/roadmap.html)

Build docs locally:

```bash
pip install -e ".[docs]"
make docs
# open docs/_build/html/index.html
```

## Development

See [Contributing](https://rowguard.readthedocs.io/en/latest/developer/CONTRIBUTING.html),
[Security](https://rowguard.readthedocs.io/en/latest/project/security.html), and
[Releasing](https://rowguard.readthedocs.io/en/latest/project/releasing.html).

```bash
pip install -e ".[dev,async]"
make all          # ruff + mypy + pytest --cov
python examples/basic.py
python examples/streaming.py
python examples/async_basic.py
```

## License

MIT
