Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
save
  • Loading branch information
crcrpar committed Jun 28, 2022
commit 1bbc4fb82eb301208f8de36259b9bec0b8599cea
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"python.linting.flake8Enabled": true,
"python.linting.mypyEnabled": false,
"python.linting.pydocstyleEnabled": false,
"python.linting.enabled": true
}
34 changes: 28 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,31 @@ struct ReduceOp {
UNUSED,
};

ReduceOp() = delete;
// ReduceOp() = delete;
ReduceOp() = default;
ReduceOp(Kind op) : op{op} {}

Kind op;
Kind op = SUM;
};

void func(const ReduceOp a, const ReduceOp b) {
return;
}

void func(const ReduceOp::Kind a, const ReduceOp::Kind b) { return; }
void func(const ReduceOp::Kind a, const ReduceOp::Kind b) {
func(ReduceOp(a), ReduceOp(b));
}

struct Options {
Options() {}
Options(ReduceOp reduce_op) : reduce_op{reduce_op} {}
Options(ReduceOp::Kind reduce_op_kind) : reduce_op(reduce_op_kind) {}
ReduceOp reduce_op;
};

void func(Options a, Options b) {
return;
}

#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
Expand All @@ -56,16 +70,17 @@ PYBIND11_MODULE(python_example, m) {
m.def("func", py::overload_cast<const ReduceOp::Kind, const ReduceOp::Kind>(&func), R"pbdoc(
Takes two ReduceOp
)pbdoc");
m.def("func", py::overload_cast<Options, Options>(&func), R"pbdoc(Takes two Options)pbdoc");

// m.def("func", static_cast<void ()(const ReduceOp::Kind, const ReduceOp::Kind)>(&func), R"pbdoc(
// Takes two ReduceOp
// )pbdoc");

py::class_<ReduceOp> reduce_op(m, "ReduceOp");

reduce_op.def(
py::init<ReduceOp::Kind>()
).def_readonly("op", &ReduceOp::op);
reduce_op
.def(py::init<ReduceOp::Kind>())
.def_readwrite("op", &ReduceOp::op);

py::enum_<ReduceOp::Kind>(reduce_op, "Kind")
.value("SUM", ReduceOp::SUM)
Expand All @@ -78,6 +93,13 @@ PYBIND11_MODULE(python_example, m) {
.value("UNUSED", ReduceOp::UNUSED)
.export_values();

py::class_<Options> options(m, "Options");
options
.def(py::init<>())
.def(py::init<ReduceOp>())
.def(py::init<ReduceOp::Kind>())
.def_readwrite("reduce_op", &Options::reduce_op);

#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
Expand Down
25 changes: 25 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@ def test_func(self):
def test_func_auto_call_ctor(self):
m.func(m.ReduceOp.SUM, m.ReduceOp.UNUSED)

def test_options_op_assignment_from_internal_enum(self):
# ======================================================================
# ERROR: test_options_op_assignment_from_internal_enum (__main__.SimpleTest)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/masaki/ghq/github.com/crcrpar/python_example/tests/test.py", line 16, in test_options_op_assignment_from_internal_enum
# opt_a.reduce_op = m.ReduceOp.SUM
# TypeError: (): incompatible function arguments. The following argument types are supported:
# 1. (self: python_example.Options, arg0: python_example.ReduceOp) -> None

# Invoked with: <python_example.Options object at 0x7f4fe49adcf0>, <Kind.SUM: 0>

# ----------------------------------------------------------------------
# Ran 3 tests in 0.000s

# FAILED (errors=1)
opt_a = m.Options()
opt_a.reduce_op = m.ReduceOp.SUM
m.func(opt_a, opt_a)

def test_options_op_assignment_from_internal_enum_WAR(self):
opt_a = m.Options()
opt_a.reduce_op = m.ReduceOp(m.ReduceOp.SUM)
m.func(opt_a, opt_a)


if __name__ == "__main__":
unittest.main()