diff --git a/comtypes/_comobject.py b/comtypes/_comobject.py index 7aa3a5a2..1959575a 100644 --- a/comtypes/_comobject.py +++ b/comtypes/_comobject.py @@ -2,7 +2,7 @@ import os import queue import sys -from _ctypes import CopyComPointer +from _ctypes import COMError, CopyComPointer from ctypes import ( POINTER, WINFUNCTYPE, @@ -15,9 +15,19 @@ pointer, windll, ) -from typing import TYPE_CHECKING, Callable, Optional, Sequence +from typing import ( + TYPE_CHECKING, + Any, + Callable, + ClassVar, + Dict, + Optional, + Sequence, + Tuple, + Type, +) -from comtypes import COMError, IPersist, ReturnHRESULT, instancemethod +from comtypes import GUID, IPersist, IUnknown, ReturnHRESULT, instancemethod from comtypes._memberspec import _encode_idl from comtypes.errorinfo import ISupportErrorInfo, ReportError, ReportException from comtypes.hresult import ( @@ -34,7 +44,10 @@ from comtypes.typeinfo import IProvideClassInfo, IProvideClassInfo2 if TYPE_CHECKING: + from ctypes import _FuncPointer + from comtypes import hints # type: ignore + from comtypes._memberspec import _ParamFlagType logger = logging.getLogger(__name__) _debug = logger.debug @@ -91,7 +104,13 @@ def _not_implemented(*args): return _not_implemented -def catch_errors(obj, mth, paramflags, interface, mthname): +def catch_errors( + obj: "COMObject", + mth: Callable[..., Any], + paramflags: Optional[Tuple["_ParamFlagType", ...]], + interface: Type[IUnknown], + mthname: str, +) -> Callable[..., Any]: clsid = getattr(obj, "_reg_clsid_", None) def call_with_this(*args, **kw): @@ -134,7 +153,13 @@ def call_with_this(*args, **kw): ################################################################ -def hack(inst, mth, paramflags, interface, mthname): +def hack( + inst: "COMObject", + mth: Callable[..., Any], + paramflags: Optional[Tuple["_ParamFlagType", ...]], + interface: Type[IUnknown], + mthname: str, +) -> Callable[..., Any]: if paramflags is None: return catch_errors(inst, mth, paramflags, interface, mthname) code = mth.__code__ @@ -299,7 +324,9 @@ def get(self): return instancemethod(get, self.inst, type(self.inst)) -def _create_vtbl_type(fields, itf): +def _create_vtbl_type( + fields: Tuple[Tuple[str, Type["_FuncPointer"]], ...], itf: Type[IUnknown] +) -> Type[Structure]: try: return _vtbl_types[fields] except KeyError: @@ -313,7 +340,7 @@ class Vtbl(Structure): # Ugh. Another type cache to avoid leaking types. -_vtbl_types = {} +_vtbl_types: Dict[Tuple[Tuple[str, Type["_FuncPointer"]], ...], Type[Structure]] = {} ################################################################ @@ -412,7 +439,9 @@ def DllCanUnloadNow(self) -> int: class COMObject(object): - _instances_ = {} + _instances_: ClassVar[Dict["COMObject", None]] = {} + _reg_clsid_: ClassVar[GUID] + __typelib: "hints.ITypeLib" def __new__(cls, *args, **kw): self = super(COMObject, cls).__new__(cls) diff --git a/comtypes/hints.pyi b/comtypes/hints.pyi index bc4a308d..2d3793f6 100644 --- a/comtypes/hints.pyi +++ b/comtypes/hints.pyi @@ -28,7 +28,7 @@ from comtypes import IUnknown as IUnknown, GUID as GUID from comtypes.automation import IDispatch as IDispatch, VARIANT as VARIANT from comtypes.server import IClassFactory as IClassFactory from comtypes.server import localserver as localserver -from comtypes.typeinfo import ITypeInfo as ITypeInfo +from comtypes.typeinfo import ITypeInfo as ITypeInfo, ITypeLib as ITypeLib from comtypes._safearray import tagSAFEARRAY as tagSAFEARRAY Incomplete: TypeAlias = Any