PyO3 / PyO3/pyo3

Quality of life macro for generic functions

Open
#3,865 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.2k
Forks
1k
Avg merge
2d 6h
Merged PRs (30d)
66

Description

Hello there!
im currently using something like this in my project (please ignore the specifics, i want to share the general idea without ripping it out of the context that sparked the idea):

macro_rules! concrete_ordinal_encoding {
    ($typename: ident, $pymodule: ident) => {
        paste::item! {
            #[pyfunction]
            fn [< ordinal_encoding_$typename >]<'py>(py: Python<'py>, labels: Vec<$typename>) -> &'py PyArray1<usize> {
                ordinal_encoding(labels).into_pyarray(py)
            }
            $pymodule.add_function(wrap_pyfunction!([< ordinal_encoding_$typename >], $pymodule)?)?;
        }
    };
}

So, naturally, i wondered: is it a good idea to turn it into some sort of concrete_function!() macro to specialize generic functions to interface with python? on the python side, it could be implemented with an object with __getitem__ or __getattr__ to resolve what specific function to call.
Do you think it is worth a dependency? Do you think this approach smells nice enough to be included in pyo3?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the shown concrete_ordinal_encoding! macro and assess how a concrete_function! macro could specialize generic functions for Python. Consider the proposed Python-side getitem or getattr resolution and whether this belongs in PyO3 or requires a dependency. Done means reaching a clear decision on the API and scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, rust
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.