Skip to content

feat: ship py.typed and pass mypy --strict - #3

Draft
davidski wants to merge 3 commits into
Open-Building-Management:mainfrom
davidski:feat/strict-typing
Draft

davidski wants to merge 3 commits into
Open-Building-Management:mainfrom
davidski:feat/strict-typing

Conversation

@davidski

Copy link
Copy Markdown

Depends on #2. Stacked on that branch, so until #2 merges this diff also shows its commit; review only the last two commits (feat: ship py.typed..., fix: stop configuring the root logger...). I'll rebase onto main and mark ready once #2 merges.

Summary

Makes the package PEP 561 typed so downstream users like Home Assistant can type-check against it, and stops it configuring logging on import.

 pyemoncms/
+├── py.typed                # PEP 561 marker, added to package_data in setup.py
 └── emoncms_client.py
+      from __future__ import annotations   # `str | None` hints load on Python 3.9
-      logging.basicConfig()                 # no longer configures the root logger on import
-      url: str = None
+      url: str = ""
-      api_key: str = None
+      api_key: str | None = None
-      timeout=self.request_timeout
+      timeout=ClientTimeout(total=self.request_timeout)
-      async_get_feed_fields(...) -> list[str, Any] | None
+      async_get_feed_fields(...) -> list[Any] | None
+      cast(...) on JSON returns, `self: Self` on __aenter__

The logging.basicConfig() call moves into the README example so its debug output still prints.

Evidence

  • Before (feat/raise-errors):
    mypy --strict pyemoncms   -> Found 9 errors in 1 file
    python3.9 -c "import pyemoncms"
      -> TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'
    
    After:
    mypy --strict pyemoncms   -> Success: no issues found in 2 source files
    python3.9 -c "import pyemoncms"  -> ok
    
  • Existing tests: 8/8 pass, pylint 10.00/10.

Merge Danger

Door: two-way

Blast Radius: small

  • Programs that relied on the library calling logging.basicConfig() stop seeing its log output until they configure logging themselves.
  • url now defaults to "" instead of None. Every caller passes a URL, so in practice nothing changes.
  • python_requires=">=3.9" now actually holds: on main the package fails to import on 3.9.

Opt-in raise_errors raises EmoncmsAuthError on HTTP 401,
EmoncmsConnectionError on client errors/timeouts and EmoncmsError
otherwise, from async_request and async_input_post.

Closes Open-Building-Management#1
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.

1 participant