Fix TypeError in dp1/dp2/dp3/dp4 when value is None#3961
Open
Scanlia wants to merge 1 commit into
Open
Conversation
Avoid TypeError: type NoneType doesn't define __round__ method when load_today_comparison passes None into rounding helpers (occurs when now_utc doesn't align to PREDICT_STEP boundary). Bug surface: load_adjusted_stamp only contains future minutes >= minutes_now, so an unaligned 'now' produces a None lookup. This is a candidate upstream PR.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds None-safe rounding helpers in apps/predbat/utils.py to avoid TypeError when rounding unset values.
Changes:
- Return
Noneearly fromdp1,dp2,dp3, anddp4whenvalueisNone - Preserve existing
round(..., n)behavior for non-Noneinputs
Comments suppressed due to low confidence (3)
apps/predbat/utils.py:850
- Returning
Noneintroduces a mixed return type (float | None) which can be a breaking behavioral change for callers that assume a numeric return. Consider making this explicit via type hints (e.g.,Optional[float]for the return type, and optionally for the parameter) and/or documenting the contract centrally to avoid subtle downstream issues.
def dp1(value):
apps/predbat/utils.py:850
- The
None-guard logic is duplicated across four functions. To reduce repetition and keep behavior consistent, consider extracting a single helper (e.g.,dp(value, places)) and havingdp1–dp4delegate to it, or generating these functions from a shared implementation.
def dp1(value):
apps/predbat/utils.py:1
- The
None-guard logic is duplicated across four functions. To reduce repetition and keep behavior consistent, consider extracting a single helper (e.g.,dp(value, places)) and havingdp1–dp4delegate to it, or generating these functions from a shared implementation.
# -----------------------------------------------------------------------------
Comment on lines
851
to
+855
| """ | ||
| Round to 1 decimal place | ||
| """ | ||
| if value is None: | ||
| return None |
Comment on lines
+854
to
856
| if value is None: | ||
| return None | ||
| return round(value, 1) |
Comment on lines
870
to
874
| Round to 3 decimal places | ||
| """ | ||
| if value is None: | ||
| return None | ||
| return round(value, 3) |
Comment on lines
879
to
883
| Round to 4 decimal places | ||
| """ | ||
| if value is None: | ||
| return None | ||
| return round(value, 4) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
dp1/dp2/dp3/dp4rounding helpers inutils.pycrash withTypeErrorwhen passedNone(e.g. an inverter entity not yet initialised). Add an earlyNoneguard to each.Same pattern for
dp2,dp3,dp4. 1 file changed, 8 lines inserted.