Skip to main content

flat-adapter

flat-adapter converts nested mappings into deterministic flat rows suitable for database imports and ETL pipelines.

Quick start

from flat_adapter import FlatAdapter


class ItemAdapter(FlatAdapter):
    item_id: int
    quantity: int


class OrderAdapter(FlatAdapter):
    order_id: int
    items: list[ItemAdapter]


rows = OrderAdapter.adapt(
    {
        "order_id": "1001",
        "items": [
            {"item_id": "10", "quantity": "2"},
            {"item_id": "20", "quantity": "1"},
        ],
    }
)

assert rows == [
    {"order_id": 1001, "item_id": 10, "quantity": 2},
    {"order_id": 1001, "item_id": 20, "quantity": 1},
]

The first release supports Mapping[str, object] input, nested mappings, typed scalar conversion, optional fields, aliases, custom Field paths, and deterministic Cartesian expansion of nested lists. adapt() returns list[dict[str, object]]; iter_adapt() returns an iterator over the same rows.

Field configuration

Use typing.Annotated to attach extraction metadata without a runtime assignment or a cast:

from typing import Annotated

from flat_adapter import Field, FlatAdapter


class CustomerAdapter(FlatAdapter):
    customer_id: Annotated[int, Field(source="payload.customer_id")]
    display_name: Annotated[str, Field(source="payload.name", default="Unknown")]

The legacy name: int = Field(...) form remains supported at runtime, but Annotated is the preferred form for mypy --strict projects.

Performance and row limits

Depth of seven to ten nested adapters is normally safe; the main cost comes from Cartesian expansion. For list lengths L1, L2, ..., the result count can grow as product(max(1, len(Li))). adapt() materializes all rows in memory, while iter_adapt() yields them lazily.

Use max_rows to fail fast before an expansion becomes too large. The limit works with both eager and lazy APIs:

rows = OrderAdapter.adapt(payload, max_rows=10_000)

For large results, consume rows lazily:

for row in OrderAdapter.iter_adapt(payload, max_rows=10_000):
    process(row)

Run the local benchmark scenarios with:

uv run python benchmarks/flatten_benchmark.py

Development

The project uses uv for environments, dependencies, and lockfile management.

uv sync
uv run pre-commit install
uv run pre-commit run --all-files
uv run pytest --cov=flat_adapter --cov-report=term-missing
uv run ruff check src tests
uv run mypy --strict src tests
uv build
uv run twine check dist/*

Package layout

src/flat_adapter/  Library package
tests/unit/        Pure behavior tests
docs/              English/Russian guides and promotion notes
benchmarks/        Manual performance scenarios

See CONTEXT.md for architecture boundaries and TECHDEBT.md for known risks.

Русская версия руководства: docs/README.ru.md. English guide: docs/README.en.md.

Versioning

Releases follow Semantic Versioning. While the package is below 1.0.0, a minor release may include a documented contract change; patch releases remain backward-compatible fixes. CHANGELOG.md records user-visible changes.

Download files

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

Source Distribution

flat_adapter-0.2.0.tar.gz (90.7 kB view details)

Uploaded Source

Built Distribution

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

flat_adapter-0.2.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file flat_adapter-0.2.0.tar.gz.

File metadata

  • Download URL: flat_adapter-0.2.0.tar.gz
  • Upload date:
  • Size: 90.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for flat_adapter-0.2.0.tar.gz
Algorithm Hash digest
SHA256 2d082ba6c15ee17f544efb253aaefe8f71e0687e7fa1a5fecd73738dc00cffb4
MD5 d81c7c78ae9a7b3a17026ea7876fd21f
BLAKE2b-256 1c8f84cf7dd4dfcf51d03de1270e58eea1a7e56ad651471f585181a633a8312c

See more details on using hashes here.

Provenance

The following attestation bundles were made for flat_adapter-0.2.0.tar.gz:

Publisher: release.yml on groshevpavel/flat_adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flat_adapter-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: flat_adapter-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for flat_adapter-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7dadbe0b78bfd28520ce3ee04b4c1f09a3a4635d9201055cbad09f86018a32d
MD5 5ea40594e54ca93d59b34dfe8d8ecde6
BLAKE2b-256 9c5ebcb860ae88b059e87b69a59fd817e4dc218cc821e396c1d58ef650ad6f6f

See more details on using hashes here.

Provenance

The following attestation bundles were made for flat_adapter-0.2.0-py3-none-any.whl:

Publisher: release.yml on groshevpavel/flat_adapter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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