Skip to content

Function overloading #196

Open
Open
@benruijl

Description

@benruijl

I have a function that should yield type A when it is called with a single string argument and Sequence[A] when it is called with multiple string arguments. In PyO3, I use PyTuple and PyObject, and I do the necessary type checks in the function itself. For example:

pub fn test(
    _cls: &Bound<'_, PyType>,
    args: &Bound<'_, PyTuple>,
) -> PyResult<PyObject> {
    if args.len() == 1 {
      ...
    } else {
      ...
    }
}

The associated manual stub entry looks like:

@overload
@classmethod
def test(_cls, args: str) -> A:
   pass

@overload
@classmethod
def test(_cls, *args: str) -> Sequence[A]:
   pass

I also have a more complicated function that takes in a tuple of type A | B, and that should return type C when all arguments are of type A, otherwise it should return type D. My stub file looks like:

@overload
@classmethod
def __call__(_cls, *args: A) -> C:
   pass

@overload
@classmethod
def _call(_cls, *args: A | B) -> D:
   pass

Would it be possible for pyo3-stub-gen to support these complicated function overloadings?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions