hoffstadt / hoffstadt/DearPyGui
Usage of decorated functions as item callback
- Dominant language
- C++
- Stars
- 15.6k
- Forks
- 783
- PR merge metrics
- No merged PRs in 30d
Description
## Version of Dear PyGui
Version: 1.11.1 and 2.0.0 on Python 3.12
Operating System: MacOS 15.3.1
## My Issue/Question
Are there any restrictions on using decorated functions as item callbacks?
I am trying to use decorated functions as item callbacks. These functions are decorated to catch exceptions in order to log them and show an error message to the user.
Decorated functions called from a callback seem to lack the function parameters, as I get the following error message:
```text
Function foo raised an exception: foo() missing 3 required positional arguments: 'sender', 'app_data', and 'user_data'
```
Calling the decorated functions directly, i.e., not via a callback, works as expected. It does not matter whether the function is a class member or not.
## To Reproduce
Steps to reproduce the behavior:
1. Start the sample code from below.
2. The decorated functions are called directly and print the expected output.
3. Click on any of the two buttons to call any of the functions.
4. The callback fails and prints an error message in the terminal.
## Expected behavior
The decorated functions called via a callback produce the same output as those being called directly.
## Standalone, minimal, complete and verifiable example
```python
import functools
import logging
import dearpygui.dearpygui as dpg
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def log(logger: logging.Logger):
def log_decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
logger.info(f"Calling function {func.__name__}")
try:
return func(*args, **kwargs)
except Exception as e:
logger.error(f"Function {func.__name__} raised exception {e}")
return wrapper
return log_decorator
class Bob:
def __init__(self):
pass
@log(logger)
def foo(self, sender, app_data, user_data):
print(f"Sender {sender}")
print(f"App Data {app_data}")
print(f"user_data {user_data}")
@log(logger)
def foo(sender, app_data, user_data):
print(f"Sender {sender}")
print(f"App Data {app_data}")
print(f"user_data {user_data}")
if __name__ == "__main__":
# Calling decorated functions works as expected.
bob = Bob()
print(foo(None, None, "Called foo"))
print(bob.foo(None, None, "Called Bob.foo"))
dpg.create_context()
dpg.create_viewport(title='Custom Title', width=600, height=300)
with dpg.window(label="Example Window"):
# Calling decorated functions via callback fails with error:
# Function foo raised exception foo() missing 3 required positional arguments: 'sender', 'app_data', and 'user_data'
dpg.add_button(
label="Call decorated function 'foo'", callback=foo, user_data="Called foo via callback"
)
dpg.add_button(
label="Call decorated function 'bob.foo'", callback=bob.foo, user_data="Called bob.foo via callback"
)
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()
```
Contributor guide
Assessment
This issue has not been assessed yet.