Skip to content

Commit

Permalink
Test agains older pybind11 (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
sizmailov authored Oct 23, 2023
1 parent 2f969a1 commit 91adf2e
Show file tree
Hide file tree
Showing 48 changed files with 990 additions and 39 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ jobs:
strategy:
fail-fast: false
matrix:
pybind11-branch: [ "master" ]
pybind11-branch:
- "master"
python:
- "3.12"
- "3.11"
- "3.10"
- "3.9"
- "3.8"
- "3.7"
include:
- pybind11-branch: "v2.9"
python: "3.12"
steps:
- uses: actions/checkout@v3

Expand All @@ -74,7 +78,7 @@ jobs:

- name: Install demo module
shell: bash
run: ./tests/install-demo-module.sh --pybind11-branch "${{ matrix.pybind11-branch }}"
run: ./tests/install-demo-module.sh --pybind11-branch "${{ matrix.pybind11-branch }}" --eigen-branch "master"

- name: Check stubs generation
shell: bash
Expand Down
1 change: 1 addition & 0 deletions pybind11_stubgen/parser/mixins/fix.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ class FixTypingTypeNames(IParser):
map(
Identifier,
[
"buffer",
"Buffer",
],
)
Expand Down
16 changes: 12 additions & 4 deletions tests/install-demo-module.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -e

PYTHON_EXECUTABLE=$(python -c 'import sys; print(sys.executable)')

function parse_args() {

CLEAR='\033[0m'
Expand All @@ -13,17 +15,20 @@ function parse_args() {
fi
echo "Usage: $0 --pybind11-branch PYBIND11_BRANCH"
echo " --pybind11-branch name of pybind11 branch"
echo " --eigen-branch name of eigen branch"
exit 1
}

# parse params
while [[ "$#" > 0 ]]; do case $1 in
--pybind11-branch) PYBIND11_BRANCH="$2"; shift;shift;;
--eigen-branch) EIGEN_BRANCH="$2"; shift;shift;;
*) usage "Unknown parameter passed: $1"; shift; shift;;
esac; done

# verify params
if [ -z "$PYBIND11_BRANCH" ]; then usage "PYBIND11_BRANCH is not set"; fi;
if [ -z "$EIGEN_BRANCH" ]; then usage "EIGEN_BRANCH is not set"; fi;

TESTS_ROOT="$(readlink -m "$(dirname "$0")")"
PROJECT_ROOT="${TESTS_ROOT}/.."
Expand All @@ -39,7 +44,7 @@ clone_eigen() {
if [ ! -d "${EXTERNAL_DIR}/eigen" ]; then
git clone \
--depth 1 \
--branch master \
--branch "${EIGEN_BRANCH}" \
--single-branch \
https://gitlab.com/libeigen/eigen.git \
"${EXTERNAL_DIR}/eigen"
Expand All @@ -65,7 +70,10 @@ install_eigen() {
}

install_pybind11() {
cmake -S "${EXTERNAL_DIR}/pybind11" -B "${BUILD_ROOT}/pybind11"
cmake \
-S "${EXTERNAL_DIR}/pybind11" \
-B "${BUILD_ROOT}/pybind11"\
-DPYTHON_EXECUTABLE=${PYTHON_EXECUTABLE}
cmake --install "${BUILD_ROOT}/pybind11" \
--prefix "${INSTALL_PREFIX}"
}
Expand All @@ -80,8 +88,8 @@ install_demo() {
install_pydemo() {
(
export CMAKE_PREFIX_PATH="$(readlink -m "${INSTALL_PREFIX}")";
export CMAKE_ARGS="CMAKE_CXX_STANDARD=17";
pip install "${TESTS_ROOT}/py-demo"
export CMAKE_ARGS="-DCMAKE_CXX_STANDARD=17";
pip install --force-reinstall "${TESTS_ROOT}/py-demo"
)
}

Expand Down
30 changes: 16 additions & 14 deletions tests/py-demo/bindings/src/modules.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
#include <pybind11/pybind11.h>
#include "opaque_types.h"

#define PYBIND11_VERSION_AT_LEAST(x,y) (PYBIND11_VERSION_MAJOR>x || ( PYBIND11_VERSION_MAJOR>=x && PYBIND11_VERSION_MINOR>=y ))

namespace py = pybind11;

void bind_aliases_module(py::module_&& m);
void bind_classes_module(py::module_&& m);
void bind_eigen_module(py::module_&& m);
void bind_enum_module(py::module_&& m);
void bind_flawed_bindings_module(py::module_&& m);
void bind_functions_module(py::module_&& m);
void bind_issues_module(py::module_&& m);
void bind_methods_module(py::module_&& m);
void bind_numpy_module(py::module_&& m);
void bind_properties_module(py::module_&& m);
void bind_stl_module(py::module_&& m);
void bind_stl_bind_module(py::module_&& m);
void bind_typing_module(py::module_&& m);
void bind_values_module(py::module_&& m);
void bind_aliases_module(py::module&& m);
void bind_classes_module(py::module&& m);
void bind_eigen_module(py::module&& m);
void bind_enum_module(py::module&& m);
void bind_flawed_bindings_module(py::module&& m);
void bind_functions_module(py::module&& m);
void bind_issues_module(py::module&& m);
void bind_methods_module(py::module&& m);
void bind_numpy_module(py::module&& m);
void bind_properties_module(py::module&& m);
void bind_stl_module(py::module&& m);
void bind_stl_bind_module(py::module&& m);
void bind_typing_module(py::module&& m);
void bind_values_module(py::module&& m);
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/aliases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace {
};
} // namespace

void bind_aliases_module(py::module_ &&m) {
void bind_aliases_module(py::module &&m) {
{
// python module as value
auto &&pyDummy = py::class_<Dummy>(m, "Dummy");
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <demo/Inheritance.h>
#include <demo/NestedClasses.h>

void bind_classes_module(py::module_&&m) {
void bind_classes_module(py::module&&m) {

{
auto pyOuter = py::class_<demo::Outer>(m, "Outer");
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/eigen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <Eigen/Core>
#include <pybind11/eigen.h>

void bind_eigen_module(py::module_ &&m) {
void bind_eigen_module(py::module &&m) {

using DenseMatrixR = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
using DenseMatrixC = Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>;
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <demo/sublibA/ConsoleColors.h>

void bind_enum_module(py::module_&&m) {
void bind_enum_module(py::module&&m) {

py::enum_<demo::sublibA::ConsoleForegroundColor>(m, "ConsoleForegroundColor")
.value("Green", demo::sublibA::ConsoleForegroundColor::Green)
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/flawed_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum Enum { ONE = 1, TWO = 2 };

} // namespace

void bind_flawed_bindings_module(py::module_&& m) {
void bind_flawed_bindings_module(py::module&& m) {
// This submodule will have C++ signatures in python docstrings to emulate
// common mistakes in using pybind11
m.def("get_unbound_type", [] { return Unbound{}; });
Expand Down
16 changes: 12 additions & 4 deletions tests/py-demo/bindings/src/modules/functions.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "modules.h"

#if PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR >= 11
#include <pybind11/typing.h>
#include <pybind11/stl.h>
#endif

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

#include <demo/sublibA/add.h>

Expand All @@ -15,7 +18,7 @@ namespace {
};
}; // namespace

void bind_functions_module(py::module_ &&m) {
void bind_functions_module(py::module &&m) {
m.def("add", demo::sublibA::add);
{

Expand All @@ -35,6 +38,7 @@ void bind_functions_module(py::module_ &&m) {

m.def("func_w_anon_args", [](int x, int y, int z) {});

#if PYBIND11_VERSION_AT_LEAST(2, 6)
m.def(
"func_w_named_pos_args",
[](int x, int y, int z) {},
Expand All @@ -61,15 +65,19 @@ void bind_functions_module(py::module_ &&m) {
py::arg("j"),
py::kw_only(),
py::arg("k"));

#endif
m.def("accept_callable", [](py::function &callable) { return callable(); });
m.def("accept_py_object", [](py::object &object) { return py::str(object); });
m.def("accept_py_handle", [](py::handle &handle) { return py::str(handle); });

#if PYBIND11_VERSION_AT_LEAST(2, 12)
m.def("accept_annotated_callable",
[](py::typing::Callable<int(int, int)> &callable) { return callable(13, 42); });
#endif

#if PYBIND11_VERSION_AT_LEAST(2, 10)
m.def("accept_frozenset", [](const py::frozenset &) {});
#endif
m.def("accept_set", [](const py::set &) {});

m.def("default_int_arg", [](int n) {}, py::arg("n") = 5);
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/issues.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "modules.h"

void bind_issues_module(py::module_ &&m) {
void bind_issues_module(py::module &&m) {
{
// https://github.com/sizmailov/pybind11-stubgen/issues/51
m.def(
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct Dummy {

} // namespace

void bind_methods_module(py::module_&& m) {
void bind_methods_module(py::module&& m) {
auto &&pyDummy = py::class_<Dummy>(m, "Dummy");

pyDummy.def_static("def_static", &Dummy::static_method);
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/numpy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "modules.h"

void bind_numpy_module(py::module_&&m) {
void bind_numpy_module(py::module&&m) {
{
m.def("get_ndarray_int", [] { return py::array_t<int>{}; });
m.def("get_ndarray_float64", [] { return py::array_t<double>{}; });
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int WithPropDoc::static_value = 0;
int WithGetterSetterDoc::static_value = 0;
int WithPropAndGetterSetterDoc::static_value = 0;

void bind_properties_module(py::module_ &&m) {
void bind_properties_module(py::module &&m) {
{
auto &&pyDummy = py::class_<WithoutDoc>(m, "WithoutDoc", "No user docstring provided");

Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/stl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <map>
#include <variant>

void bind_stl_module(py::module_ &&m) {
void bind_stl_module(py::module &&m) {
m.def("std_map", [] { return std::map<int, std::complex<double>>{}; });
m.def("std_vector", [] { return std::vector<std::pair<int, double>>{}; });
m.def("std_array", [](const std::array<int, 3> &a) { return a; });
Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/stl_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <complex>
#include <map>

void bind_stl_bind_module(py::module_&&m) {
void bind_stl_bind_module(py::module&&m) {
py::bind_vector<std::vector<std::pair<std::string, double>>>(m, "VectorPairStringDouble");
py::bind_map<std::map<std::string, std::complex<double>>>(m, "MapStringComplex");

Expand Down
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/typing.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "modules.h"

void bind_typing_module(py::module_ &&m) {
void bind_typing_module(py::module &&m) {
m.def("get_sequence", [](py::sequence &seq) { return seq; });
m.def("get_buffer", [](py::buffer &buffer) { return buffer; });
}
2 changes: 1 addition & 1 deletion tests/py-demo/bindings/src/modules/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Dummy {};
class Foo {};
} // namespace

void bind_values_module(py::module_ &&m) {
void bind_values_module(py::module &&m) {
{
// python module as value
auto &&pyDummy = py::class_<Dummy>(m, "Dummy");
Expand Down
6 changes: 4 additions & 2 deletions tests/py-demo/bindings/src/opaque_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
#include <map>
#include <vector>

PYBIND11_MAKE_OPAQUE(std::map<std::string, std::complex<double>>);
PYBIND11_MAKE_OPAQUE(std::vector<std::pair<std::string, double>>);
using MapStringComplex = std::map<std::string, std::complex<double>>;
using VectorPairStringDouble = std::vector<std::pair<std::string, double>>;
PYBIND11_MAKE_OPAQUE(MapStringComplex);
PYBIND11_MAKE_OPAQUE(VectorPairStringDouble);
41 changes: 41 additions & 0 deletions tests/stubs/python-3.12/pybind11-v2.9/demo/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from __future__ import annotations

from demo._bindings import (
aliases,
classes,
eigen,
enum,
flawed_bindings,
functions,
issues,
methods,
numpy,
properties,
stl,
stl_bind,
typing,
values,
)

from . import _bindings, core, pure_python

__all__ = [
"aliases",
"classes",
"core",
"eigen",
"enum",
"flawed_bindings",
"functions",
"issues",
"methods",
"numpy",
"properties",
"pure_python",
"stl",
"stl_bind",
"typing",
"values",
"version",
]
version: str = "0.0.0"
35 changes: 35 additions & 0 deletions tests/stubs/python-3.12/pybind11-v2.9/demo/_bindings/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations

from . import (
aliases,
classes,
eigen,
enum,
flawed_bindings,
functions,
issues,
methods,
numpy,
properties,
stl,
stl_bind,
typing,
values,
)

__all__ = [
"aliases",
"classes",
"eigen",
"enum",
"flawed_bindings",
"functions",
"issues",
"methods",
"numpy",
"properties",
"stl",
"stl_bind",
"typing",
"values",
]
Loading

0 comments on commit 91adf2e

Please sign in to comment.