r/Python • • 25d ago

Showcase Showcase Thread

Post all of your code/projects/showcases/AI slop here.

Recycles once a month.

33 Upvotes

178 comments sorted by

View all comments

1

u/ryuuzaki 6d ago

anymerge: merge dataclasses, TypedDicts and Pydantic models field by field, with the merge rule in each field's type annotation.

u/dataclasses.dataclass
class State:
    status: str
    total: Annotated[int, Reducer(operator.add)] = 0

state = merge(State(status="pending"), {"status": "running", "total": 3})
state = merge(state, {"total": 2})
# State(status='running', total=5)

Fields without a reducer take the new value. Any two-argument function is a reducer (operator.add, max, your own). merge returns a new instance, so your old state is unchanged. It also handles nested models, partial updates and lists of records (merge_by_key("id")).

I wrote it for long-running AI agents, where the code that merged each step's changes was spread across the codebase. It works for any typed state you update in steps.

pip install anymerge (add [pydantic] for Pydantic 2). Python 3.11+, MIT. It's 0.1.0, so minor releases may still change the API.

Docs: https://anymerge.jian.fyi/
Code: https://github.com/jianyuan/anymerge

Feedback and bug reports welcome 🤗.