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. It always returns list[dict[str, object]].

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))), and the returned rows are materialized in memory.

Use max_rows to fail fast before an expansion becomes too large:

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

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.1.0.tar.gz (89.4 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.1.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flat_adapter-0.1.0.tar.gz
  • Upload date:
  • Size: 89.4 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.1.0.tar.gz
Algorithm Hash digest
SHA256 a23fab66903939a15987f6ad236d11da8a55e575d7013cc8c54d9536b9f297e4
MD5 811cffd8bc4b89f1f6941a02eb6d6e67
BLAKE2b-256 e4925747736289a1a524426d50ac3c8bddbd51b46d02903f8c3d2779080aa6a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for flat_adapter-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: flat_adapter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24ccb7808cbc79736f5214f3d9d9d54ab611cb0930bdc88fbf26a961835cd479
MD5 7231b84944de95d80b66c5d4e932087c
BLAKE2b-256 1bdfc60b437158b2fb75363b735315a3ef5269109a1b1468cf323e8af891bb8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for flat_adapter-0.1.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