Skip to content

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

Description

@johanzander

Now that the library is type-checked by consumers (py.typed, 2.2.0), a few annotations turn out not to match the actual runtime shapes / call sites. This is follow-up on the typing I added — fixing it at the source so downstream consumers (Home Assistant) don't have to work around it with # type: ignore / Any.

1. GrowattApi.plant_list return type (base_api.py)

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

For the classic PlantListAPI.do endpoint, back is a dict, not a list:

{
    "data": [
        {"plantId": "123456", "plantName": "...", "todayEnergy": "2.6 kWh", ...},
    ],
    "totalData": {...},
}

Callers index it as plant_info["data"][0]["plantId"], which only works on a dict. The current list[dict[str, Any]] annotation makes mypy reject "data" in plant_info (comparison-overlap) and plant_info["data"] (call-overload).

  • Fix: -> dict[str, Any], and change the fallback default from [] to {}.
  • The docstring ("Returns: list ...") should be updated to say dict.

2. OpenApiV1.device_list parameter type (open_api_v1/__init__.py)

def device_list(self, plant_id: int) -> dict[str, Any]:  # type: ignore[override]

GrowattApi.device_list uses plant_id: str, and plant IDs are opaque string identifiers throughout the API. The int here mismatches the base class — which is why the # type: ignore[override] is needed — and forces str-passing callers to cast.

  • Fix: plant_id: str, which also removes the # type: ignore[override].

3. OpenApiV1.plant_energy_overview parameter type (open_api_v1/__init__.py)

def plant_energy_overview(self, plant_id: int) -> dict[str, Any]:

Same as above — callers pass a str plant ID.

  • Fix: plant_id: str.

I'll follow up with a PR.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions