Skip to content

Replace the return type of make_iterator from "iterator" to "typing.Iterator" #2270

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

Closed
AWhetter opened this issue Jun 26, 2020 · 2 comments · Fixed by #2273
Closed

Replace the return type of make_iterator from "iterator" to "typing.Iterator" #2270

AWhetter opened this issue Jun 26, 2020 · 2 comments · Fixed by #2273

Comments

@AWhetter
Copy link
Contributor

Issue description

Currently when declaring a method that returns the result of py::make_iterator(), the type signature in the docstring uses the return type iterator, which is not a valid Python type. In the example below we see the docstring:

__iter__(self: numbers.Numbers) -> iterator\n

I was hoping that the type could instead be switched to either typing.Iterator or Iterator.

__iter__(self: numbers.Numbers) -> typing.Iterator\n

The use case where this is coming up:
I'm generating type stubs of a pybind module using stubgen. When running mypy with the generated stubs I see the following error:

error: name 'iterator' is not defined

Reproducible example code

#include <pybind11/pybind11.h>

namespace py = pybind11;

static const std::vector<string> numbers = {"1", "2", "3"};

class Numbers {
    std::vector<int>::iterator begin() { return numbers.begin(); }
    std::vector<int>::iterator end() { return numbers.end(); }
}

PYBIND11_MODULE(numbers, m) {
    py::class<Numbers>(m, "Numbers")
        .def("__iter__",
            [](const Numbers& self) {
                return py::make_iterator(self.begin(), self.end());
            },
            py::keep_alive<0, 1>()
        )
    ;
}
>>> import numbers
>>> numbers.Numbers.__iter__.__doc__
'__iter__(self: numbers.Numbers) -> iterator\n'
@AWhetter
Copy link
Contributor Author

AWhetter commented Jun 26, 2020

I'm going to include this under the same issue because it seems very similar; the py::none type declares it's type in a docstring as none, but I would expected it to be None so that it produces a valid Python type.

@AWhetter
Copy link
Contributor Author

I've since discovered that the Iterator problem has already been fixed in 57070fb
However "none" is still an issue.

AWhetter added a commit to AWhetter/pybind11 that referenced this issue Jun 28, 2020
wjakob pushed a commit that referenced this issue Jun 29, 2020
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 a pull request may close this issue.

1 participant