mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 23:22:01 +00:00
Merge branch 'pybind:master' into master
This commit is contained in:
commit
c2ccd08d24
@ -63,6 +63,21 @@ class Callable<Return(Args...)> : public function {
|
|||||||
using function::function;
|
using function::function;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class Type : public type {
|
||||||
|
using type::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Types>
|
||||||
|
class Union : public object {
|
||||||
|
using object::object;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
class Optional : public object {
|
||||||
|
using object::object;
|
||||||
|
};
|
||||||
|
|
||||||
PYBIND11_NAMESPACE_END(typing)
|
PYBIND11_NAMESPACE_END(typing)
|
||||||
|
|
||||||
PYBIND11_NAMESPACE_BEGIN(detail)
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
||||||
@ -121,5 +136,22 @@ struct handle_type_name<typing::Callable<Return(Args...)>> {
|
|||||||
+ const_name("], ") + make_caster<retval_type>::name + const_name("]");
|
+ const_name("], ") + make_caster<retval_type>::name + const_name("]");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct handle_type_name<typing::Type<T>> {
|
||||||
|
static constexpr auto name = const_name("type[") + make_caster<T>::name + const_name("]");
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... Types>
|
||||||
|
struct handle_type_name<typing::Union<Types...>> {
|
||||||
|
static constexpr auto name = const_name("Union[")
|
||||||
|
+ ::pybind11::detail::concat(make_caster<Types>::name...)
|
||||||
|
+ const_name("]");
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct handle_type_name<typing::Optional<T>> {
|
||||||
|
static constexpr auto name = const_name("Optional[") + make_caster<T>::name + const_name("]");
|
||||||
|
};
|
||||||
|
|
||||||
PYBIND11_NAMESPACE_END(detail)
|
PYBIND11_NAMESPACE_END(detail)
|
||||||
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
||||||
|
@ -844,4 +844,27 @@ TEST_SUBMODULE(pytypes, m) {
|
|||||||
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
|
m.def("annotate_iterator_int", [](const py::typing::Iterator<int> &) {});
|
||||||
m.def("annotate_fn",
|
m.def("annotate_fn",
|
||||||
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
|
[](const py::typing::Callable<int(py::typing::List<py::str>, py::str)> &) {});
|
||||||
|
m.def("annotate_type", [](const py::typing::Type<int> &) {});
|
||||||
|
|
||||||
|
m.def("annotate_union",
|
||||||
|
[](py::typing::List<py::typing::Union<py::str, py::int_, py::object>> l,
|
||||||
|
py::str a,
|
||||||
|
py::int_ b,
|
||||||
|
py::object c) -> py::typing::List<py::typing::Union<py::str, py::int_, py::object>> {
|
||||||
|
l.append(a);
|
||||||
|
l.append(b);
|
||||||
|
l.append(c);
|
||||||
|
return l;
|
||||||
|
});
|
||||||
|
|
||||||
|
m.def("union_typing_only",
|
||||||
|
[](py::typing::List<py::typing::Union<py::str>> &l)
|
||||||
|
-> py::typing::List<py::typing::Union<py::int_>> { return l; });
|
||||||
|
|
||||||
|
m.def("annotate_optional",
|
||||||
|
[](py::list &list) -> py::typing::List<py::typing::Optional<py::str>> {
|
||||||
|
list.append(py::str("hi"));
|
||||||
|
list.append(py::none());
|
||||||
|
return list;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
@ -955,3 +955,28 @@ def test_fn_annotations(doc):
|
|||||||
doc(m.annotate_fn)
|
doc(m.annotate_fn)
|
||||||
== "annotate_fn(arg0: Callable[[list[str], str], int]) -> None"
|
== "annotate_fn(arg0: Callable[[list[str], str], int]) -> None"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_type_annotation(doc):
|
||||||
|
assert doc(m.annotate_type) == "annotate_type(arg0: type[int]) -> None"
|
||||||
|
|
||||||
|
|
||||||
|
def test_union_annotations(doc):
|
||||||
|
assert (
|
||||||
|
doc(m.annotate_union)
|
||||||
|
== "annotate_union(arg0: list[Union[str, int, object]], arg1: str, arg2: int, arg3: object) -> list[Union[str, int, object]]"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_union_typing_only(doc):
|
||||||
|
assert (
|
||||||
|
doc(m.union_typing_only)
|
||||||
|
== "union_typing_only(arg0: list[Union[str]]) -> list[Union[int]]"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_optional_annotations(doc):
|
||||||
|
assert (
|
||||||
|
doc(m.annotate_optional)
|
||||||
|
== "annotate_optional(arg0: list) -> list[Optional[str]]"
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user