Skip to content

Commit f62fbc0

Browse files
authored
Modernize IGlobalInterfaceTable with type hints. (#941)
* refactor: Fix linting issues in `comtypes/git.py` and remove lint ignores. - Replace wildcard `ctypes` import with explicit `POINTER` import. - Remove unused `COMMETHOD` import. - Remove `comtypes/git.py` from `per-file-ignores` in `pyproject.toml`. * refactor: Remove redundant `if __name__ == "__main__":` block from `comtypes/git.py`. The manual test code in the main block is no longer needed as the functionality is covered by other tests or was intended only for local verification. * fix: Add type ignores to `IGlobalInterfaceTable` methods. Add `# type: ignore` to `RegisterInterfaceInGlobal`, `GetInterfaceFromGlobal`, and `RevokeInterfaceFromGlobal` in `IGlobalInterfaceTable` to suppress type checking errors for generated `__com_*` methods. * refactor: Add type hints to `IGlobalInterfaceTable` methods. - Use `TypeVar` `_T_IUnknown` to define generic interface types. - Add type hints to `RegisterInterfaceInGlobal`, `GetInterfaceFromGlobal`, and `RevokeInterfaceFromGlobal`. - Update method signatures to use `_T_IUnknown` for improved type safety.
1 parent 74300b2 commit f62fbc0

2 files changed

Lines changed: 15 additions & 27 deletions

File tree

comtypes/git.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
between different threading appartments.
55
"""
66

7-
from ctypes import *
7+
from ctypes import POINTER
88
from ctypes.wintypes import DWORD
9+
from typing import Type, TypeVar
910

1011
from comtypes import (
1112
CLSCTX_INPROC_SERVER,
12-
COMMETHOD,
1313
GUID,
1414
HRESULT,
1515
STDMETHOD,
1616
CoCreateInstance,
1717
IUnknown,
1818
)
1919

20+
_T_IUnknown = TypeVar("_T_IUnknown", bound=IUnknown)
21+
2022

2123
class IGlobalInterfaceTable(IUnknown):
2224
_iid_ = GUID("{00000146-0000-0000-C000-000000000046}")
@@ -34,18 +36,22 @@ class IGlobalInterfaceTable(IUnknown):
3436
),
3537
]
3638

37-
def RegisterInterfaceInGlobal(self, obj, interface=IUnknown):
39+
def RegisterInterfaceInGlobal(
40+
self, obj: IUnknown, interface: Type[_T_IUnknown] = IUnknown
41+
) -> int:
3842
cookie = DWORD()
39-
self.__com_RegisterInterfaceInGlobal(obj, interface._iid_, cookie)
43+
self.__com_RegisterInterfaceInGlobal(obj, interface._iid_, cookie) # type: ignore
4044
return cookie.value
4145

42-
def GetInterfaceFromGlobal(self, cookie, interface=IUnknown):
46+
def GetInterfaceFromGlobal(
47+
self, cookie: int, interface: Type[_T_IUnknown] = IUnknown
48+
) -> _T_IUnknown:
4349
ptr = POINTER(interface)()
44-
self.__com_GetInterfaceFromGlobal(cookie, interface._iid_, ptr)
45-
return ptr
50+
self.__com_GetInterfaceFromGlobal(cookie, interface._iid_, ptr) # type: ignore
51+
return ptr # type: ignore
4652

47-
def RevokeInterfaceFromGlobal(self, cookie):
48-
self.__com_RevokeInterfaceFromGlobal(cookie)
53+
def RevokeInterfaceFromGlobal(self, cookie: int) -> None:
54+
self.__com_RevokeInterfaceFromGlobal(cookie) # type: ignore
4955

5056

5157
# It was a pain to get this CLSID: it's neither in the registry, nor
@@ -69,20 +75,3 @@ def RevokeInterfaceFromGlobal(self, cookie):
6975
"GetInterfaceFromGlobal",
7076
]
7177
# fmt: on
72-
73-
74-
if __name__ == "__main__":
75-
from comtypes.typeinfo import CreateTypeLib, ICreateTypeLib
76-
77-
tlib = CreateTypeLib("foo.bar") # we don not save it later
78-
assert (tlib.AddRef(), tlib.Release()) == (2, 1)
79-
80-
cookie = RegisterInterfaceInGlobal(tlib)
81-
assert (tlib.AddRef(), tlib.Release()) == (3, 2)
82-
83-
GetInterfaceFromGlobal(cookie, ICreateTypeLib)
84-
GetInterfaceFromGlobal(cookie, ICreateTypeLib)
85-
GetInterfaceFromGlobal(cookie)
86-
assert (tlib.AddRef(), tlib.Release()) == (3, 2)
87-
RevokeInterfaceFromGlobal(cookie)
88-
assert (tlib.AddRef(), tlib.Release()) == (2, 1)

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ ignore = ["E402"]
6767
"comtypes/_npsupport.py" = ["F401"]
6868
"comtypes/_vtbl.py" = ["E722"]
6969
"comtypes/automation.py" = ["F401", "F403", "F405"]
70-
"comtypes/git.py" = ["F401", "F403", "F405"]
7170
"comtypes/viewobject.py" = ["F403", "F405"]
7271
"comtypes/client/_constants.py" = ["F401"]
7372
"comtypes/server/automation.py" = ["F403", "F405"]

0 commit comments

Comments
 (0)