{"library":"backcall","code":"from backcall import callback_prototype\n\n@callback_prototype\ndef my_prototype_handler(sender, message, detail=None):\n    # This function defines the expected signature for callbacks.\n    # Positional parameters without defaults, keyword-only with defaults (Python 3).\n    pass\n\ndef register_event_handler(callback_func):\n    # The adapt method inspects the callback and wraps it if necessary\n    # to discard extra arguments not in the prototype.\n    adapted_callback = my_prototype_handler.adapt(callback_func)\n    print(f\"Registered adapted callback: {adapted_callback.__name__}\")\n    return adapted_callback\n\n# Example callbacks\ndef simple_callback(sender, message):\n    print(f\"Simple: {sender} says '{message}'\")\n\ndef detailed_callback(sender, message, detail):\n    print(f\"Detailed: {sender} says '{message}' with detail: {detail}\")\n\ndef super_detailed_callback(sender, message, detail, extra_arg='default'):\n    print(f\"Super Detailed: {sender} says '{message}' with detail: {detail} and extra: {extra_arg}\")\n\n# Registering and calling\nprint(\"--- Registering simple_callback ---\")\nadapted_simple = register_event_handler(simple_callback)\nadapted_simple('System', 'Hello world!')\n\nprint(\"--- Registering detailed_callback ---\")\nadapted_detailed = register_event_handler(detailed_callback)\nadapted_detailed('System', 'Another message', 'Important info')\n\n# This would normally raise TypeError if passed to .adapt directly, \n# but backcall will raise it because the callback has *more* arguments than prototype.\nprint(\"\\n--- Attempting to register super_detailed_callback (expecting TypeError) ---\")\ntry:\n    register_event_handler(super_detailed_callback)\nexcept TypeError as e:\n    print(f\"Caught expected error: {e}\")","lang":"python","description":"To use `backcall`, define a 'callback prototype' function using the `@callback_prototype` decorator. This prototype's signature specifies all parameters that your API might pass. Then, use the `prototype.adapt(callback)` method to inspect and potentially wrap user-provided callback functions. If a callback takes fewer arguments than the prototype, `adapt` creates a wrapper that discards the extra arguments. If a callback takes *more* arguments than the prototype, `adapt` will raise a `TypeError`.","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":1},{"runtime":"python:3.10-slim","exit_code":1},{"runtime":"python:3.11-alpine","exit_code":1},{"runtime":"python:3.11-slim","exit_code":1},{"runtime":"python:3.12-alpine","exit_code":1},{"runtime":"python:3.12-slim","exit_code":1},{"runtime":"python:3.13-alpine","exit_code":1},{"runtime":"python:3.13-slim","exit_code":1},{"runtime":"python:3.9-alpine","exit_code":1},{"runtime":"python:3.9-slim","exit_code":1}]}