Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect type hints for custom classes named Sequence, Union, etc #241

Open
markuspi opened this issue Jan 30, 2025 · 0 comments
Open

Incorrect type hints for custom classes named Sequence, Union, etc #241

markuspi opened this issue Jan 30, 2025 · 0 comments

Comments

@markuspi
Copy link

Defining a custom pybind11 class that is named Sequence, Union, or other classes from the typing package leads to incorrectly generated signatures:

#include <pybind11/pybind11.h>
#include <pybind11/cast.h>

namespace py = pybind11;
using namespace pybind11::literals;

class Sequence {
  public:
  Sequence(int x) {}
};

PYBIND11_MODULE(sandbox, m)
{
  py::class_<Sequence>(m, "Sequence") //
    .def(py::init<int>(), "x"_a);
}

pybind11-stubgen incorrectly annotated the CTOR as self: typing.Sequence, x: int instead of self, x: int or at least self: Sequence, x: int

from __future__ import annotations
import typing
__all__ = ['Sequence']
class Sequence:
    def __init__(self: typing.Sequence, x: int) -> None:
        ...

Note that the original docstring __init__(self: sandbox.Sequence, x: int) contains the fully qualified class name, which would in theory allow more fine-grained differentiation between custom classes and classes from the standard library:

$ python -c "import sandbox; print(help(sandbox.Sequence))"

Help on class Sequence in module sandbox:

class Sequence(pybind11_builtins.pybind11_object)
 |  Method resolution order:
 |      Sequence
 |      pybind11_builtins.pybind11_object
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __init__(...)
 |      __init__(self: sandbox.Sequence, x: int) -> None
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from pybind11_builtins.pybind11_object:
 |
 |  __new__(*args, **kwargs) class method of pybind11_builtins.pybind11_object
 |      Create and return a new object.  See help(type) for accurate signature.
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

No branches or pull requests

1 participant