From c88f6ed0e068bbae54ea84b166963ce44293a6fc Mon Sep 17 00:00:00 2001 From: Chen Zhang Date: Thu, 3 Oct 2024 18:16:40 -0400 Subject: [PATCH] Merge pull request #38144 from mantidproject/patch-add-direction-to-propwithval Add optional direction to `PropertyWithValue` binding --- .../core/PropertyWithValueExporter.h | 3 +- .../mantid/kernel/PropertyWithValueTest.py | 33 +++++++++++++++++++ .../Framework/Python/New_features/38144.rst | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 docs/source/release/v6.12.0/Framework/Python/New_features/38144.rst diff --git a/Framework/PythonInterface/core/inc/MantidPythonInterface/core/PropertyWithValueExporter.h b/Framework/PythonInterface/core/inc/MantidPythonInterface/core/PropertyWithValueExporter.h index 60594e269025..59f48596c61d 100644 --- a/Framework/PythonInterface/core/inc/MantidPythonInterface/core/PropertyWithValueExporter.h +++ b/Framework/PythonInterface/core/inc/MantidPythonInterface/core/PropertyWithValueExporter.h @@ -43,7 +43,8 @@ struct PropertyWithValueExporter { using namespace Mantid::Kernel; class_, bases, boost::noncopyable>( - pythonClassName, init((arg("self"), arg("name"), arg("value")))) + pythonClassName, init( + (arg("self"), arg("name"), arg("value"), arg("direction") = Direction::Input))) .add_property("value", make_function(&PropertyWithValue::operator(), return_value_policy())) .def("dtype", &dtype, arg("self")); diff --git a/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py b/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py index 3badfe438b0e..04adc6a468dd 100644 --- a/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py +++ b/Framework/PythonInterface/test/python/mantid/kernel/PropertyWithValueTest.py @@ -201,6 +201,39 @@ def test_set_property_of_vector_int_succeeds_with_numpy_array_of_int_type(self): def test_set_property_of_vector_int_succeeds_with_numpy_array_of_int_type(self): self._do_vector_int_numpy_test("WorkspaceIndexList") + def test_property_as_output(self): + """ + Test that PropertyWithValue can be declared as an output property of an algorithm, + and that it will be returned as the output when called using the python API function approach + """ + from mantid.api import AlgorithmFactory, PythonAlgorithm + from mantid.kernel import Direction, IntPropertyWithValue, ULongLongPropertyWithValue + from mantid.simpleapi import _create_algorithm_function + # a large number than cannot fit in a C++ int, requiring u_int64 + BIGINT: int = 125824461545280 + # create an algorithm that returns a ULongLongPropertyWithValue as the output + # then register it so that it can be called as a function + class MyAlgorithm(PythonAlgorithm): + def PyInit(self): + self.declareProperty(IntPropertyWithValue("InputInt", 0)) + self.declareProperty(ULongLongPropertyWithValue("MyProperty", 0, direction=Direction.Output)) + def PyExec(self): + self.setProperty("MyProperty", BIGINT) + AlgorithmFactory.subscribe(MyAlgorithm) + algo = MyAlgorithm() + algo.initialize() + myAlgorithmName = "MyAlgorithm" + _create_algorithm_function(myAlgorithmName, 1, algo) + from mantid.simpleapi import MyAlgorithm as MyAlgorithmInMantid + # call the algorithm, ensure the output is the large integer expected + ret = MyAlgorithmInMantid(1) + assert ret == BIGINT + # clean up the the algorithm manager and verify + AlgorithmFactory.unsubscribe(myAlgorithmName, 1) + with self.assertRaises(RuntimeError) as cm: + MyAlgorithmInMantid(2) + assert f"not registered {myAlgorithmName}" in str(cm.exception) + if __name__ == "__main__": unittest.main() diff --git a/docs/source/release/v6.12.0/Framework/Python/New_features/38144.rst b/docs/source/release/v6.12.0/Framework/Python/New_features/38144.rst new file mode 100644 index 000000000000..5f8f0388d2b5 --- /dev/null +++ b/docs/source/release/v6.12.0/Framework/Python/New_features/38144.rst @@ -0,0 +1 @@ +- allows for declaring the many :class:`PropertyWithValue ` types as output properties from the python API. \ No newline at end of file