Skip to content

Commit 753f8f8

Browse files
committed
Take the proposal bounds apart with ifs instead of a match
The `match` covered all four combinations of a proposal's optional lower and upper bounds, but mypy can't narrow tuple items across `case` clauses (see python/mypy#12364), so it can't prove such a `match` exhaustive no matter how many cases are written. Nested `if`s over the two bounds cover the same four combinations, mypy does check those, and the shared code between the cases no longer needs repeating. There is no behaviour change. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 0aa21e7 commit 753f8f8

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

‎src/frequenz/sdk/microgrid/_power_managing/_shifting_matryoshka.py‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,24 @@ def _calc_targets( # pylint: disable=too-many-branches,too-many-statements
122122
if upper_bound < lower_bound:
123123
break
124124

125-
match (next_proposal.bounds.lower, next_proposal.bounds.upper):
126-
case (None, None):
125+
# Bounds the proposal leaves open are taken from the currently available
126+
# bounds, unless the bound the proposal does set is already past them, in
127+
# which case the proposal collapses to that single value.
128+
proposal_lower = next_proposal.bounds.lower
129+
proposal_upper = next_proposal.bounds.upper
130+
if proposal_lower is None:
131+
if proposal_upper is None:
127132
proposal_lower = lower_bound
128133
proposal_upper = upper_bound
129-
case (Power(), None):
130-
proposal_lower = next_proposal.bounds.lower
131-
if proposal_lower > upper_bound:
132-
proposal_upper = proposal_lower
133-
else:
134-
proposal_upper = upper_bound
135-
case (None, Power()):
136-
proposal_upper = next_proposal.bounds.upper
137-
if proposal_upper < lower_bound:
138-
proposal_lower = proposal_upper
139-
else:
140-
proposal_lower = lower_bound
141-
case (Power(), Power()):
142-
proposal_lower = next_proposal.bounds.lower
143-
proposal_upper = next_proposal.bounds.upper
134+
elif proposal_upper < lower_bound:
135+
proposal_lower = proposal_upper
136+
else:
137+
proposal_lower = lower_bound
138+
elif proposal_upper is None:
139+
if proposal_lower > upper_bound:
140+
proposal_upper = proposal_lower
141+
else:
142+
proposal_upper = upper_bound
144143

145144
proposal_power = next_proposal.preferred_power
146145

0 commit comments

Comments
 (0)