diff --git a/src/sublime_aio.py b/src/sublime_aio.py index 5f7c301..11eaed8 100644 --- a/src/sublime_aio.py +++ b/src/sublime_aio.py @@ -2,6 +2,7 @@ import asyncio import concurrent.futures +import inspect import io import os import sys @@ -413,15 +414,11 @@ class ApplicationCommand(sublime_plugin.ApplicationCommand): An async `Command` instantiated just once. """ - def __init__(self): - """:meta private:""" - if not iscoroutinefunction(self.run): - raise TypeError(f"{type(self).__name__}.run() must be an asyncio coroutine function!") - def run_(self, edit_token: int, args: sublime.CommandArgs) -> None: args = self.filter_args(args) try: - call_coroutine(self.run(**args) if args else self.run()) + if (maybe_coro := self.run(**args) if args else self.run()) and inspect.iscoroutine(maybe_coro): + call_coroutine(maybe_coro) except TypeError as e: if ( "required positional argument" in str(e) @@ -448,17 +445,17 @@ class WindowCommand(sublime_plugin.WindowCommand): retrieved via `self.window `. """ - def __init__(self, window: sublime.Window): # pyright: ignore[reportMissingSuperCall] + def __init__(self, window: sublime.Window): """:meta private:""" - self.window: Window = Window(window.id()) # pyright: ignore[reportIncompatibleVariableOverride] - """The asyncio supporting `Window` this command is attached to.""" - if not iscoroutinefunction(self.run): - raise TypeError(f"{type(self).__name__}.run() must be an asyncio coroutine function!") + + self.window: Window = Window(window.id()) + """ The asyncio supporting `Window` this command is attached to. """ def run_(self, edit_token: int, args: sublime.CommandArgs) -> None: args = self.filter_args(args) try: - call_coroutine(self.run(**args) if args else self.run()) + if (maybe_coro := self.run(**args) if args else self.run()) and inspect.iscoroutine(maybe_coro): + call_coroutine(maybe_coro) except TypeError as e: if ( "required positional argument" in str(e) @@ -497,17 +494,13 @@ async def run(self): ``` """ - def __init__(self, view: sublime.View): # pyright: ignore[reportMissingSuperCall] - """:meta private:""" - self.view: View = View(view.id()) # pyright: ignore[reportIncompatibleVariableOverride] - """The asyncio supporting `View` this command is attached to.""" - if not iscoroutinefunction(self.run): - raise TypeError(f"{type(self).__name__}.run() must be an asyncio coroutine function!") - def run_(self, edit_token: int, args: sublime.CommandArgs) -> None: args = self.filter_args(args) try: - call_coroutine(self.run(**args) if args else self.run()) + if inspect.iscoroutinefunction(self.run): + call_coroutine(self.run(**args) if args else self.run()) + else: + self.run(edit_token, **args) if args else self.run(edit_token) except TypeError as e: if ( "required positional argument" in str(e)