Skip to content

Commit

Permalink
Add type hints to _comobject.py (part 2). (#704)
Browse files Browse the repository at this point in the history
* Change import source of `COMError`.

* Add type hints to `catch_errors`.

* Add type hints to `hack`.

* Add type hints to `_create_vtbl_type`.

* Add type hints to `_vtbl_types`.

* Add type hints to class variables and instance variables of `COMObject`.
  • Loading branch information
junkmd authored Dec 17, 2024
1 parent 4e20f7e commit b07cec2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
45 changes: 37 additions & 8 deletions comtypes/_comobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import queue
import sys
from _ctypes import CopyComPointer
from _ctypes import COMError, CopyComPointer
from ctypes import (
POINTER,
WINFUNCTYPE,
Expand All @@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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__
Expand Down Expand Up @@ -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:
Expand All @@ -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]] = {}

################################################################

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion comtypes/hints.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b07cec2

Please sign in to comment.