Skip to content

Fix incorrect type annotations in OpenApiV1 plant_id and plant_list - #159

Merged
johanzander merged 2 commits into
indykoning:masterfrom
johanzander:align-v1-plant-id-type
Aug 1, 2026
Merged

Fix incorrect type annotations in OpenApiV1 plant_id and plant_list#159
johanzander merged 2 commits into
indykoning:masterfrom
johanzander:align-v1-plant-id-type

Conversation

@johanzander

@johanzander johanzander commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Closes #157.

1. V1 plant_id annotations

GrowattApi annotates plant_id as str on every method that takes one, but OpenApiV1 annotates it as int on plant_details, plant_energy_overview, plant_power_overview, plant_energy_history and device_list.

All five only pass the value through to params={"plant_id": plant_id}, so str has always worked at runtime — only the annotation disagreed. Since 2.2.0 shipped py.typed, the mismatch is now visible to consumers: a caller holding a plant id as a str (which is what the base class returns, and what the ID naturally is) must either suppress arg-type errors or coerce with int() — and coercing crashes on non-numeric ids.

The # type: ignore[override] on device_list is left in place — it covers the return type differing from the base class (dict vs list), which is unrelated to plant_id.

2. plant_list empty-case return

#153 corrected the plant_list annotation to dict[str, Any], but the fallback when the response has no back key is still []:

def plant_list(self, user_id: str) -> dict[str, Any]:
    ...
    return response.json().get("back", [])

So the empty case returns a list and contradicts the annotation. A caller that follows the annotation and does plant_info.get("data") hits an AttributeError on that path, which forces an isinstance() guard purely to defend against the default. plant_detail, on the same endpoint family, already defaults to {}.

Scope

Both changes are annotation-consistency fixes; the only runtime change is the {} default in the empty-response path, which replaces an AttributeError with a falsy dict.

Release note

Together with #153 — merged but unreleased, as 2.2.0 shipped one day before it landed — this unblocks removing the remaining type suppressions and defensive guards in the Home Assistant growatt_server integration (home-assistant/core#173220). Verified locally: with this branch installed, that integration's # type: ignore[arg-type] comments delete cleanly with mypy passing and its full test suite green. A 2.3.0 covering both PRs would let those changes land.

johanzander and others added 2 commits July 31, 2026 08:52
GrowattApi annotates plant_id as str on every method that takes one, but
OpenApiV1 annotates it as int on plant_details, plant_energy_overview,
plant_power_overview, plant_energy_history and device_list.

All five only pass the value through to params={"plant_id": plant_id},
so str has always worked at runtime; only the annotation disagreed.
The mismatch forces callers that hold a plant id as a str to either
suppress arg-type errors or coerce with int(), which breaks on
non-numeric ids.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
plant_list is annotated as returning dict[str, Any], but the fallback
when the response has no "back" key is [], so the empty case returns a
list and contradicts the annotation.

Callers that follow the annotation and do plant_info.get("data") hit an
AttributeError on that path, forcing an isinstance() guard purely to
defend against the default. plant_detail on the same endpoint family
already defaults to {}.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@johanzander johanzander changed the title Align V1 plant_id annotations with the base class Fix incorrect type annotations in OpenApiV1 plant_id and plant_list Jul 31, 2026

@indykoning indykoning left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Simple, and looks good!

@johanzander
johanzander merged commit 6469d88 into indykoning:master Aug 1, 2026
2 checks passed
@johanzander
johanzander deleted the align-v1-plant-id-type branch August 1, 2026 20:44
@johanzander

Copy link
Copy Markdown
Collaborator Author

great! looking forward to the next release!

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.

Incorrect type annotations in 2.2.0 (plant_list return type; OpenApiV1 plant_id: int)

2 participants