From b6d393ff7ad71e72797235d910a73cc7907be0e7 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 10 Sep 2023 17:39:28 +0200 Subject: [PATCH 1/3] Fixed issue with creating metadata. Added test that both tests creation of metadata and that it is possible to create two metadata with ref-properties that cyclic refer to each other. --- bindings/python/dlite-entity-python.i | 3 +++ bindings/python/tests/test_ref_create.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 bindings/python/tests/test_ref_create.py diff --git a/bindings/python/dlite-entity-python.i b/bindings/python/dlite-entity-python.i index 0ede8394e..f713d16b6 100644 --- a/bindings/python/dlite-entity-python.i +++ b/bindings/python/dlite-entity-python.i @@ -39,6 +39,9 @@ class Metadata(Instance): return Instance.create_metadata( uri, dimensions, properties, description) + def __init__(self, *args, **kwargs): + pass # do nothing, just avoid calling Instance.__init__() + def __repr__(self): return f"" diff --git a/bindings/python/tests/test_ref_create.py b/bindings/python/tests/test_ref_create.py new file mode 100644 index 000000000..4ae87646f --- /dev/null +++ b/bindings/python/tests/test_ref_create.py @@ -0,0 +1,24 @@ +import dlite + + +# Create two entities with ref properties that cyclic refer to each other... +prop_a = dlite.Property("a", "ref", "http://onto-ns.com/meta/0.1/A") +B = dlite.Metadata( + "http://onto-ns.com/meta/0.1/B", dimensions=[], properties=[prop_a], +) + +prop_v = dlite.Property("v", "int") +prop_b = dlite.Property("b", "ref", "http://onto-ns.com/meta/0.1/B") +A = dlite.Metadata( + "http://onto-ns.com/meta/0.1/A", dimensions=[], properties=[prop_v, prop_b], +) + +# Create instances +b = B() +a = A(properties={"v": 3, "b": b}) +b.a = a + + +assert a.v == 3 +assert a.b == b +assert b.a == a From 829305f9a31510d55daef3e2e5a5a55d6ac1f247 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 18 Oct 2023 11:39:11 +0200 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Anders Eklund <96499163+ajeklund@users.noreply.github.com> --- bindings/python/tests/test_ref_create.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bindings/python/tests/test_ref_create.py b/bindings/python/tests/test_ref_create.py index 4ae87646f..fcde12652 100644 --- a/bindings/python/tests/test_ref_create.py +++ b/bindings/python/tests/test_ref_create.py @@ -1,7 +1,7 @@ import dlite -# Create two entities with ref properties that cyclic refer to each other... +# Create two entities (with ref properties) that cyclically refer to each other. prop_a = dlite.Property("a", "ref", "http://onto-ns.com/meta/0.1/A") B = dlite.Metadata( "http://onto-ns.com/meta/0.1/B", dimensions=[], properties=[prop_a], @@ -14,11 +14,11 @@ ) # Create instances -b = B() -a = A(properties={"v": 3, "b": b}) -b.a = a +inst_b = B() +inst_a = A(properties={"v": 3, "b": inst_b}) +inst_b.a = inst_a -assert a.v == 3 -assert a.b == b -assert b.a == a +assert inst_a.v == 3 +assert inst_a.b == inst_b +assert inst_b.a == inst_a From 0bc0a774c12636a3933f54eccd4a6107d3c5fa5e Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 18 Oct 2023 14:04:00 +0200 Subject: [PATCH 3/3] Added comment suggested by reviewer --- bindings/python/dlite-entity-python.i | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bindings/python/dlite-entity-python.i b/bindings/python/dlite-entity-python.i index f713d16b6..51301a0aa 100644 --- a/bindings/python/dlite-entity-python.i +++ b/bindings/python/dlite-entity-python.i @@ -40,7 +40,12 @@ class Metadata(Instance): uri, dimensions, properties, description) def __init__(self, *args, **kwargs): - pass # do nothing, just avoid calling Instance.__init__() + # Do nothing, just avoid calling Instance.__init__() + # + # The reason for this is that Instance.__init__() requires that the + # first argument is a dlite.Instance object (). All needed + # instantiation is already done in __new__(). + pass def __repr__(self): return f""