From 72d4dacf8f54b129e1428c86db59d634fb9898b5 Mon Sep 17 00:00:00 2001 From: Samuel Abels Date: Sat, 17 Oct 2026 14:43:39 +0200 Subject: [PATCH] Type the event callback in the generated stubs The event_callback parameter was typed typing.Any, leaving embedders without a checked contract. The stubs now declare it as typing.Optional[collections.abc.Callable[[SessionEvent], typing.Any]] via a gen_stub argument type override. --- python/raydriver/grbl/__init__.pyi | 5 +++-- src/python/grbl/session.rs | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/python/raydriver/grbl/__init__.pyi b/python/raydriver/grbl/__init__.pyi index afee7b8..d7641cc 100644 --- a/python/raydriver/grbl/__init__.pyi +++ b/python/raydriver/grbl/__init__.pyi @@ -7,6 +7,7 @@ The central type is GrblSession: it owns the connection lifecycle (handshake, st """ import builtins +import collections.abc import typing from . import parser from . import types @@ -45,9 +46,9 @@ class GrblSession: def job_running(self) -> builtins.bool: ... @property def resource_uri(self) -> typing.Optional[builtins.str]: ... - def __new__(cls, config: typing.Optional[dict], dialect: typing.Optional[dict] = None, event_callback: typing.Optional[typing.Any] = None) -> GrblSession: ... + def __new__(cls, config: typing.Optional[dict], dialect: typing.Optional[dict] = None, event_callback: typing.Optional[collections.abc.Callable[[SessionEvent], typing.Any]] = None) -> GrblSession: ... @staticmethod - def with_transport(transport: MockTransport, config: typing.Optional[dict] = None, dialect: typing.Optional[dict] = None, event_callback: typing.Optional[typing.Any] = None) -> GrblSession: + def with_transport(transport: MockTransport, config: typing.Optional[dict] = None, dialect: typing.Optional[dict] = None, event_callback: typing.Optional[collections.abc.Callable[[SessionEvent], typing.Any]] = None) -> GrblSession: r""" Build a session around a [`MockTransport`] (for tests). """ diff --git a/src/python/grbl/session.rs b/src/python/grbl/session.rs index 82c402b..913b460 100644 --- a/src/python/grbl/session.rs +++ b/src/python/grbl/session.rs @@ -188,6 +188,10 @@ impl GrblSession { fn new( config: Option>, dialect: Option>, + #[gen_stub(override_type( + type_repr = "typing.Optional[collections.abc.Callable[[SessionEvent], typing.Any]]", + imports = ("collections.abc", "typing") + ))] event_callback: Option>, ) -> PyResult { let kind = transport_kind_from_config(config.as_ref())?; @@ -202,6 +206,10 @@ impl GrblSession { transport: PyRef<'_, MockTransport>, config: Option>, dialect: Option>, + #[gen_stub(override_type( + type_repr = "typing.Optional[collections.abc.Callable[[SessionEvent], typing.Any]]", + imports = ("collections.abc", "typing") + ))] event_callback: Option>, ) -> PyResult { let kind = TransportKind::Mock(transport.inner.clone());