From a507e5fe9d5d444050fdb7a2f9ee82e192ba10e7 Mon Sep 17 00:00:00 2001 From: "David F. Severski" Date: Sat, 14 Nov 2026 14:50:16 -0500 Subject: [PATCH 1/4] feat: ship py.typed and pass mypy --strict --- pyemoncms/emoncms_client.py | 24 +++++++++++++----------- pyemoncms/py.typed | 0 setup.py | 1 + 3 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 pyemoncms/py.typed diff --git a/pyemoncms/emoncms_client.py b/pyemoncms/emoncms_client.py index 181a84a..f8f17a5 100644 --- a/pyemoncms/emoncms_client.py +++ b/pyemoncms/emoncms_client.py @@ -12,13 +12,15 @@ so there is a type error if you search for a key in the response """ +from __future__ import annotations + import asyncio from dataclasses import dataclass import json import logging -from typing import Any, Dict, TypeVar, Optional +from typing import Any, Dict, TypeVar, Optional, cast -from aiohttp import ClientError, ClientSession +from aiohttp import ClientError, ClientSession, ClientTimeout HTTP_STATUS = { 400: "invalid request", @@ -51,8 +53,8 @@ class EmoncmsConnectionError(EmoncmsError): class EmoncmsClient: """Emoncms client.""" - url: str = None - api_key: str = None + url: str = "" + api_key: str | None = None request_timeout: int = 20 session: ClientSession | None = None _close_session: bool = False @@ -75,7 +77,7 @@ async def async_request( url= f'{self.url}/{path}' try: response = await self.session.get( - url, timeout=self.request_timeout, params=params + url, timeout=ClientTimeout(total=self.request_timeout), params=params ) if response.status == 200: json_response = await response.json() @@ -144,7 +146,7 @@ async def async_input_post( try: async with self.session.post( - endpoint, params=params, timeout=self.request_timeout + endpoint, params=params, timeout=ClientTimeout(total=self.request_timeout) ) as resp: text = await resp.text() status = resp.status @@ -169,7 +171,7 @@ async def async_get_uuid(self) -> str | None: if json_response is False: return None if json_response[SUCCESS_KEY]: - return json_response[MESSAGE_KEY] + return cast(str, json_response[MESSAGE_KEY]) message = json_response[MESSAGE_KEY] message = f"{message} : {self.url}" self.logger.debug(message) @@ -179,15 +181,15 @@ async def async_list_feeds(self) -> list[dict[str, Any]] | None: """Request emoncms feeds list.""" feed_data = await self.async_request("/feed/list.json") if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[dict[str, Any]], feed_data[MESSAGE_KEY]) return None - async def async_get_feed_fields(self, feed_id: int) -> list[str, Any] | None: + async def async_get_feed_fields(self, feed_id: int) -> list[Any] | None: """Get all fields for a single feed.""" params = {"apikey": self.api_key, "id": feed_id} feed_data = await self.async_request("/feed/aget.json", params=params) if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[Any], feed_data[MESSAGE_KEY]) return None async def close(self) -> None: @@ -195,7 +197,7 @@ async def close(self) -> None: if self.session and self._close_session: await self.session.close() - async def __aenter__(self) -> Self: + async def __aenter__(self: Self) -> Self: """Async enter for context manager.""" return self diff --git a/pyemoncms/py.typed b/pyemoncms/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 1e645e9..33e181e 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "Operating System :: OS Independent", ], packages=setuptools.find_packages(), + package_data={"pyemoncms": ["py.typed"]}, install_requires=[ 'aiohttp' ], From f068850b294f6e003b136ff8866f2db3e5fe9d79 Mon Sep 17 00:00:00 2001 From: "David F. Severski" Date: Sat, 14 Nov 2026 14:52:30 -0500 Subject: [PATCH 2/4] fix: stop configuring the root logger on import --- README.md | 1 + pyemoncms/emoncms_client.py | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index f1c68b2..a02e266 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ import logging import asyncio from pyemoncms import EmoncmsClient +logging.basicConfig() key = "your32bitsAPIkey" url = "http://url:8081" client = EmoncmsClient(url, key, request_timeout=1) diff --git a/pyemoncms/emoncms_client.py b/pyemoncms/emoncms_client.py index f8f17a5..080fdb5 100644 --- a/pyemoncms/emoncms_client.py +++ b/pyemoncms/emoncms_client.py @@ -32,8 +32,6 @@ MESSAGE_KEY = "message" SUCCESS_KEY = "success" -logging.basicConfig() - Self = TypeVar("Self", bound="EmoncmsClient") From 9603c12ea1d49a7fb92ea79c431bf1273d96dbd7 Mon Sep 17 00:00:00 2001 From: "David F. Severski" Date: Sat, 14 Nov 2026 14:50:16 -0500 Subject: [PATCH 3/4] feat: ship py.typed and pass mypy --strict --- pyemoncms/emoncms_client.py | 24 +++++++++++++----------- pyemoncms/py.typed | 0 setup.py | 1 + 3 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 pyemoncms/py.typed diff --git a/pyemoncms/emoncms_client.py b/pyemoncms/emoncms_client.py index f6abe68..f231e24 100644 --- a/pyemoncms/emoncms_client.py +++ b/pyemoncms/emoncms_client.py @@ -12,13 +12,15 @@ so there is a type error if you search for a key in the response """ +from __future__ import annotations + import asyncio from dataclasses import dataclass import json import logging -from typing import Any, Dict, TypeVar, Optional +from typing import Any, Dict, TypeVar, Optional, cast -from aiohttp import ClientError, ClientSession +from aiohttp import ClientError, ClientSession, ClientTimeout HTTP_STATUS = { 400: "invalid request", @@ -51,8 +53,8 @@ class EmoncmsConnectionError(EmoncmsError): class EmoncmsClient: """Emoncms client.""" - url: str = None - api_key: str = None + url: str = "" + api_key: str | None = None request_timeout: int = 20 session: ClientSession | None = None _close_session: bool = False @@ -75,7 +77,7 @@ async def async_request( url= f'{self.url}/{path}' try: response = await self.session.get( - url, timeout=self.request_timeout, params=params + url, timeout=ClientTimeout(total=self.request_timeout), params=params ) except ClientError as er: message = f"client error : {er}" @@ -143,7 +145,7 @@ async def async_input_post( try: async with self.session.post( - endpoint, params=params, timeout=self.request_timeout + endpoint, params=params, timeout=ClientTimeout(total=self.request_timeout) ) as resp: text = await resp.text() status = resp.status @@ -168,7 +170,7 @@ async def async_get_uuid(self) -> str | None: if json_response is False: return None if json_response[SUCCESS_KEY]: - return json_response[MESSAGE_KEY] + return cast(str, json_response[MESSAGE_KEY]) message = json_response[MESSAGE_KEY] message = f"{message} : {self.url}" self.logger.debug(message) @@ -178,15 +180,15 @@ async def async_list_feeds(self) -> list[dict[str, Any]] | None: """Request emoncms feeds list.""" feed_data = await self.async_request("/feed/list.json") if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[dict[str, Any]], feed_data[MESSAGE_KEY]) return None - async def async_get_feed_fields(self, feed_id: int) -> list[str, Any] | None: + async def async_get_feed_fields(self, feed_id: int) -> list[Any] | None: """Get all fields for a single feed.""" params = {"apikey": self.api_key, "id": feed_id} feed_data = await self.async_request("/feed/aget.json", params=params) if feed_data[SUCCESS_KEY]: - return feed_data[MESSAGE_KEY] + return cast(list[Any], feed_data[MESSAGE_KEY]) return None async def close(self) -> None: @@ -194,7 +196,7 @@ async def close(self) -> None: if self.session and self._close_session: await self.session.close() - async def __aenter__(self) -> Self: + async def __aenter__(self: Self) -> Self: """Async enter for context manager.""" return self diff --git a/pyemoncms/py.typed b/pyemoncms/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 1e645e9..33e181e 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ "Operating System :: OS Independent", ], packages=setuptools.find_packages(), + package_data={"pyemoncms": ["py.typed"]}, install_requires=[ 'aiohttp' ], From 9b913fbb6ffed211f873ca9f2c63e8515b7cc540 Mon Sep 17 00:00:00 2001 From: Alexandre CUER Date: Mon, 16 Nov 2026 16:01:57 +0100 Subject: [PATCH 4/4] updating python-publish workflow so it checks for py.typed not sure it is needed.... --- .github/workflows/python-publish.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml index 15e93dd..73b7dda 100644 --- a/.github/workflows/python-publish.yml +++ b/.github/workflows/python-publish.yml @@ -32,6 +32,9 @@ jobs: pip install build - name: Build package run: python -m build + - name: Verify py.typed + run: | + unzip -l dist/*.whl | grep 'pyemoncms/py.typed' - name: Publish package uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29 with: