From 30839413484f72343d66035bbd77af700059d0a3 Mon Sep 17 00:00:00 2001 From: prabinpanta0 Date: Sun, 26 Oct 2025 19:42:36 +0545 Subject: config(hypr): improve numeric coercion and add ensure_* warnings - Parse numeric strings more robustly in coerce_number: convert to float then return int when the float has no fractional part (handles scientific notation and avoids brittle '.'/'e' checks). - Add diagnostic warnings to ensure_dict and ensure_list that print the unexpected type and a truncated repr to stderr to help detect API shape mismatches. --- config/hypr/UserScripts/Weather.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'config/hypr/UserScripts/Weather.py') diff --git a/config/hypr/UserScripts/Weather.py b/config/hypr/UserScripts/Weather.py index d76fb8db..a566d5ec 100755 --- a/config/hypr/UserScripts/Weather.py +++ b/config/hypr/UserScripts/Weather.py @@ -207,9 +207,9 @@ def coerce_number(value: Any) -> Union[int, float, None]: return value if isinstance(value, str): try: - # Try float first, then int if no decimal + # Parse to float, then return int if it has no fractional part f = float(value) - return f if '.' in value or 'e' in value.lower() else int(f) + return int(f) if f.is_integer() else f except (ValueError, TypeError): return None return None @@ -508,6 +508,11 @@ def ensure_dict(value: Any) -> JSONDict: """Return a JSON-like dict when the incoming value looks like one.""" if isinstance(value, dict): return cast(JSONDict, value) + # Warn about unexpected type to catch API shape mismatches + val_repr = repr(value) if value is not None else "None" + if len(val_repr) > 100: + val_repr = val_repr[:100] + "..." + print(f"Warning: ensure_dict received {type(value).__name__} instead of dict: {val_repr}", file=sys.stderr) return cast(JSONDict, {}) @@ -515,6 +520,11 @@ def ensure_list(value: Any) -> JSONList: """Return a JSON-like list when the incoming value looks like one.""" if isinstance(value, list): return cast(JSONList, value) + # Warn about unexpected type to catch API shape mismatches + val_repr = repr(value) if value is not None else "None" + if len(val_repr) > 100: + val_repr = val_repr[:100] + "..." + print(f"Warning: ensure_list received {type(value).__name__} instead of list: {val_repr}", file=sys.stderr) return cast(JSONList, []) -- cgit v1.2.3