Skip to content

Add service.call() and service.has_service() to the IDE stubs (#788) - #952

Open
craigbarratt wants to merge 1 commit into
masterfrom
stub-service-call
Open

craigbarratt wants to merge 1 commit into
masterfrom
stub-service-call

Conversation

@craigbarratt

Copy link
Copy Markdown
Member

Fixes #788.

In pyscript_builtins.py, service was only the @service decorator function, so IDEs and type checkers flagged service.call() and service.has_service(), which pyscript provides at runtime.

service is now an instance of a small _service class:

  • __call__ has two overloads, for bare @service and for @service("DOMAIN.SERVICE", supports_response=...). Both keep the decorated function's type, so calls to it are still checked.
  • call(domain, name, /, *, blocking=False, return_response=False, **kwargs) and has_service(domain, name) match Function.service_call and service_has_service.

service is a real object, not only an annotation, so from stubs.pyscript_builtins import service still works if the file is imported by plain Python.

Checked with mypy on a sample script using the stub: both decorator forms, a call to the decorated function, service.call() with and without return_response, and service.has_service() pass. service.call("light"), a decorated function called with the wrong type, and supports_response="bad" are reported.

Tests: a new test in tests/test_stubs.py checks that the stub's service is callable and has call and has_service; it fails on master. All 308 tests pass, and ruff, pylint and mypy are clean.

🤖 Generated with Claude Code

The service stub was only the @service decorator function, so IDEs flagged
service.call() and service.has_service(). It is now an object that works as
the decorator, bare or with arguments, and also has typed call() and
has_service() methods. The decorator keeps the decorated function's type.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

service.call() is not generated in pyscript_builtins.py

1 participant