Skip to content

Commit

Permalink
Forbid valid_sequence to overwrite existing length
Browse files Browse the repository at this point in the history
  • Loading branch information
p-snft committed Aug 29, 2024
1 parent f7e72ae commit f6f5339
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 9 additions & 3 deletions src/oemof/solph/_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/test_plumbing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f6f5339

Please sign in to comment.