Skip to content

Commit 2f9ace8

Browse files
committed
add type annotations
1 parent 5f5429f commit 2f9ace8

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

comtypes/automation.py

+61-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from ctypes import *
88
from ctypes import _Pointer
99
from _ctypes import CopyComPointer
10-
from comtypes import IUnknown, GUID, IID, STDMETHOD, BSTR, COMMETHOD, COMError
10+
from comtypes import (
11+
BSTR, COMError, COMMETHOD, GUID, IID, IUnknown, STDMETHOD, TYPE_CHECKING,
12+
)
1113
from comtypes.hresult import *
1214
import comtypes.patcher
1315
import comtypes
@@ -19,6 +21,12 @@ class _safearray(object):
1921

2022
from ctypes.wintypes import DWORD, LONG, UINT, VARIANT_BOOL, WCHAR, WORD
2123

24+
if TYPE_CHECKING:
25+
from typing import (
26+
Any, Callable, ClassVar, List, Optional, Tuple, Union as _UnionT,
27+
)
28+
from comtypes import hints
29+
2230

2331
if sys.version_info >= (3, 0):
2432
int_types = (int, )
@@ -149,6 +157,13 @@ def as_decimal(self):
149157
# The VARIANT structure is a good candidate for implementation in a C
150158
# helper extension. At least the get/set methods.
151159
class tagVARIANT(Structure):
160+
if TYPE_CHECKING:
161+
vt = hints.AnnoField() # type: int
162+
_ = hints.AnnoField() # type: U_VARIANT1.__tagVARIANT.U_VARIANT2
163+
null = hints.AnnoField() # type: ClassVar[VARIANT]
164+
empty = hints.AnnoField() # type: ClassVar[VARIANT]
165+
missing = hints.AnnoField() # type: ClassVar[VARIANT]
166+
152167
class U_VARIANT1(Union):
153168
class __tagVARIANT(Structure):
154169
# The C Header file defn of VARIANT is much more complicated, but
@@ -650,6 +665,17 @@ def Next(self, celt):
650665

651666

