Skip to content

Is it possible to type the iterator? #76

Closed
@alkatar21

Description

@alkatar21

This is probably more of a question and I don't know where else to ask it right now.

Is it possible to type the iterator?

I don't know if pybind11 doesn't currently offer that in general, if it's because of my possibly specific implementation or if this package just hasn't implemented that so far?

class Value
{
public:
  virtual ~Value() = default;
};

class Array : public Value, public std::vector<Value*>
{
public:
  using std::vector<Value*>::vector;
....
};
....
  py::class_<Array, Value>(m, "Array", "")
....
    .def("__iter__", [](Array& array) {
      return py::make_iterator(array.begin(), array.end());
    }, py::keep_alive<0, 1>()); /* Keep vector alive while iterator is used */

Generates:

class Array(Value):
    def __iter__(self) -> typing.Iterator: ...

Is it possible to generate somethin like the following?

class Array(Value):
    def __iter__(self) -> typing.Iterator[Value]: ...

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