mo-kwargs

raw JSON →
8.703.26061 verified Mon Apr 27 auth: no python

Python library for object destructuring of function parameters using typed dictionaries/objects. Current version 8.703.26061. Very frequent releases (multiple per month).

pip install mo-kwargs
error AttributeError: 'NoneType' object has no attribute 'items'
cause The @kwargs decorator expects a dict-like argument; passing None causes this error.
fix
Ensure the argument passed to the decorated class is a dictionary, not None. Add a guard or default empty dict.
error TypeError: __init__() got multiple values for argument 'x'
cause The same key appears both in the input dict and as a positional argument, often due to mixing decorator usage.
fix
Remove the duplicate argument. Either pass via dict or as keyword, not both.
error ImportError: cannot import name 'kwargs_decorator' from 'mo_kwargs'
cause Old import path removed in recent versions.
fix
Use 'from mo_kwargs import kwargs' instead.
breaking Version 8 introduced major changes: the default destructuring changed from 'typed' to 'explicit' mode. Existing code relying on implicit type matching may fail. Use @kwargs to opt into typed mode if needed.
fix Add @kwargs decorator to classes that previously worked without it, or adjust parameter names to match dictionary keys exactly.
gotcha The library uses the underscore (_) to mark positional parameters. Parameters starting with underscore are not destructured from the input dict; they remain as positional arguments.
fix Ensure that any parameter you want to receive from a dict does not start with underscore.
deprecated The old pattern using 'from mo_kwargs import kwargs_decorator' is deprecated and may be removed. Use the kwargs function directly.
fix Replace 'from mo_kwargs import kwargs_decorator' with 'from mo_kwargs import kwargs'.

Decorate a class with @kwargs to automatically destructure a dictionary argument into constructor parameters.

from mo_kwargs import kwargs

@kwargs
class MyClass:
    def __init__(self, a, b=10, c=None):
        self.a = a
        self.b = b
        self.c = c

obj = MyClass({'a': 1, 'c': 2})
print(obj.a, obj.b, obj.c)  # 1 10 2