Skip to main content

anymerge

PyPI - Version PyPI - Python Version

anymerge merges dataclasses, TypedDicts and Pydantic models field by field. A reducer in each field's type annotation decides how the old and new values combine, and merge returns a new instance.

import dataclasses
import operator
from typing import Annotated

from anymerge import Reducer, merge

@dataclasses.dataclass
class State:
    status: str
    total: Annotated[int, Reducer(operator.add)] = 0
    log: Annotated[list[str], Reducer(operator.add)] = dataclasses.field(default_factory=list)


state = State(status="pending")
state = merge(state, {"status": "running", "total": 3, "log": ["started"]})
state = merge(state, {"total": 2, "log": ["finished"]})

print(state)
#> State(status='running', total=5, log=['started', 'finished'])

status has no reducer, so it takes the new value. total and log are combined with operator.add.

Features

  • Any two-argument function works as a reducer: operator.add, operator.or_, max, or your own.
  • Reducer(...) merges a nested model field by field instead of replacing it.
  • Partial updates: a dict changes only the keys it has, and a Pydantic model only the fields that were set explicitly.
  • Overwrite(value) replaces a field without running its reducer.
  • merge_by_key("id") updates, appends and, with Remove(key), deletes items in a list of records.
  • reduce(state, updates) applies a sequence of updates in order.
  • Fully typed: merge and reduce return the type of the model you pass in.

Installation

pip install anymerge

To merge Pydantic models, install the pydantic extra, which adds Pydantic 2:

pip install "anymerge[pydantic]"

anymerge needs Python 3.11 or later.

Why anymerge?

Dict-merging libraries such as mergedeep and deepmerge pick a strategy per call or per Python type, and they modify their first argument in place. anymerge sets the rule on each field of a typed model and leaves its arguments unchanged.

Pydantic's model_copy(update=...) replaces each field you pass with the new value. It can't add to the old value, and it replaces a nested model as a whole.

Documentation

The documentation covers supported models, reducers, updates and lists of records, and has an API reference. Changes are listed in the release notes, and bugs and ideas go in the issue tracker.

anymerge is at version 0.x, so a minor release can still change the API. Patch releases never do, and breaking changes are called out in the release notes. See the stability policy.

License

anymerge is distributed under the terms of the MIT license.

Metadata

Release files for anymerge 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anymerge 0.1.0
File Size Uploaded Provenance Actions
anymerge-0.1.0.tar.gz 10.0 kB With provenance Details for anymerge-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anymerge 0.1.0
File Interpreter ABI Platform Actions
anymerge-0.1.0-py3-none-any.whl Python 3 none any Details for anymerge-0.1.0-py3-none-any.whl

Total release size: 24.2 kB

Release files / anymerge-0.1.0.tar.gz

Download URL anymerge-0.1.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
066bd561f0e91b2ec12455cd0c58d54d88c4c125ff576473707cfe1bb08bc81d
BLAKE2b-256 checksum
How to use checksums
87c73a9253be13459495b8a270080223b1765aeb0a3e5e0053f237d2c74deee9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Release files / anymerge-0.1.0-py3-none-any.whl

Download URL anymerge-0.1.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
92fb7bb6480b55d923620f126cae10b257b0e6d857678042275a07a6579a2109
BLAKE2b-256 checksum
How to use checksums
8511f5d6ee80fcd0bf204879df3937545f20033d64e2322ce1ec1bdfba66d768
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

Security for anymerge 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for anymerge-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 19, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page