652667
class tagEXCEPINFO(Structure):
668+
if TYPE_CHECKING:
669+
wCode = hints.AnnoField() # type: int
670+
wReserved = hints.AnnoField() # type: int
671+
bstrSource = hints.AnnoField() # type: str
672+
bstrDescription = hints.AnnoField() # type: str
673+
bstrHelpFile = hints.AnnoField() # type: str
674+
dwHelpContext = hints.AnnoField() # type: int
675+
pvReserved = hints.AnnoField() # type: Optional[int]
676+
pfnDeferredFillIn = hints.AnnoField() # type: Optional[int]
677+
scode = hints.AnnoField() # type: int
678+
653679
def __repr__(self):
654680
return "<EXCEPINFO %s>" % \
655681
((self.wCode, self.bstrSource, self.bstrDescription, self.bstrHelpFile, self.dwHelpContext,
@@ -669,6 +695,11 @@ def __repr__(self):
669695
EXCEPINFO = tagEXCEPINFO
670696

671697
class tagDISPPARAMS(Structure):
698+
if TYPE_CHECKING:
699+
rgvarg = hints.AnnoField() # type: Array[VARIANT]
700+
rgdispidNamedArgs = hints.AnnoField() # type: _Pointer[DISPID]
701+
cArgs = hints.AnnoField() # type: int
702+
cNamedArgs = hints.AnnoField() # type: int
672703
_fields_ = [
673704
# C:/Programme/gccxml/bin/Vc71/PlatformSDK/oaidl.h 696
674705
('rgvarg', POINTER(VARIANTARG)),
@@ -691,17 +722,39 @@ def __del__(self):
691722
DISPID_DESTRUCTOR = -7
692723
DISPID_COLLECT = -8
693724

725+
726+
if TYPE_CHECKING:
727+
RawGetIDsOfNamesFunc = Callable[
728+
[_byref_type, Array[c_wchar_p], int, int, Array[DISPID]], int,
729+
]
730+
RawInvokeFunc = Callable[
731+
[
732+
int, _byref_type, int, int, # dispIdMember, riid, lcid, wFlags
733+
_UnionT[_byref_type, DISPPARAMS], # *pDispParams
734+
_UnionT[_byref_type, VARIANT], # pVarResult
735+
_UnionT[_byref_type, EXCEPINFO, None], # pExcepInfo
736+
_UnionT[_byref_type, c_uint], # puArgErr
737+
],
738+
int
739+
]
740+
694741
class IDispatch(IUnknown):
742+
if TYPE_CHECKING:
743+
_disp_methods_ = hints.AnnoField() # type: ClassVar[List[comtypes._DispMemberSpec]]
744+
_GetTypeInfo = hints.AnnoField() # type: Callable[[int, int], IUnknown]
745+
__com_GetIDsOfNames = hints.AnnoField() # type: RawGetIDsOfNamesFunc
746+
__com_Invoke = hints.AnnoField() # type: RawInvokeFunc
747+
695748
_iid_ = GUID("{00020400-0000-0000-C000-000000000046}")
696749
_methods_ = [
697750
COMMETHOD([], HRESULT, 'GetTypeInfoCount',
698751
(['out'], POINTER(UINT) ) ),
699752
COMMETHOD([], HRESULT, 'GetTypeInfo',
700753
(['in'], UINT, 'index'),
701754
(['in'], LCID, 'lcid', 0),
702-
## Normally, we would declare this parameter in this way:
703-
## (['out'], POINTER(POINTER(ITypeInfo)) ) ),
704-
## but we cannot import comtypes.typeinfo at the top level (recursive imports!).
755+
# Normally, we would declare this parameter in this way:
756+
# (['out'], POINTER(POINTER(ITypeInfo)) ) ),
757+
# but we cannot import comtypes.typeinfo at the top level (recursive imports!).
705758
(['out'], POINTER(POINTER(IUnknown)) ) ),
706759
STDMETHOD(HRESULT, 'GetIDsOfNames', [POINTER(IID), POINTER(c_wchar_p),
707760
UINT, LCID, POINTER(DISPID)]),
@@ -711,12 +764,14 @@ class IDispatch(IUnknown):
711764
]
712765

713766
def GetTypeInfo(self, index, lcid=0):
767+
# type: (int, int) -> hints.ITypeInfo
714768
"""Return type information. Index 0 specifies typeinfo for IDispatch"""
715769
import comtypes.typeinfo
716770
result = self._GetTypeInfo(index, lcid)
717771
return result.QueryInterface(comtypes.typeinfo.ITypeInfo)
718772

719773
def GetIDsOfNames(self, *names, **kw):
774+
# type: (str, Any) -> List[int]
720775
"""Map string names to integer ids."""
721776
lcid = kw.pop("lcid", 0)
722777
assert not kw
@@ -726,6 +781,7 @@ def GetIDsOfNames(self, *names, **kw):
726781
return ids[:]
727782

728783
def _invoke(self, memid, invkind, lcid, *args):
784+
# type: (int, int, int, Any) -> Any
729785
var = VARIANT()
730786
argerr = c_uint()
731787
dp = DISPPARAMS()
@@ -747,6 +803,7 @@ def _invoke(self, memid, invkind, lcid, *args):
747803
return var._get_value(dynamic=True)
748804

749805
def Invoke(self, dispid, *args, **kw):
806+
# type: (int, Any, Any) -> Any
750807
"""Invoke a method or property."""
751808

752809
# Memory management in Dispatch::Invoke calls:

comtypes/hints.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ from typing import (
44
)
55

66
# symbols those what might occur recursive imports in runtime.
7+
from comtypes.automation import IDispatch as IDispatch, VARIANT as VARIANT
78
from comtypes.server import IClassFactory as IClassFactory
9+
from comtypes.typeinfo import ITypeInfo as ITypeInfo
810

911

1012
def AnnoField() -> Any:

0 commit comments

Comments
 (0)