diff --git a/src/oemof/solph/_plumbing.py b/src/oemof/solph/_plumbing.py index 10e9f423d..77c8ffb09 100644 --- a/src/oemof/solph/_plumbing.py +++ b/src/oemof/solph/_plumbing.py @@ -59,15 +59,21 @@ def sequence(iterable_or_scalar): def valid_sequence(sequence, length: int) -> bool: """Checks if an object is a numpy array of at least the given length or an 'emulated' sequence object of class _FakeSequence. - The latter is set to the required lenght. + If unset, the latter is set to the required lenght. """ if sequence[0] is None: return False if isinstance(sequence, _FakeSequence): - sequence.size = length - return True + if sequence.size is None: + sequence.size = length + + if sequence.size == length: + return True + else: + return False + if isinstance(sequence, np.ndarray): if sequence.size == length: return True diff --git a/tests/test_plumbing.py b/tests/test_plumbing.py index 618f2df7d..f97ff96c1 100644 --- a/tests/test_plumbing.py +++ b/tests/test_plumbing.py @@ -81,8 +81,14 @@ def test_valid_sequence(): assert valid_sequence(fake_sequence, 5) assert len(fake_sequence) == 5 - # possible for any length + # wil not automatically overwrite size + assert not valid_sequence(fake_sequence, 1337) + assert len(fake_sequence) == 5 + + # manually overwriting length is still possible + fake_sequence.size = 1337 assert valid_sequence(fake_sequence, 1337) assert len(fake_sequence) == 1337 + # strings are no valid sequences assert not valid_sequence("abc", 3)