Skip to content

Commit ecc6b2e

Browse files
committed
Add pybind11 tests for insert typing.List
1 parent 827361f commit ecc6b2e

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

mypy/stubgenc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,22 @@ def __init__(
239239
self.resort_members = self.is_c_module
240240
super().__init__(_all_, include_private, export_less, include_docstrings)
241241
self.module_name = module_name
242+
self.known_imports.update(
243+
{
244+
"typing": [
245+
"Any",
246+
"Callable",
247+
"ClassVar",
248+
"Dict",
249+
"Iterable",
250+
"Iterator",
251+
"List",
252+
"Optional",
253+
"Tuple",
254+
"Union",
255+
]
256+
}
257+
)
242258

243259
def get_default_function_sig(self, func: object, ctx: FunctionContext) -> FunctionSig:
244260
argspec = None

mypy/stubutil.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,14 @@ def __init__(
576576
self.sig_generators = self.get_sig_generators()
577577
# populated by visit_mypy_file
578578
self.module_name: str = ""
579+
# These are "soft" imports for objects which might appear in annotations but not have
580+
# a corresponding import statement.
581+
self.known_imports = {
582+
"_typeshed": ["Incomplete"],
583+
"typing": ["Any", "TypeVar", "NamedTuple"],
584+
"collections.abc": ["Generator"],
585+
"typing_extensions": ["TypedDict", "ParamSpec", "TypeVarTuple"],
586+
}
579587

580588
def get_sig_generators(self) -> list[SignatureGenerator]:
581589
return []
@@ -667,15 +675,7 @@ def set_defined_names(self, defined_names: set[str]) -> None:
667675
for name in self._all_ or ():
668676
self.import_tracker.reexport(name)
669677

670-
# These are "soft" imports for objects which might appear in annotations but not have
671-
# a corresponding import statement.
672-
known_imports = {
673-
"_typeshed": ["Incomplete"],
674-
"typing": ["Any", "TypeVar", "NamedTuple"],
675-
"collections.abc": ["Generator"],
676-
"typing_extensions": ["TypedDict", "ParamSpec", "TypeVarTuple"],
677-
}
678-
for pkg, imports in known_imports.items():
678+
for pkg, imports in self.known_imports.items():
679679
for t in imports:
680680
# require=False means that the import won't be added unless require_name() is called
681681
# for the object during generation.

test-data/pybind11_mypy_demo/src/main.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
#include <cmath>
4646
#include <pybind11/pybind11.h>
47+
#include <pybind11/stl.h>
4748

4849
namespace py = pybind11;
4950

@@ -102,6 +103,11 @@ struct Point {
102103
return distance_to(other.x, other.y);
103104
}
104105

106+
std::vector<double> as_vector()
107+
{
108+
return std::vector<double>{x, y};
109+
}
110+
105111
double x, y;
106112
};
107113

@@ -134,6 +140,7 @@ void bind_basics(py::module& basics) {
134140
.def(py::init<double, double>(), py::arg("x"), py::arg("y"))
135141
.def("distance_to", py::overload_cast<double, double>(&Point::distance_to, py::const_), py::arg("x"), py::arg("y"))
136142
.def("distance_to", py::overload_cast<const Point&>(&Point::distance_to, py::const_), py::arg("other"))
143+
.def("as_vector", &Point::as_vector)
137144
// Note that the trailing newline is required because the generated docstring
138145
// is concatenated to the signature, e.g.:
139146
// 'some docstring\n(self: pybind11_mypy_demo.basics.Point) -> float\n'

test-data/pybind11_mypy_demo/stubgen-include-docs/pybind11_mypy_demo/basics.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar, overload
1+
from typing import ClassVar, List, overload
22

33
PI: float
44
__version__: str
@@ -73,6 +73,8 @@ class Point:
7373
7474
2. __init__(self: pybind11_mypy_demo.basics.Point, x: float, y: float) -> None
7575
"""
76+
def as_vector(self) -> List[float]:
77+
"""as_vector(self: pybind11_mypy_demo.basics.Point) -> List[float]"""
7678
@overload
7779
def distance_to(self, x: float, y: float) -> float:
7880
"""distance_to(*args, **kwargs)

test-data/pybind11_mypy_demo/stubgen/pybind11_mypy_demo/basics.pyi

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import ClassVar, overload
1+
from typing import ClassVar, List, overload
22

33
PI: float
44
__version__: str
@@ -47,6 +47,7 @@ class Point:
4747
def __init__(self) -> None: ...
4848
@overload
4949
def __init__(self, x: float, y: float) -> None: ...
50+
def as_vector(self) -> List[float]: ...
5051
@overload
5152
def distance_to(self, x: float, y: float) -> float: ...
5253
@overload

0 commit comments

Comments
 (0)