Check the JSON shape explicitly instead of AttributeError control flow

This commit is contained in:
J. Nick Koston
2026-08-22 23:33:26 -05:00
parent 5a356215c4
commit f301f54fcf
2 changed files with 5 additions and 5 deletions
+4 -4
View File
@@ -337,14 +337,14 @@ def copy_src_tree():
else:
try:
existing = json.loads(build_info_json_path.read_text(encoding="utf-8"))
if (
if not isinstance(existing, dict) or (
existing.get("config_hash") != config_hash
or existing.get("esphome_version") != __version__
):
# Valid JSON that is not an object (truncated or
# hand-edited) is stale like every other damage case
sources_changed = True
except (json.JSONDecodeError, AttributeError, KeyError, OSError):
# AttributeError: valid JSON that is not an object (truncated
# or hand-edited) has no .get; treat it as stale like the rest
except (json.JSONDecodeError, OSError):
sources_changed = True
# Write build_info header and JSON metadata
+1 -1
View File
@@ -2232,7 +2232,7 @@ def test_copy_src_tree_handles_non_dict_build_info_json(
build_path = tmp_path / "build"
build_path.mkdir()
# Create invalid build_info.json
# Valid JSON that is not an object: no .get, must read as stale
build_info_json_path = build_path / "build_info.json"
build_info_json_path.write_text("[]")