Skip to content

Ignore generic type signature in pybind11 functions and methods with overloads #9070

Description

@AWhetter

I don't know if this is the correct place to address this issue, but I know that there's the expertise here to be able to discuss the correct approach to the problem. I've written this as a feature request, but there may be better approaches to the problem.

Use Case

I have a C++ module that's exposed to Python with pybind11. In this module I have a class with a method that has multiple types. To use an example from the pybind11 documentation:

py::class_<Pet>(m, "Pet")
.def(py::init<const std::string &, int>())
.def("set", (void (Pet::*)(int)) &Pet::set, "Set the pet's age")
.def("set", (void (Pet::*)(const std::string &)) &Pet::set, "Set the pet's name");

This produces docstrings that look like the following:

>>>help(example.Pet)
classPet(__builtin__.object)
|Methodsdefinedhere:
||__init__(...)
|__init__(self: Pet, arg0: str, arg1: int) ->None||set(...)
|set(*args, **kwargs) ->Any|Overloadedfunction.
||1.set(self: Pet, arg0: int) ->None||Setthepet'sage||2.set(self: Pet, arg0: str) ->None||Setthepet'sname

Current Behaviour

When running stubgen against this module, the generated type stubs look like the following:

fromtypingimportoverloadclassPet:
def__init__(self: Pet, arg0: str, arg1: int) ->None: ...
@overloaddefset(self: Pet, arg0: int) ->None: ...
@overloaddefset(self: Pet, arg0: str) ->None: ...
@overloaddefset(*args, **kwargs) ->Any: ...

The inclusion of the last overload essentially makes the type definition of the method useless because it'll accept anything.
The reason that this feels to me like an issue for stubgen to address is because I think that pybind11 is doing the right thing. The inclusion of set(*args, **kwargs) -> Any makes sense because tools like Sphinx need to be given a valid type signature and those tools expect that signature on the first line of the docstring.

Expected Behaviour

When running stubgen against this module, the generated type stubs should look like the following:

fromtypingimportoverloadclassPet:
def__init__(self: Pet, arg0: str, arg1: int) ->None: ...
@overloaddefset(self: Pet, arg0: int) ->None: ...
@overloaddefset(self: Pet, arg0: str) ->None: ...

Note that the set(*args, **kwargs) -> Any type signature has been ignored.

Implementation

If we decide that getting stubgen to ignore this specific type signature is the right thing to do, how should we do it? Ignoring *args, *kwargs here is not correct because having *args and **kwargs could be part of a valid signature:

mypy/mypy/stubdoc.py

Lines 193 to 202 in 007b7af

defget_signatures(self) ->List[FunctionSig]:
"""Return sorted copy of the list of signatures found so far."""
defhas_arg(name: str, signature: FunctionSig) ->bool:
returnany(x.name==nameforxinsignature.args)
defargs_kwargs(signature: FunctionSig) ->bool:
returnhas_arg('*args', signature) andhas_arg('**kwargs', signature)
# Move functions with (*args, **kwargs) in their signature to last place.
returnlist(sorted(self.signatures, key=lambdax: 1ifargs_kwargs(x) else0))

We could look for the "Overloaded signature." line and ignore the type signature in only that case. It feels brittle, but I don't know how else we can determine whether this situation exists.

Do we need to worry about the case where someone might decide to write the following:

"""func(*args, **kwargs) ->AnyDoathing.
func() ->NoneDosomethingelse.

Versions

mypy 0.780
Python 3.7.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions