Skip to content

Commit

Permalink
minimize pybind11_mypy_demo to bare minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
Keller Fabian Rudolf (CC-AD/EYC3) authored and bluenote10 committed May 12, 2022
1 parent e681915 commit 5012113
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 77 deletions.
3 changes: 2 additions & 1 deletion misc/test-stubgenc.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

set -e
set -x

cd "$(dirname $0)/.."

Expand All @@ -10,7 +11,7 @@ python -m pip install ./test-data/pybind11_mypy_demo
python -m pip install .

# Remove expected stubs and generate new inplace
STUBGEN_OUTPUT_FOLDER=test-data/pybind11_mypy_demo/stubgen
STUBGEN_OUTPUT_FOLDER=./test-data/pybind11_mypy_demo/stubgen
rm -rf $STUBGEN_OUTPUT_FOLDER/*
stubgen -p pybind11_mypy_demo -o $STUBGEN_OUTPUT_FOLDER

Expand Down
36 changes: 0 additions & 36 deletions test-data/pybind11_mypy_demo/LICENSE

This file was deleted.

2 changes: 1 addition & 1 deletion test-data/pybind11_mypy_demo/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [
"setuptools>=42",
"wheel",
# Officially supported pybind11 version. This is pinned to guarantee 100% reproducible CI.
# As a results, needs to be updated deliberately at will.
# As a result, the version needs to be bumped manually at will.
"pybind11==2.9.2",
]

Expand Down
34 changes: 5 additions & 29 deletions test-data/pybind11_mypy_demo/setup.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,18 @@
import sys

from pybind11 import get_cmake_dir
# Available at setup time due to pyproject.toml
from pybind11.setup_helpers import Pybind11Extension, build_ext
# pybind11 is available at setup time due to pyproject.toml
from pybind11.setup_helpers import Pybind11Extension
from setuptools import setup

__version__ = "0.0.1"

# The main interface is through Pybind11Extension.
# * You can add cxx_std=11/14/17, and then build_ext can be removed.
# * You can set include_pybind11=false to add the include directory yourself,
# say from a submodule.
#
# Note:
# Sort input source files if you glob sources to ensure bit-for-bit
# reproducible builds (https://github.com/pybind/python_example/pull/53)

# Documentation: https://pybind11.readthedocs.io/en/stable/compiling.html
ext_modules = [
Pybind11Extension(
"pybind11_mypy_demo",
["src/main.cpp"],
# Example: passing in the version to the compiled code
define_macros=[('VERSION_INFO', __version__)],
cxx_std=17,
),
]

setup(
name="pybind11-mypy-demo",
version=__version__,
author="Sergei Izmailov",
author_email="sergei.a.izmailov@gmail.com", # Subject to change
url="https://github.com/sizmailov/pybind11-mypy-demo", # Subject to change
description="A demo project using pybind11 to test mypy stubgen",
long_description="",
version="0.0.1",
ext_modules=ext_modules,
extras_require={"test": "pytest"},
# Currently, build_ext only provides an optional "highest supported C++
# level" feature, but in the future it may provide more features.
cmdclass={"build_ext": build_ext},
zip_safe=False,
)
58 changes: 48 additions & 10 deletions test-data/pybind11_mypy_demo/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
/**
* This file contains the pybind11 reference implementation for the stugen tests,
* and was originally inspired by:
*
* https://github.com/sizmailov/pybind11-mypy-demo
*
* Copyright (c) 2016 The Pybind Development Team, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* You are under no obligation whatsoever to provide any bug fixes, patches, or
* upgrades to the features, functionality or performance of the source code
* ("Enhancements") to anyone; however, if you choose to make your Enhancements
* available either publicly, or directly to the author of this software, without
* imposing a separate written license agreement for such Enhancements, then you
* hereby grant the following license: a non-exclusive, royalty-free perpetual
* license to install, use, modify, prepare derivative works, incorporate into
* other computer software, distribute, and sublicense such enhancements or
* derivative works thereof, in binary and source code form.
*/

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

Expand Down Expand Up @@ -68,7 +112,7 @@ const Point Point::y_axis = Point(0, 1);
Point::LengthUnit Point::length_unit = Point::LengthUnit::mm;
Point::AngleUnit Point::angle_unit = Point::AngleUnit::radian;

}
} // namespace: basics

void bind_basics(py::module& basics) {

Expand All @@ -80,7 +124,6 @@ void bind_basics(py::module& basics) {
basics.def("midpoint", &midpoint, py::arg("left"), py::arg("right"));
basics.def("weighted_midpoint", weighted_midpoint, py::arg("left"), py::arg("right"), py::arg("alpha")=0.5);


// Classes
py::class_<Point> pyPoint(basics, "Point");
py::enum_<Point::LengthUnit> pyLengthUnit(pyPoint, "LengthUnit");
Expand All @@ -103,30 +146,25 @@ void bind_basics(py::module& basics) {
.def_property_static("angle_unit",
[](py::object& /*cls*/){ return Point::angle_unit; },
[](py::object& /*cls*/, Point::AngleUnit value){ Point::angle_unit = value; }
)
;
);

pyPoint.attr("origin") = Point::origin;

pyLengthUnit
.value("mm", Point::LengthUnit::mm)
.value("pixel", Point::LengthUnit::pixel)
.value("inch", Point::LengthUnit::inch)
;
.value("inch", Point::LengthUnit::inch);

pyAngleUnit
.value("radian", Point::AngleUnit::radian)
.value("degree", Point::AngleUnit::degree)
;
.value("degree", Point::AngleUnit::degree);

// Module-level attributes
basics.attr("PI") = std::acos(-1);
basics.attr("__version__") = "0.0.1";
}

PYBIND11_MODULE(pybind11_mypy_demo, m) {

auto basics = m.def_submodule("basics");
bind_basics(basics);

}

0 comments on commit 5012113

Please sign in to comment.