{"library":"makefun","title":"makefun library","description":"makefun is a small Python library for dynamically creating functions and modifying existing function signatures at runtime. It is currently at version 1.16.0 and maintains an active development cycle with regular updates, including support for new Python versions and bug fixes.","status":"active","version":"1.16.0","language":"en","source_language":"en","source_url":"https://github.com/smarie/python-makefun","tags":["dynamic functions","decorators","metaprogramming","function signature"],"install":[{"cmd":"pip install makefun","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Used to create new functions from a signature and an implementation.","symbol":"create_function","correct":"from makefun import create_function"},{"note":"Used to create a wrapper around an existing function, potentially with a modified signature.","symbol":"create_wrapper","correct":"from makefun import create_wrapper"},{"note":"A decorator for creating a wrapper for a function, similar to `functools.wraps` but with more control over signature manipulation.","symbol":"wraps","correct":"from makefun import wraps"},{"note":"A decorator to apply a new signature to an existing function.","symbol":"with_signature","correct":"from makefun import with_signature"}],"quickstart":{"code":"from makefun import create_function, wraps\nfrom inspect import Signature, Parameter\n\n# Example 1: Creating a function from scratch\ndef my_implementation(a, b):\n    return f\"a={a}, b={b}\"\n\nsig = Signature([\n    Parameter('a', Parameter.POSITIONAL_OR_KEYWORD),\n    Parameter('b', Parameter.POSITIONAL_OR_KEYWORD, default=2)\n])\n\ndynamic_func = create_function(sig, my_implementation, doc=\"A dynamically created function\")\nprint(dynamic_func(1)) # Output: a=1, b=2\nprint(dynamic_func(10, b=20)) # Output: a=10, b=20\n\n# Example 2: Wrapping an existing function and changing its signature\ndef original(x, y):\n    return x * y\n\n@wraps(original, new_sig=Signature([Parameter('val', Parameter.POSITIONAL_OR_KEYWORD)]))\ndef my_wrapper(val):\n    # original expects x, y. Let's map 'val' to both.\n    return original(val, val)\n\nprint(my_wrapper(val=5)) # Output: 25","lang":"python","description":"This quickstart demonstrates the core functionalities: creating a new function with `create_function` by specifying its signature and implementation, and wrapping an existing function while modifying its signature using the `wraps` decorator."},"warnings":[{"fix":"Upgrade your Python environment to 3.9+ or pin makefun to an older version (e.g., `makefun<1.16.0`) in your requirements.","message":"Version 1.16.0 (and later) drops official support for Python versions older than 3.9. If you are using Python 3.8 or earlier, you must pin makefun to a version less than 1.16.0.","severity":"breaking","affected_versions":"< 1.16.0 (for users on Python < 3.9)"},{"fix":"Review code that inspects `__wrapped__` or `__signature__` on functions decorated with `makefun.wraps` after upgrading to 1.15.0+.","message":"The behavior of `wraps` was updated in version 1.15.0 to be more compliant with PEP 362 regarding the setting of `__wrapped__` and `__signature__` attributes. While generally an improvement, this could subtly change behavior for applications inadvertently relying on the pre-1.15.0 `wraps` implementation.","severity":"gotcha","affected_versions":"< 1.15.0"},{"fix":"Upgrade to makefun 1.15.3 or newer to resolve this `SyntaxError`.","message":"Prior to version 1.15.3, creating functions where a default argument's value was a subclass of a basic primitive (e.g., `int` or `str`) could lead to a `SyntaxError: invalid syntax`.","severity":"gotcha","affected_versions":"< 1.15.3"},{"fix":"Upgrade to makefun 1.15.2 or newer, or rename problematic coroutine functions.","message":"Versions prior to 1.15.2 could raise a `SyntaxError` if a native coroutine function's name contained the substring `'return'`.","severity":"gotcha","affected_versions":"< 1.15.2"},{"fix":"Upgrade to makefun 1.15.1 or newer, or ensure function names follow standard Python naming conventions for `co_name` compatibility.","message":"In Python 2, prior to version 1.15.1, creating functions with names starting or ending with `_`, or containing `__`, could result in a `ValueError: Invalid co_name`.","severity":"gotcha","affected_versions":"< 1.15.1 (Python 2 only)"}],"env_vars":null,"last_verified":"2026-04-05T00:00:00.000Z","next_check":"2026-07-04T00:00:00.000Z"}