Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

🎛️ Python Redux

codecov PyPI - Python Version PyPI PyPI - License GitHub Workflow Status

🌟 Overview

Python Redux is a Redux implementation for Python, bringing Redux's state management architecture to Python applications.

🔎 Sample Usage

Minimal todo application store implemented using python-redux:

import uuid
from dataclasses import replace
from typing import Sequence

from immutable import Immutable

from redux import (
    BaseAction,
    BaseEvent,
    CompleteReducerResult,
    FinishAction,
    ReducerResult,
)
from redux.main import Store


# state:
class ToDoItem(Immutable):
    id: str
    content: str
    is_done: bool = False


class ToDoState(Immutable):
    items: Sequence[ToDoItem]


# actions:
class AddTodoItemAction(BaseAction):
    content: str


class MarkTodoItemDone(BaseAction):
    id: str


class RemoveTodoItemAction(BaseAction):
    id: str


# events:
class CallApi(BaseEvent):
    parameters: object


# reducer:
def reducer(
    state: ToDoState | None,
    action: BaseAction,
) -> ReducerResult[ToDoState, BaseAction, BaseEvent]:
    if state is None:
        return ToDoState(
            items=[
                ToDoItem(
                    id=uuid.uuid4().hex,
                    content='Initial Item',
                ),
            ],
        )
    if isinstance(action, AddTodoItemAction):
        return replace(
            state,
            items=[
                *state.items,
                ToDoItem(
                    id=uuid.uuid4().hex,
                    content=action.content,
                ),
            ],
        )
    if isinstance(action, RemoveTodoItemAction):
        return replace(
            state,
            actions=[item for item in state.items if item.id != action.id],
        )
    if isinstance(action, MarkTodoItemDone):
        return CompleteReducerResult(
            state=replace(
                state,
                items=[
                    replace(item, is_done=True) if item.id == action.id else item
                    for item in state.items
                ],
            ),
            events=[CallApi(parameters={})],
        )
    return state


store = Store(reducer)


# subscription:
dummy_render = print
store.subscribe(dummy_render)


# autorun:
@store.autorun(
    lambda state: state.items[0].content if len(state.items) > 0 else None,
)
def reaction(content: str | None) -> None:
    print(content)


@store.view(lambda state: state.items[0])
def first_item(first_item: ToDoItem) -> ToDoItem:
    return first_item


@store.view(lambda state: [item for item in state.items if item.is_done])
def done_items(done_items: list[ToDoItem]) -> list[ToDoItem]:
    return done_items


# event listener, note that this will run async in a separate thread, so it can include
# io operations like network calls, etc:
dummy_api_call = print
store.subscribe_event(
    CallApi,
    lambda event: dummy_api_call(event.parameters, done_items()),
)

# dispatch:
store.dispatch(AddTodoItemAction(content='New Item'))

store.dispatch(MarkTodoItemDone(id=first_item().id))

store.dispatch(FinishAction())

⚙️ Features

  • Redux API for Python developers.

  • Reduce boilerplate by dropping type property, payload classes and action creators:

    • Each action is a subclass of BaseAction.
    • Its type is checked by utilizing isinstance (no need for type property).
    • Its payload are its direct properties (no need for a separate payload object).
    • Its creator is its auto-generated constructor.
  • Use type annotations for all its API.

  • Immutable state management for predictable state updates using python-immutable.

  • Offers a streamlined, native API for handling side-effects asynchronously, eliminating the necessity for more intricate utilities such as redux-thunk or redux-saga.

  • Incorporates the autorun decorator and the view decorator, inspired by the mobx framework, to better integrate with elements of the software following procedural patterns.

  • Supports middlewares.

📦 Installation

The package handle in PyPI is python-redux

Pip

pip install python-redux

Poetry

poetry add python-redux

🛠 Usage

Handling Side Effects with Events

Python-redux introduces a powerful concept for managing side effects: Events. This approach allows reducers to remain pure while still signaling the need for side effects.

Why Events?

  • Separation of Concerns: By returning events, reducers stay pure and focused solely on state changes, delegating side effects to other parts of the software.
  • Flexibility: Events allow asynchronous operations like API calls to be handled separately, enhancing scalability and maintainability.

How to Use Events

  • Reducers: Reducers primarily return a new state. They can optionally return actions and events, maintaining their purity as these do not enact side effects themselves.
  • Dispatch Function: Besides actions, dispatch function can now accept events, enabling a more integrated flow of state and side effects.
  • Event Listeners: Implement listeners for specific events. These listeners handle the side effects (e.g., API calls) asynchronously.

Best Practices

  • Define Clear Events: Create well-defined events that represent specific side effects.
  • Use Asynchronously: Design event listeners to operate asynchronously, keeping your application responsive. Note that python-redux, by default, runs all event handler functions in new threads.

This concept fills the gap in handling side effects within Redux's ecosystem, offering a more nuanced and integrated approach to state and side effect management.

See todo sample below or check the todo demo or features demo to see it in action.

Autorun Decorator

Inspired by MobX's autorun and reaction, python-redux introduces the autorun decorator. This decorator requires a selector function as an argument. The selector is a function that accepts the store instance and returns a derived object from the store's state. The primary function of autorun is to establish a subscription to the store. Whenever the store is changed, autorun executes the selector with the updated store. Importantly, the decorated function is triggered only if there is a change in the selector's return value. This mechanism ensures that the decorated function runs in response to relevant state changes, enhancing efficiency and responsiveness in the application.

See todo sample below or check the todo demo or features demo to see it in action.

View Decorator

Inspired by MobX's computed, python-redux introduces the view decorator. It takes a selector and each time the decorated function is called, it only runs the function body if the returned value of the selector is changed, otherwise it simply returns the previous value. So unlike computed of MobX, it doesn't extract the requirements of the function itself, you need to provide them in the return value of the selector function.

Combining reducers - combine_reducers

You can compose high level reducers by combining smaller reducers using combine_reducers utility function. This works mostly the same as the JS redux library version except that it provides a mechanism to dynamically add/remove reducers to/from it. This is done by generating an id and returning it along the generated reducer. This id is used to refer to this reducer in the future. Let's assume you composed a reducer like this:

reducer, reducer_id = combine_reducers(
    state_type=StateType,
    first=straight_reducer,
    second=second_reducer,
)

You can then add a new reducer to it using the reducer_id like this:

store.dispatch(
    CombineReducerRegisterAction(
        combine_reducers_id=reducer_id,
        key='third',
        third=third_reducer,
    ),
)

You can also remove a reducer from it like this:

store.dispatch(
    CombineReducerRegisterAction(
        combine_reducers_id=reducer_id,
        key='second',
    ),
)

Without this id, all the combined reducers in the store tree would register third reducer and unregister second reducer, but thanks to this reducer_id, these actions will only target the desired combined reducer.

🎉 Demo

For a detailed example, see features demo.

🤝 Contributing

Contributions following Python best practices are welcome.

📜 License

This project is released under the Apache-2.0 License. See the LICENSE file for more details.

Metadata

Release files for python-redux 0.25.2.dev326020910310098575

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

Built distributions (wheels)

Table of built distributions (wheels) for python-redux 0.25.2.dev326020910310098575
File
python_redux-0.25.2.dev326020910310098575-cp312-cp312-macosx_10_13_x86_64.whl CPython 3.12 CPython 3.12 macOS 10.13+ x86-64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_arm64.whl CPython 3.11 CPython 3.11 Windows ARM64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-win32.whl CPython 3.11 CPython 3.11 Windows x86-32 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_x86_64.whl CPython 3.11 CPython 3.11 Linux musl 1.2+ x86-64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_aarch64.whl CPython 3.11 CPython 3.11 Linux musl 1.2+ ARM64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64, Linux glibc 2.17+ x86-64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ ARM64, Linux glibc 2.17+ ARM64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details
python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_10_9_x86_64.whl CPython 3.11 CPython 3.11 macOS 10.9+ x86-64 Details

Total release size: 17.4 MB

