Merge branch 'pybind:master' into master

This commit is contained in:
Steve R. Sun 2023-09-13 08:17:51 +08:00 committed by GitHub
commit ae96fd6526
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 81 additions and 22 deletions

View File

@ -661,7 +661,7 @@ public:
} }
static constexpr auto name static constexpr auto name
= const_name("Tuple[") + concat(make_caster<Ts>::name...) + const_name("]"); = const_name("tuple[") + concat(make_caster<Ts>::name...) + const_name("]");
template <typename T> template <typename T>
using cast_op_type = type; using cast_op_type = type;
@ -882,6 +882,10 @@ struct handle_type_name<bytes> {
static constexpr auto name = const_name(PYBIND11_BYTES_NAME); static constexpr auto name = const_name(PYBIND11_BYTES_NAME);
}; };
template <> template <>
struct handle_type_name<buffer> {
static constexpr auto name = const_name("Buffer");
};
template <>
struct handle_type_name<int_> { struct handle_type_name<int_> {
static constexpr auto name = const_name("int"); static constexpr auto name = const_name("int");
}; };
@ -902,10 +906,18 @@ struct handle_type_name<function> {
static constexpr auto name = const_name("Callable"); static constexpr auto name = const_name("Callable");
}; };
template <> template <>
struct handle_type_name<handle> {
static constexpr auto name = handle_type_name<object>::name;
};
template <>
struct handle_type_name<none> { struct handle_type_name<none> {
static constexpr auto name = const_name("None"); static constexpr auto name = const_name("None");
}; };
template <> template <>
struct handle_type_name<sequence> {
static constexpr auto name = const_name("Sequence");
};
template <>
struct handle_type_name<args> { struct handle_type_name<args> {
static constexpr auto name = const_name("*args"); static constexpr auto name = const_name("*args");
}; };

View File

@ -100,7 +100,7 @@ public:
return s.release(); return s.release();
} }
PYBIND11_TYPE_CASTER(type, const_name("Set[") + key_conv::name + const_name("]")); PYBIND11_TYPE_CASTER(type, const_name("set[") + key_conv::name + const_name("]"));
}; };
template <typename Type, typename Key, typename Value> template <typename Type, typename Key, typename Value>
@ -157,7 +157,7 @@ public:
} }
PYBIND11_TYPE_CASTER(Type, PYBIND11_TYPE_CASTER(Type,
const_name("Dict[") + key_conv::name + const_name(", ") + value_conv::name const_name("dict[") + key_conv::name + const_name(", ") + value_conv::name
+ const_name("]")); + const_name("]"));
}; };
@ -208,7 +208,7 @@ public:
return l.release(); return l.release();
} }
PYBIND11_TYPE_CASTER(Type, const_name("List[") + value_conv::name + const_name("]")); PYBIND11_TYPE_CASTER(Type, const_name("list[") + value_conv::name + const_name("]"));
}; };
template <typename Type, typename Alloc> template <typename Type, typename Alloc>
@ -274,7 +274,7 @@ public:
PYBIND11_TYPE_CASTER(ArrayType, PYBIND11_TYPE_CASTER(ArrayType,
const_name<Resizable>(const_name(""), const_name("Annotated[")) const_name<Resizable>(const_name(""), const_name("Annotated["))
+ const_name("List[") + value_conv::name + const_name("]") + const_name("list[") + value_conv::name + const_name("]")
+ const_name<Resizable>(const_name(""), + const_name<Resizable>(const_name(""),
const_name(", FixedSize(") const_name(", FixedSize(")
+ const_name<Size>() + const_name(")]"))); + const_name<Size>() + const_name(")]")));

View File

@ -45,6 +45,16 @@ class Set : public set {
using set::set; using set::set;
}; };
template <typename T>
class Iterable : public iterable {
using iterable::iterable;
};
template <typename T>
class Iterator : public iterator {
using iterator::iterator;
};
template <typename Signature> template <typename Signature>
class Callable; class Callable;
@ -85,6 +95,16 @@ struct handle_type_name<typing::Set<T>> {
static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]"); static constexpr auto name = const_name("set[") + make_caster<T>::name + const_name("]");
}; };
template <typename T>
struct handle_type_name<typing::Iterable<T>> {
static constexpr auto name = const_name("Iterable[") + make_caster<T>::name + const_name("]");
};
template <typename T>
struct handle_type_name<typing::Iterator<T>> {
static constexpr auto name = const_name("Iterator[") + make_caster<T>::name + const_name("]");
};
template <typename Return, typename... Args> template <typename Return, typename... Args>
struct handle_type_name<typing::Callable<Return(Args...)>> { struct handle_type_name<typing::Callable<Return(Args...)>> {
using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>; using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>;

View File

@ -219,3 +219,10 @@ def test_ctypes_from_buffer():
assert cinfo.shape == pyinfo.shape assert cinfo.shape == pyinfo.shape
assert cinfo.strides == pyinfo.strides assert cinfo.strides == pyinfo.strides
assert not cinfo.readonly assert not cinfo.readonly
def test_buffer_docstring():
assert (
m.get_buffer_info.__doc__.strip()
== "get_buffer_info(arg0: Buffer) -> pybind11_tests.buffers.buffer_info"
)

View File

@ -352,7 +352,7 @@ def test_tuple(doc):
assert ( assert (
doc(m.pair_passthrough) doc(m.pair_passthrough)
== """ == """
pair_passthrough(arg0: Tuple[bool, str]) -> Tuple[str, bool] pair_passthrough(arg0: tuple[bool, str]) -> tuple[str, bool]
Return a pair in reversed order Return a pair in reversed order
""" """
@ -360,7 +360,7 @@ def test_tuple(doc):
assert ( assert (
doc(m.tuple_passthrough) doc(m.tuple_passthrough)
== """ == """
tuple_passthrough(arg0: Tuple[bool, str, int]) -> Tuple[int, str, bool] tuple_passthrough(arg0: tuple[bool, str, int]) -> tuple[int, str, bool]
Return a triple in reversed order Return a triple in reversed order
""" """

View File

@ -77,7 +77,7 @@ def test_init_factory_signature(msg):
1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int) 1. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.unique_ptr_tag, arg1: int)
2. m.factory_constructors.TestFactory1(arg0: str) 2. m.factory_constructors.TestFactory1(arg0: str)
3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag) 3. m.factory_constructors.TestFactory1(arg0: m.factory_constructors.tag.pointer_tag)
4. m.factory_constructors.TestFactory1(arg0: handle, arg1: int, arg2: handle) 4. m.factory_constructors.TestFactory1(arg0: object, arg1: int, arg2: object)
Invoked with: 'invalid', 'constructor', 'arguments' Invoked with: 'invalid', 'constructor', 'arguments'
""" """
@ -95,7 +95,7 @@ def test_init_factory_signature(msg):
3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None 3. __init__(self: m.factory_constructors.TestFactory1, arg0: m.factory_constructors.tag.pointer_tag) -> None
4. __init__(self: m.factory_constructors.TestFactory1, arg0: handle, arg1: int, arg2: handle) -> None 4. __init__(self: m.factory_constructors.TestFactory1, arg0: object, arg1: int, arg2: object) -> None
""" """
) )

View File

@ -8,7 +8,7 @@ def test_function_signatures(doc):
assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str" assert doc(m.kw_func1) == "kw_func1(x: int, y: int) -> str"
assert doc(m.kw_func2) == "kw_func2(x: int = 100, y: int = 200) -> str" assert doc(m.kw_func2) == "kw_func2(x: int = 100, y: int = 200) -> str"
assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None" assert doc(m.kw_func3) == "kw_func3(data: str = 'Hello world!') -> None"
assert doc(m.kw_func4) == "kw_func4(myList: List[int] = [13, 17]) -> str" assert doc(m.kw_func4) == "kw_func4(myList: list[int] = [13, 17]) -> str"
assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str" assert doc(m.kw_func_udl) == "kw_func_udl(x: int, y: int = 300) -> str"
assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str" assert doc(m.kw_func_udl_z) == "kw_func_udl_z(x: int, y: int = 0) -> str"
assert doc(m.args_function) == "args_function(*args) -> tuple" assert doc(m.args_function) == "args_function(*args) -> tuple"

View File

@ -828,6 +828,8 @@ TEST_SUBMODULE(pytypes, m) {
m.def("annotate_dict_str_int", [](const py::typing::Dict<py::str, int> &) {}); m.def("annotate_dict_str_int", [](const py::typing::Dict<py::str, int> &) {});
m.def("annotate_list_int", [](const py::typing::List<int> &) {}); m.def("annotate_list_int", [](const py::typing::List<int> &) {});
m.def("annotate_set_str", [](const py::typing::Set<std::string> &) {}); m.def("annotate_set_str", [](const py::typing::Set<std::string> &) {});
m.def("annotate_iterable_str", [](const py::typing::Iterable<std::string> &) {});
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)> &) {});
} }

View File

@ -926,6 +926,20 @@ def test_set_annotations(doc):
assert doc(m.annotate_set_str) == "annotate_set_str(arg0: set[str]) -> None" assert doc(m.annotate_set_str) == "annotate_set_str(arg0: set[str]) -> None"
def test_iterable_annotations(doc):
assert (
doc(m.annotate_iterable_str)
== "annotate_iterable_str(arg0: Iterable[str]) -> None"
)
def test_iterator_annotations(doc):
assert (
doc(m.annotate_iterator_int)
== "annotate_iterator_int(arg0: Iterator[int]) -> None"
)
def test_fn_annotations(doc): def test_fn_annotations(doc):
assert ( assert (
doc(m.annotate_fn) doc(m.annotate_fn)

View File

@ -171,6 +171,10 @@ def test_sequence_length():
assert m.sequence_length("hello") == 5 assert m.sequence_length("hello") == 5
def test_sequence_doc():
assert m.sequence_length.__doc__.strip() == "sequence_length(arg0: Sequence) -> int"
def test_map_iterator(): def test_map_iterator():
sm = m.StringMap({"hi": "bye", "black": "white"}) sm = m.StringMap({"hi": "bye", "black": "white"})
assert sm["hi"] == "bye" assert sm["hi"] == "bye"

View File

@ -16,8 +16,8 @@ def test_vector(doc):
assert m.load_bool_vector([True, False]) assert m.load_bool_vector([True, False])
assert m.load_bool_vector((True, False)) assert m.load_bool_vector((True, False))
assert doc(m.cast_vector) == "cast_vector() -> List[int]" assert doc(m.cast_vector) == "cast_vector() -> list[int]"
assert doc(m.load_vector) == "load_vector(arg0: List[int]) -> bool" assert doc(m.load_vector) == "load_vector(arg0: list[int]) -> bool"
# Test regression caused by 936: pointers to stl containers weren't castable # Test regression caused by 936: pointers to stl containers weren't castable
assert m.cast_ptr_vector() == ["lvalue", "lvalue"] assert m.cast_ptr_vector() == ["lvalue", "lvalue"]
@ -39,10 +39,10 @@ def test_array(doc):
assert m.load_array(lst) assert m.load_array(lst)
assert m.load_array(tuple(lst)) assert m.load_array(tuple(lst))
assert doc(m.cast_array) == "cast_array() -> Annotated[List[int], FixedSize(2)]" assert doc(m.cast_array) == "cast_array() -> Annotated[list[int], FixedSize(2)]"
assert ( assert (
doc(m.load_array) doc(m.load_array)
== "load_array(arg0: Annotated[List[int], FixedSize(2)]) -> bool" == "load_array(arg0: Annotated[list[int], FixedSize(2)]) -> bool"
) )
@ -53,8 +53,8 @@ def test_valarray(doc):
assert m.load_valarray(lst) assert m.load_valarray(lst)
assert m.load_valarray(tuple(lst)) assert m.load_valarray(tuple(lst))
assert doc(m.cast_valarray) == "cast_valarray() -> List[int]" assert doc(m.cast_valarray) == "cast_valarray() -> list[int]"
assert doc(m.load_valarray) == "load_valarray(arg0: List[int]) -> bool" assert doc(m.load_valarray) == "load_valarray(arg0: list[int]) -> bool"
def test_map(doc): def test_map(doc):
@ -66,8 +66,8 @@ def test_map(doc):
assert "key2" in d assert "key2" in d
assert m.load_map(d) assert m.load_map(d)
assert doc(m.cast_map) == "cast_map() -> Dict[str, str]" assert doc(m.cast_map) == "cast_map() -> dict[str, str]"
assert doc(m.load_map) == "load_map(arg0: Dict[str, str]) -> bool" assert doc(m.load_map) == "load_map(arg0: dict[str, str]) -> bool"
def test_set(doc): def test_set(doc):
@ -78,8 +78,8 @@ def test_set(doc):
assert m.load_set(s) assert m.load_set(s)
assert m.load_set(frozenset(s)) assert m.load_set(frozenset(s))
assert doc(m.cast_set) == "cast_set() -> Set[str]" assert doc(m.cast_set) == "cast_set() -> set[str]"
assert doc(m.load_set) == "load_set(arg0: Set[str]) -> bool" assert doc(m.load_set) == "load_set(arg0: set[str]) -> bool"
def test_recursive_casting(): def test_recursive_casting():
@ -303,7 +303,7 @@ def test_stl_pass_by_pointer(msg):
msg(excinfo.value) msg(excinfo.value)
== """ == """
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
1. (v: List[int] = None) -> List[int] 1. (v: list[int] = None) -> list[int]
Invoked with: Invoked with:
""" """
@ -315,7 +315,7 @@ def test_stl_pass_by_pointer(msg):
msg(excinfo.value) msg(excinfo.value)
== """ == """
stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported: stl_pass_by_pointer(): incompatible function arguments. The following argument types are supported:
1. (v: List[int] = None) -> List[int] 1. (v: list[int] = None) -> list[int]
Invoked with: None Invoked with: None
""" """