Skip to content

Commit 7b38a7a

Browse files
authored
change type hints from comment annotations to inline annotations in comtypes/__init__.py, comtypes/automation.py and comtypes/typeinfo.py (enthought#454)
* update `comtypes/__init__.py` * update `comtypes/automation.py` * update `comtypes/typeinfo.py` * fix `Array[T]` to quoted literal strings
1 parent 7321eaf commit 7b38a7a

File tree

3 files changed

+157
-171
lines changed

3 files changed

+157
-171
lines changed

comtypes/__init__.py

+19-24
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,10 @@ class _cominterface_meta(type):
260260
methods from COMMETHOD lists.
261261
"""
262262

263-
if TYPE_CHECKING:
264-
_case_insensitive_: bool
265-
_iid_: GUID
266-
_methods_: List[_ComMemberSpec]
267-
_disp_methods_: List[_DispMemberSpec]
263+
_case_insensitive_: bool
264+
_iid_: GUID
265+
_methods_: List[_ComMemberSpec]
266+
_disp_methods_: List[_DispMemberSpec]
268267

269268
# This flag is set to True by the atexit handler which calls
270269
# CoUninitialize.
@@ -489,8 +488,7 @@ def _make_case_insensitive(self):
489488
d.update(getattr(self, "__map_case__", {}))
490489
self.__map_case__ = d
491490

492-
def _make_dispmethods(self, methods):
493-
# type: (List[_DispMemberSpec]) -> None
491+
def _make_dispmethods(self, methods: List[_DispMemberSpec]) -> None:
494492
if self._case_insensitive_:
495493
self._make_case_insensitive()
496494
# create dispinterface methods and properties on the interface 'self'
@@ -525,8 +523,7 @@ def __get_baseinterface_methodcount(self):
525523
raise TypeError("baseinterface '%s' has no _methods_" % itf.__name__)
526524
raise
527525

528-
def _make_methods(self, methods):
529-
# type: (List[_ComMemberSpec]) -> None
526+
def _make_methods(self, methods: List[_ComMemberSpec]) -> None:
530527
if self._case_insensitive_:
531528
self._make_case_insensitive()
532529
# register com interface. we insist on an _iid_ in THIS class!
@@ -808,22 +805,22 @@ class IUnknown(_IUnknown_Base, metaclass=_cominterface_meta):
808805
with STDMETHOD or COMMETHOD calls.
809806
"""
810807

811-
_case_insensitive_ = False # type: ClassVar[bool]
812-
_iid_ = GUID("{00000000-0000-0000-C000-000000000046}") # type: ClassVar[GUID]
813-
814-
_methods_ = [
808+
_case_insensitive_: ClassVar[bool] = False
809+
_iid_: ClassVar[GUID] = GUID("{00000000-0000-0000-C000-000000000046}")
810+
_methods_: ClassVar[List[_ComMemberSpec]] = [
815811
STDMETHOD(HRESULT, "QueryInterface", [POINTER(GUID), POINTER(c_void_p)]),
816812
STDMETHOD(c_ulong, "AddRef"),
817813
STDMETHOD(c_ulong, "Release"),
818-
] # type: ClassVar[List[_ComMemberSpec]]
814+
]
819815

820816
# NOTE: Why not `QueryInterface(T) -> _Pointer[T]`?
821817
# Any static type checkers is not able to provide members of `T` from `_Pointer[T]`,
822818
# regardless of the pointer is able to access members of contents in runtime.
823819
# And if `isinstance(p, POINTER(T))` is `True`, then `isinstance(p, T)` is also `True`.
824820
# So returning `T` is not a lie, and good way to know what members the class has.
825-
def QueryInterface(self, interface, iid=None):
826-
# type: (Type[_T_IUnknown], Optional[GUID]) -> _T_IUnknown
821+
def QueryInterface(
822+
self, interface: Type[_T_IUnknown], iid: Optional[GUID] = None
823+
) -> _T_IUnknown:
827824
"""QueryInterface(interface) -> instance"""
828825
p = POINTER(interface)()
829826
if iid is None:
@@ -836,13 +833,11 @@ def QueryInterface(self, interface, iid=None):
836833

837834
# these are only so that they get a docstring.
838835
# XXX There should be other ways to install a docstring.
839-
def AddRef(self):
840-
# type: () -> int
836+
def AddRef(self) -> int:
841837
"""Increase the internal refcount by one and return it."""
842838
return self.__com_AddRef()
843839

844-
def Release(self):
845-
# type: () -> int
840+
def Release(self) -> int:
846841
"""Decrease the internal refcount by one and return it."""
847842
return self.__com_Release()
848843

@@ -867,12 +862,12 @@ def GetClassID(self) -> GUID:
867862

868863
class IServiceProvider(IUnknown):
869864
_iid_ = GUID("{6D5140C1-7436-11CE-8034-00AA006009FA}")
870-
if TYPE_CHECKING:
871-
_QueryService: Callable[[Any, Any, Any], int]
865+
_QueryService: Callable[[Any, Any, Any], int]
872866
# Overridden QueryService to make it nicer to use (passing it an
873867
# interface and it returns a pointer to that interface)
874-
def QueryService(self, serviceIID, interface):
875-
# type: (GUID, Type[_T_IUnknown]) -> _T_IUnknown
868+
def QueryService(
869+
self, serviceIID: GUID, interface: Type[_T_IUnknown]
870+
) -> _T_IUnknown:
876871
p = POINTER(interface)()
877872
self._QueryService(byref(serviceIID), byref(interface._iid_), byref(p))
878873
return p # type: ignore

comtypes/automation.py

+23-31
Original file line numberDiff line numberDiff line change
@@ -749,32 +749,28 @@ def __del__(self):
749749
DISPID_COLLECT = -8
750750

751751

752-
if TYPE_CHECKING:
753-
RawGetIDsOfNamesFunc = Callable[
754-
[_byref_type, Array[c_wchar_p], int, int, Array[DISPID]],
755-
int,
756-
]
757-
RawInvokeFunc = Callable[
758-
[
759-
int,
760-
_byref_type,
761-
int,
762-
int, # dispIdMember, riid, lcid, wFlags
763-
_UnionT[_byref_type, DISPPARAMS], # *pDispParams
764-
_UnionT[_byref_type, VARIANT], # pVarResult
765-
_UnionT[_byref_type, EXCEPINFO, None], # pExcepInfo
766-
_UnionT[_byref_type, c_uint], # puArgErr
767-
],
768-
int,
769-
]
752+
RawGetIDsOfNamesFunc = Callable[
753+
[_byref_type, "Array[c_wchar_p]", int, int, "Array[DISPID]"], int
754+
]
755+
# fmt: off
756+
RawInvokeFunc = Callable[
757+
[
758+
int, _byref_type, int, int, # dispIdMember, riid, lcid, wFlags
759+
_UnionT[_byref_type, DISPPARAMS], # *pDispParams
760+
_UnionT[_byref_type, VARIANT], # pVarResult
761+
_UnionT[_byref_type, EXCEPINFO, None], # pExcepInfo
762+
_UnionT[_byref_type, c_uint], # puArgErr
763+
],
764+
int,
765+
]
766+
# fmt: on
770767

771768

772769
class IDispatch(IUnknown):
773-
if TYPE_CHECKING:
774-
_disp_methods_: ClassVar[List[comtypes._DispMemberSpec]]
775-
_GetTypeInfo: Callable[[int, int], IUnknown]
776-
__com_GetIDsOfNames: RawGetIDsOfNamesFunc
777-
__com_Invoke: RawInvokeFunc
770+
_disp_methods_: ClassVar[List[comtypes._DispMemberSpec]]
771+
_GetTypeInfo: Callable[[int, int], IUnknown]
772+
__com_GetIDsOfNames: RawGetIDsOfNamesFunc
773+
__com_Invoke: RawInvokeFunc
778774

779775
_iid_ = GUID("{00020400-0000-0000-C000-000000000046}")
780776
_methods_ = [
@@ -811,16 +807,14 @@ class IDispatch(IUnknown):
811807
),
812808
]
813809