Release files / python_redux-0.25.2.dev326020910310098575-cp312-cp312-macosx_10_13_x86_64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp312-cp312-macosx_10_13_x86_64.whl
Size 1.1 MB
Tags CPython 3.12 macOS 10.13+ x86-64
SHA-256 checksum
How to use checksums
3b2be5a8186fe85ab2a09743ce431e204778f2ad13fabb506f0e0c547a0c3dd2
BLAKE2b-256 checksum
How to use checksums
602c4e7f6bfd6eed14137e4bed5694df6641a0bba07ad267a77c53bd0f18be14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_arm64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_arm64.whl
Size 977.5 kB
Tags CPython 3.11 Windows ARM64
SHA-256 checksum
How to use checksums
760ad4792be1d6e45912f70d2aa571d35682f2e15caa7c515476c2b9c7b511cc
BLAKE2b-256 checksum
How to use checksums
5f0160ed0af00eafdfe82588b8c51bcdced454624e98fdbf6d9177a69dc9dd31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_amd64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-win_amd64.whl
Size 1.0 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
5f6494c20a160dd9c6df879eb2b1635443679200c17e200dad2eb716a3908f7e
BLAKE2b-256 checksum
How to use checksums
038f51dec1ed007740eca6340a9742b88028cc2ac15728903527ad3c5b530e0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-win32.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-win32.whl
Size 989.4 kB
Tags CPython 3.11 Windows x86-32
SHA-256 checksum
How to use checksums
6fc716e2fe6b09aa93eb3776a84780f62867df3453a1e1a0acb7da0574db2e2b
BLAKE2b-256 checksum
How to use checksums
c899dba942f091c2680ced733d4380cd8da184c4558e99ee176340880117833b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_x86_64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_x86_64.whl
Size 2.8 MB
Tags CPython 3.11 Linux musl 1.2+ x86-64
SHA-256 checksum
How to use checksums
7657ca41b4f40be3ec8de7aac72b815e3cf9db62cf655e40938716f1e5636ff5
BLAKE2b-256 checksum
How to use checksums
0300f99d8380091d977d7d6622c2bd750dae17df61128c7627d00e3a2df87352
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_aarch64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-musllinux_1_2_aarch64.whl
Size 2.8 MB
Tags CPython 3.11 Linux musl 1.2+ ARM64
SHA-256 checksum
How to use checksums
e707e37860a660ae6e4eafe7254a8bc3f9c64a7a1d87db4a93813a2aead268ce
BLAKE2b-256 checksum
How to use checksums
e55a59922807a5efa3fe7b127090dc3ecca94d26424aae4229347906cfca5574
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
Size 2.8 MB
Tags CPython 3.11 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
e9f4f298ea8c4ed6d57fc3d0e28c07c56afaaada8e1d0ddeb5b46c0343ba4d6d
BLAKE2b-256 checksum
How to use checksums
0fece9ffdd7359220e583b9ec5f831fd8bdd55c83abd5c587d3bcf07911bf1a1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
Size 2.8 MB
Tags CPython 3.11 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64
SHA-256 checksum
How to use checksums
1da5b03e9681e79fc573ea91f0df59176f38f424d07f13133693b80d903c3338
BLAKE2b-256 checksum
How to use checksums
4656b84593c88153fd8b02f653db1288c18db20f6dc09bd48cf833668dc636a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_11_0_arm64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_11_0_arm64.whl
Size 1.0 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
211c8a79311deb6653ec08b16d668b16f0674788a49fc1c165f114fddb951ee6
BLAKE2b-256 checksum
How to use checksums
546a70bd947f2a50f4e18dedf65c9f86ff84a914f93d6dbcab594e90c6ba3d37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release files / python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_10_9_x86_64.whl

Download URL python_redux-0.25.2.dev326020910310098575-cp311-cp311-macosx_10_9_x86_64.whl
Size 1.1 MB
Tags CPython 3.11 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
f9affdc36001495197ffde7e1055c9da3440aae0aaa9fe3cc661577baefe5f25
BLAKE2b-256 checksum
How to use checksums
733ec5990ec458d1e9fa316751c7851a9e043a71334040d611af346a9bde319e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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 Feb 9, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.24.0

2 release files

0.23.0

2 release files

0.22.2

2 release files

0.22.1

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.20.2

2 release files

0.19.1

2 release files

0.19.0

2 release files

0.18.3

2 release files

0.18.2

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.16.0

2 release files

0.15.9

2 release files

0.15.8

2 release files

0.15.7

2 release files

0.15.6

2 release files

0.15.5

2 release files

0.15.4

2 release files

0.15.3

2 release files

0.14.3

2 release files

0.14.2

2 release files

0.14.0

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.12.7

2 release files

0.12.6

2 release files

0.12.5

2 release files

0.12.4

2 release files

0.12.3

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.7

2 release files

0.10.6

2 release files

0.10.4

2 release files

0.10.3

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.9.24

2 release files

0.9.22

2 release files

0.9.21

2 release files

0.9.20

2 release files

0.9.19

2 release files

0.9.17

2 release files

0.9.16

2 release files

0.9.15

2 release files

0.9.14

2 release files

0.9.13

2 release files

0.9.12

2 release files

0.9.11

2 release files

0.9.10

2 release files

0.9.9

2 release files

0.9.8

2 release files

0.9.6

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.0

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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