Skip to content

Conversation

@tylerriccio33
Copy link

This PR seeks to add a new method for decorating a python function, which registers it against an existing connection.

We use duckdb at my job a lot, and one thing we'd love to do is use a decorator on top of a function instead of calling the create_function method. The decorator, at times, feels more pythonic.

This PR implements this feature as syntactic sugar in python only. There is no cpp implementation which seems to be abnormal for this client. Please let me know if this is acceptable, and if not I'll change it!

Second, I'm seeking the maintainer's opinion on a testing regime... I have dedicated tests of course, but I'd love to incorporate some existing tests around create_function. I parametrized one in tests/fast/udf/test_scalar_arrow.py but that's all. Let me know if you want more and I can!

@tylerriccio33 tylerriccio33 marked this pull request as ready for review January 10, 2026 15:24
@evertlammerts evertlammerts changed the base branch from main to v1.5-variegata January 12, 2026 13:01
@evertlammerts
Copy link
Collaborator

Hey @tylerriccio33, thanks for the PR! I get the allure of a decorator, but an instance method as a decorator is a little awkward... Plus, it's limiting: you may want to register the function on multiple connections. The monkey patching (DuckDBPyConnection.register_function) isn't great either.

You're right that this would need more tests as well, but tbh I don't think we should be doing this at all, at least not in the current form. It's easy enough for users to provide their own wrapper:

  def wrapper(f):
      con.create_function(f.__name__, f)
      return f

  @wrapper
  def my_func(x: int) -> int:
      return x + 1

BTW: we're likely to rework UDFs this year. Decorators are on the list as well!

@tylerriccio33
Copy link
Author

Alrighty, makes sense! Interested in the UDF rework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants