Skip to content

Commit

Permalink
Merge pull request #43 from oemof/fix/encourage-constant-labels
Browse files Browse the repository at this point in the history
Require label of Node to be given
  • Loading branch information
p-snft authored Dec 19, 2023
2 parents 00c5e42 + 94b99e6 commit ee63186
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/oemof/network/network/edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def input(self):
@input.setter
def input(self, i):
old_input = self.input
self.label = Edge.Label(i, self.label.output)
self._label = Edge.Label(i, self.label.output)
if old_input is None and i is not None and self.output is not None:
i.outputs[self.output] = self

Expand All @@ -113,6 +113,6 @@ def output(self):
@output.setter
def output(self, o):
old_output = self.output
self.label = Edge.Label(self.label.input, o)
self._label = Edge.Label(self.label.input, o)
if old_output is None and o is not None and self.input is not None:
o.inputs[self.input] = self
8 changes: 2 additions & 6 deletions src/oemof/network/network/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Entity:
to easily attach custom information to any Entity.
"""

def __init__(self, label=None, *, custom_properties=None):
def __init__(self, label, *, custom_properties=None):
self._label = label
if custom_properties is None:
custom_properties = {}
Expand Down Expand Up @@ -78,13 +78,9 @@ def label(self):
"""
try:
return self._label if self._label is not None else self._id_label
except AttributeError:
except AttributeError: # Workaround for problems with pickle/dill
return hash(self._id_label)

@property
def _id_label(self):
return "<{} #0x{:x}>".format(type(self).__name__, id(self))

@label.setter
def label(self, label):
self._label = label
2 changes: 1 addition & 1 deletion src/oemof/network/network/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Node(Entity):

def __init__(
self,
label=None,
label,
*,
inputs=None,
outputs=None,
Expand Down
29 changes: 15 additions & 14 deletions tests/test_network_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_symmetric_input_output_assignment(self):
)

def test_accessing_outputs_of_a_node_without_output_flows(self):
n = Node()
n = Node(label="node")
exception = None
outputs = None
try:
Expand All @@ -79,7 +79,7 @@ def test_accessing_outputs_of_a_node_without_output_flows(self):
)

def test_accessing_inputs_of_a_node_without_input_flows(self):
n = Node()
n = Node(label="node")
exception = None
inputs = None
try:
Expand All @@ -98,7 +98,7 @@ def test_accessing_inputs_of_a_node_without_input_flows(self):
)

def test_that_the_outputs_attribute_of_a_node_is_a_mapping(self):
n = Node()
n = Node(label="node")
exception = None
try:
n.outputs.values()
Expand Down Expand Up @@ -252,16 +252,16 @@ def test_entity_input_output_type_assertions(self):
with pytest.raises(ValueError):
Node("An entity with an input", inputs={"Not a Node": "A Flow"})

def test_node_label_without_private_attribute(self):
def test_node_requires_label(self):
"""
A `Node` without `label` doesn't have the `_label` attribute set.
A `Node` without `label` cannot be constructed.
"""
n = Node()
assert not n._label
with pytest.raises(TypeError):
_ = Node()

def test_node_label_if_its_not_explicitly_specified(self):
"""If not explicitly given, a `Node`'s label is based on its `id`."""
n = Node()
n = Node(label=None)
assert "0x{:x}>".format(id(n)) in n.label


Expand Down Expand Up @@ -344,25 +344,26 @@ def test_entity_registration(self):

def test_deprecated_classes():
with pytest.warns(FutureWarning):
Bus()
Bus("bus")
with pytest.warns(FutureWarning):
Sink()
Sink("sink")
with pytest.warns(FutureWarning):
Source()
Source("source")
with pytest.warns(FutureWarning):
Transformer()
Transformer("transformer")


def test_custom_properties():
node0 = Node()
node0 = Node("n0")

assert not node0.custom_properties

node1 = Node(
"n1",
custom_properties={
"foo": "bar",
1: 2,
}
},
)
assert node1.custom_properties["foo"] == "bar"
assert node1.custom_properties[1] == 2
Expand Down

0 comments on commit ee63186

Please sign in to comment.