pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

[BUG]: Invalid typing for `std::array`?

Open Holt59 opened this issue 3 years ago • 3 comments

Required prerequisites

  • [X] Make sure you've read the documentation. Your issue may be addressed there.
  • [X] Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
  • [X] Consider asking first in the Gitter chat room or in a Discussion.

Problem description

When returning a std::array, the typing information is invalid, e.g. List[str[2]] for the code below. This causes tool such as mypy's stubgen or pybind11-stubgen to produce invalid stubs.

I guess it would be valid to generate List[str] in this case since Python does not have information about the size anyway?

Reproducible example code

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

namespace py = pybind11;

PYBIND11_MODULE(cpp_module, m)
{
    m.def("arr", []() -> std::array<const char*, 2> {
        return {"foo", "bar"};
    });
}

Holt59 avatar Apr 28 '22 14:04 Holt59

@sizmailov for your attention please

bricerebsamen avatar Apr 28 '22 15:04 bricerebsamen

Oddly enough, this is expected and tested behavior: https://github.com/pybind/pybind11/blob/75007dda72ad4508064c1f080394eae50d3a61ee/tests/test_stl.py#L42 . @rwgk @henryiii This doesn't seem to be a valid typing extension. Any idea why this data is included? We can use Annotated, but we need typing_extensions to backport that behavior. I guess we should remove that data or change the return type of a caster to a tuple / something that can handle it?

Skylion007 avatar Apr 29 '22 15:04 Skylion007

@henryiii Any idea how we can support typing extensions?

Skylion007 avatar May 03 '22 16:05 Skylion007