{"id":1110,"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":"python","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,"search_vec":"'1.16.0':25 'activ':29 'bug':42 'creat':11 'current':22 'cycl':31 'decor':46 'develop':30 'dynam':10,44 'exist':15 'fix':43 'function':12,16,45,48 'includ':35 'librari':2,8 'maintain':27 'makefun':1,3 'metaprogram':47 'modifi':14 'new':38 'python':7,39 'regular':33 'runtim':19 'signatur':17,49 'small':6 'support':36 'updat':34 'version':24,40","created_at":"2026-04-05T13:06:08.151536+00:00","updated_at":"2026-04-16T16:24:43.439682+00:00","problems":[{"fix":"Ensure the arguments passed to the makefun-created function strictly match its defined signature, including positional/keyword usage and required arguments.","cause":"makefun wrappers perform strict signature checking, raising TypeError early if provided arguments do not match the dynamically defined function's signature. This is an intended feature to ensure argument compliance before the wrapper body executes.","error":"TypeError: <function_name>() got an unexpected keyword argument 'arg_name'"},{"fix":"Ensure all symbols (variables, functions) used within the dynamically created function's implementation are accessible from the scope where the makefun function is defined, or pass them explicitly if they are intended to be arguments.","cause":"When makefun dynamically creates a function, its code relies on the scope where it is defined. If the function's implementation uses variables or functions that are not locally or globally available at the point of its creation, a NameError will occur when the created function is called.","error":"NameError: name 'some_variable' is not defined"},{"fix":"Carefully review the function signature string to ensure it follows correct Python syntax for function definitions (e.g., proper parameter names, default values, type hints, parentheses).","cause":"The signature string provided to makefun functions (e.g., `func_signature` in `create_function` or `@with_signature`) does not conform to valid Python function signature syntax.","error":"SyntaxError: invalid syntax"},{"fix":"Ensure the `func_name` argument (if provided) or the name derived from the `func_signature` is a valid Python identifier (alphanumeric and underscores, not starting with a number).","cause":"This error occurs when the internal `co_name` (code object name) provided or derived for the dynamically created function is not a valid Python identifier, often due to special characters or invalid formatting.","error":"ValueError: Invalid co_name"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"1.16.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/smarie/python-makefun","docs":null,"changelog":null,"pypi":"https://pypi.org/project/makefun/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":"verified"}}