Skip to main content

Python Dynamic Default Arguments

Tests Package PyPI Downloads License: MIT-0

Shared defaults you can update, and factories evaluated for each omitted argument. Pure Python, with no runtime dependencies.

Supports Python 3.8–3.15 and Cython 3.

Installation

From PyPI:

python -m pip install dynamic-default-args

Or from a local checkout (Python 3.9+):

python -m pip install .

Shared Defaults

from dynamic_default_args import default, default_factory, dynamic_default_args

timeout = default(5)

@dynamic_default_args
def request(url, timeout=timeout):
    return url, timeout

assert request("/status") == ("/status", 5)
timeout.value = 10
assert request("/status") == ("/status", 10)
assert request("/status", None) == ("/status", None)

Only omitted arguments resolve. Explicit values, including None, pass through. A default cell shares its value; use a factory when each call needs a fresh object.

Omitted static defaults stay with the wrapped callable. Place @dynamic_default_args above a validation decorator (such as Pydantic's @validate_call) to validate dynamic values while preserving static omissions. Static positional-only defaults are forwarded only when needed to reach a later positional-only argument.

Factories

default_factory computes a value per omission. Its optional depends_on names earlier parameters, passed to the factory in the given order:

@dynamic_default_args()
def take(items, limit=default_factory(len, depends_on=("items",)), *,
         output=default_factory(list)):
    output.extend(items[:limit])
    return output

assert take([1, 2, 3]) == [1, 2, 3]
assert take([1, 2, 3], 1) == [1]

Factories are synchronous and resolve from left to right. default(callable) passes the callable itself; default_factory(callable) invokes it. With an inner validator, factories receive dependency values before validation.

Methods

Instance, class, and static methods, inheritance, and bound methods are supported. Either decorator order works with classmethod and staticmethod.

class Client:
    timeout = 5

    @dynamic_default_args
    def request(self, timeout=default_factory(lambda self: self.timeout,
                                              depends_on=("self",))):
        return timeout

client = Client()
assert client.request() == 5
client.timeout = 10
assert client.request() == 10

Class-method factories can similarly depend on cls.

Named Defaults

Use named_default to retrieve a shared cell across modules:

from dynamic_default_args import named_default

shared = named_default("myapp.timeout", 5)
assert named_default("myapp.timeout") is shared
named_default("myapp.timeout").value = 10
named_default.unregister("myapp.timeout")

The first registration wins; later initialization values are ignored.

Defaults Outside the Signature

The defaults mapping is useful for typed functions and Cython parameters:

scale = default(2.0)

@dynamic_default_args(defaults={"x": scale})
def compute(x: float = 0.0) -> float:
    return x * 2

assert compute() == 4.0
scale.value = 3.0
assert compute() == 6.0

For typed Cython parameters, keep valid native defaults and supply dynamic values through this mapping. Python wrappers around cpdef are supported; C-level calls retain native defaults. See the Cython example and its build-and-run helper.

Other Features

  • Preserves positional-only, keyword-only, and variadic argument binding.
  • Supports partials, callable instances, and inspectable built-ins.
  • Preserves coroutine, generator, and async-generator behavior. Defaults resolve when execution starts, or on first iteration for generators.
  • format_doc=True updates simple docstring fields such as {timeout} when a shared value changes. Set it to False to leave docstrings unchanged.
  • Shared cells expose connect(callback), disconnect(callback), and disconnect_all() for change notifications.
  • signature=inspect.Signature(...) adapts callables without signature metadata; its static defaults are supplied explicitly to the target.

More runnable Python examples are in examples/factories.py. See CHANGELOG.md for changes. Licensed under MIT-0.

Metadata

Release files for dynamic-default-args 0.1.1

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

Source distribution (sdist)

Source distribution for dynamic-default-args 0.1.1
File Size Uploaded Provenance Actions
dynamic_default_args-0.1.1.tar.gz 24.8 kB Without provenance Details for dynamic_default_args-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dynamic-default-args 0.1.1
File Interpreter ABI Platform Actions
dynamic_default_args-0.1.1-py3-none-any.whl Python 3 none any Details for dynamic_default_args-0.1.1-py3-none-any.whl

Total release size: 37.2 kB

Release files / dynamic_default_args-0.1.1.tar.gz

Download URL dynamic_default_args-0.1.1.tar.gz
Size 24.8 kB
Tags Source
SHA-256 checksum
How to use checksums
788875edab931a25f11450d8bfafb67134d7af210d2359d2fee8f4e57c7790b2
BLAKE2b-256 checksum
How to use checksums
d473c15425bfbcc2881c7e9088f82a5a792c8cd76f16bdfce1c0029182224e70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / dynamic_default_args-0.1.1-py3-none-any.whl

Download URL dynamic_default_args-0.1.1-py3-none-any.whl
Size 12.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d73a99974f984658a27e3037b08bea58cd8a031f06bc3550c109e8255f47fb1
BLAKE2b-256 checksum
How to use checksums
c8a3f959a850d7d8a4f0b06531727c4ad3a77b77bebbd681aefe378050741329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for dynamic-default-args 0.1.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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