814-
def GetTypeInfo(self, index, lcid=0):
815-
# type: (int, int) -> hints.ITypeInfo
810+
def GetTypeInfo(self, index: int, lcid: int = 0) -> "hints.ITypeInfo":
816811
"""Return type information. Index 0 specifies typeinfo for IDispatch"""
817812
import comtypes.typeinfo
818813

819814
result = self._GetTypeInfo(index, lcid)
820815
return result.QueryInterface(comtypes.typeinfo.ITypeInfo)
821816

822-
def GetIDsOfNames(self, *names, **kw):
823-
# type: (str, Any) -> List[int]
817+
def GetIDsOfNames(self, *names: str, **kw: Any) -> List[int]:
824818
"""Map string names to integer ids."""
825819
lcid = kw.pop("lcid", 0)
826820
assert not kw
@@ -829,8 +823,7 @@ def GetIDsOfNames(self, *names, **kw):
829823
self.__com_GetIDsOfNames(riid_null, arr, len(names), lcid, ids)
830824
return ids[:]
831825

832-
def _invoke(self, memid, invkind, lcid, *args):
833-
# type: (int, int, int, Any) -> Any
826+
def _invoke(self, memid: int, invkind: int, lcid: int, *args: Any) -> Any:
834827
var = VARIANT()
835828
argerr = c_uint()
836829
dp = DISPPARAMS()
@@ -850,8 +843,7 @@ def _invoke(self, memid, invkind, lcid, *args):
850843
self.__com_Invoke(memid, riid_null, lcid, invkind, dp, var, None, argerr)
851844
return var._get_value(dynamic=True)
852845

853-
def Invoke(self, dispid, *args, **kw):
854-
# type: (int, Any, Any) -> Any
846+
def Invoke(self, dispid: int, *args: Any, **kw: Any) -> Any:
855847
"""Invoke a method or property."""
856848

857849
# Memory management in Dispatch::Invoke calls:

0 commit comments

Comments
 (0)