Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to _comobject.py (part 2). #704

Merged
merged 6 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading