Skip to content

Fix TypeError in dp1/dp2/dp3/dp4 when value is None#3961

Open
Scanlia wants to merge 1 commit into
springfall2008:mainfrom
Scanlia:fix/nonetype-guard-dp
Open

Fix TypeError in dp1/dp2/dp3/dp4 when value is None#3961
Scanlia wants to merge 1 commit into
springfall2008:mainfrom
Scanlia:fix/nonetype-guard-dp

Conversation

@Scanlia
Copy link
Copy Markdown

@Scanlia Scanlia commented May 25, 2026

The dp1/dp2/dp3/dp4 rounding helpers in utils.py crash with TypeError when passed None (e.g. an inverter entity not yet initialised). Add an early None guard to each.

def dp1(value):
    if value is None:
        return None
    return round(value, 1)

Same pattern for dp2, dp3, dp4. 1 file changed, 8 lines inserted.

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.
Copilot AI review requested due to automatic review settings May 25, 2026 09:43
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 None early from dp1, dp2, dp3, and dp4 when value is None
  • Preserve existing round(..., n) behavior for non-None inputs
Comments suppressed due to low confidence (3)

apps/predbat/utils.py:850

  • Returning None introduces 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 having dp1dp4 delegate 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 having dp1dp4 delegate to it, or generating these functions from a shared implementation.
# -----------------------------------------------------------------------------

Comment thread apps/predbat/utils.py
Comment on lines 851 to +855
"""
Round to 1 decimal place
"""
if value is None:
return None
Comment thread apps/predbat/utils.py
Comment on lines +854 to 856
if value is None:
return None
return round(value, 1)
Comment thread apps/predbat/utils.py
Comment on lines 870 to 874
Round to 3 decimal places
"""
if value is None:
return None
return round(value, 3)
Comment thread apps/predbat/utils.py
Comment on lines 879 to 883
Round to 4 decimal places
"""
if value is None:
return None
return round(value, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants