Skip to content

Commit 18dc2cc

Browse files
authored
change type hints from comment annotations to inline annotations in comtypes/client/... (#456)
* change type hints from comment annotations to inline annotations in `comtypes/client/...` * fix type annotations
1 parent 5d230aa commit 18dc2cc

File tree

2 files changed

+68
-74
lines changed

2 files changed

+68
-74
lines changed

comtypes/client/__init__.py

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
logger = logging.getLogger(__name__)
4545

4646

47-
def wrap_outparam(punk):
48-
# type: (Any) -> Any
47+
def wrap_outparam(punk: Any) -> Any:
4948
logger.debug("wrap_outparam(%s)", punk)
5049
if not punk:
5150
return None
@@ -54,8 +53,7 @@ def wrap_outparam(punk):
5453
return punk
5554

5655

57-
def GetBestInterface(punk):
58-
# type: (Any) -> Any
56+
def GetBestInterface(punk: Any) -> Any:
5957
"""Try to QueryInterface a COM pointer to the 'most useful'
6058
interface.
6159
@@ -151,21 +149,23 @@ def GetBestInterface(punk):
151149
#
152150
# Object creation
153151
#
154-
if comtypes.TYPE_CHECKING:
152+
@overload
153+
def GetActiveObject(progid: _UnionT[str, CoClass, GUID]) -> Any:
154+
...
155155

156-
@overload
157-
def GetActiveObject(progid):
158-
# type: (_UnionT[str, CoClass, GUID]) -> Any
159-
pass
160156

161-
@overload
162-
def GetActiveObject(progid, interface):
163-
# type: (_UnionT[str, CoClass, GUID], Type[_T_IUnknown]) -> _T_IUnknown
164-
pass
157+
@overload
158+
def GetActiveObject(
159+
progid: _UnionT[str, CoClass, GUID], interface: Type[_T_IUnknown]
160+
) -> _T_IUnknown:
161+
...
165162

166163

167-
def GetActiveObject(progid, interface=None, dynamic=False):
168-
# type: (_UnionT[str, CoClass, GUID], Optional[Any], bool) -> Any
164+
def GetActiveObject(
165+
progid: _UnionT[str, CoClass, GUID],
166+
interface: Optional[Type[IUnknown]] = None,
167+
dynamic: bool = False,
168+
) -> Any:
169169
"""Return a pointer to a running COM object that has been
170170
registered with COM.
171171
@@ -188,8 +188,9 @@ def GetActiveObject(progid, interface=None, dynamic=False):
188188
return _manage(obj, clsid, interface=interface)
189189

190190

191-
def _manage(obj, clsid, interface):
192-
# type: (Any, Optional[GUID], Optional[Type[IUnknown]]) -> Any
191+
def _manage(
192+
obj: Any, clsid: Optional[GUID], interface: Optional[Type[IUnknown]]
193+
) -> Any:
193194
obj.__dict__["__clsid"] = str(clsid)
194195
if interface is None:
195196
obj = GetBestInterface(obj)
@@ -221,35 +222,33 @@ def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
221222
return comtypes.CoGetClassObject(clsid, clsctx, pServerInfo, interface)
222223

223224

224-
if TYPE_CHECKING:
225+
@overload
226+
def CreateObject(progid: _UnionT[str, CoClass, GUID]) -> Any:
227+
...
225228

226-
@overload
227-
def CreateObject(progid):
228-
# type: (_UnionT[str, CoClass, GUID]) -> Any
229-
pass
230229

231-
@overload
232-
def CreateObject(
233-
progid,
234-
clsctx=None,
235-
machine=None,
236-
interface=None,
237-
dynamic=False,
238-
pServerInfo=None,
239-
):
240-
# type: (_UnionT[str, CoClass, GUID], Optional[int], Optional[str], Optional[Type[_T_IUnknown]], bool, Optional[comtypes.COSERVERINFO]) -> _T_IUnknown
241-
pass
230+
@overload
231+
def CreateObject(
232+
progid: _UnionT[str, CoClass, GUID],
233+
clsctx: Optional[int] = None,
234+
machine: Optional[str] = None,
235+
interface: Optional[Type[_T_IUnknown]] = None,
236+
dynamic: bool = ...,
237+
pServerInfo: Optional[comtypes.COSERVERINFO] = None,
238+
) -> _T_IUnknown:
239+
...
242240

243241

244242
def CreateObject(
245-
progid, # which object to create
246-
clsctx=None, # how to create the object
247-
machine=None, # where to create the object
248-
interface=None, # the interface we want
249-
dynamic=False, # use dynamic dispatch
250-
pServerInfo=None, # server info struct for remoting
251-
):
252-
# type: (_UnionT[str, CoClass, GUID], Optional[int], Optional[str], Optional[Type[IUnknown]], bool, Optional[comtypes.COSERVERINFO]) -> Any
243+
progid: _UnionT[str, CoClass, GUID], # which object to create
244+
clsctx: Optional[int] = None, # how to create the object
245+
machine: Optional[str] = None, # where to create the object
246+
interface: Optional[Type[IUnknown]] = None, # the interface we want
247+
dynamic: bool = False, # use dynamic dispatch
248+
pServerInfo: Optional[
249+
comtypes.COSERVERINFO
250+
] = None, # server info struct for remoting
251+
) -> Any:
253252
"""Create a COM object from 'progid', and try to QueryInterface()
254253
it to the most useful interface, generating typelib support on
255254
demand. A pointer to this interface is returned.
@@ -304,21 +303,21 @@ def CreateObject(
304303
return _manage(obj, clsid, interface=interface)
305304

306305

307-
if TYPE_CHECKING:
306+
@overload
307+
def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown:
308+
...
308309

309-
@overload
310-
def CoGetObject(displayname, interface):
311-
# type: (str, Type[_T_IUnknown]) -> _T_IUnknown
312-
pass
313310

314-
@overload
315-
def CoGetObject(displayname, interface=None, dynamic=False):
316-
# type: (str, None, bool) -> Any
317-
pass
311+
@overload
312+
def CoGetObject(displayname: str, interface: None = None, dynamic: bool = False) -> Any:
313+
...
318314

319315

320-
def CoGetObject(displayname, interface=None, dynamic=False):
321-
# type: (str, Optional[Type[comtypes.IUnknown]], bool) -> Any
316+
def CoGetObject(
317+
displayname: str,
318+
interface: Optional[Type[comtypes.IUnknown]] = None,
319+
dynamic: bool = False,
320+
) -> Any:
322321
"""Create an object by calling CoGetObject(displayname).
323322
324323
Additional parameters have the same meaning as in CreateObject().

comtypes/client/_generate.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
PATH = os.environ["PATH"].split(os.pathsep)
1919

2020

21-
def _my_import(fullname):
22-
# type: (str) -> types.ModuleType
21+
def _my_import(fullname: str) -> types.ModuleType:
2322
"""helper function to import dotted modules"""
2423
import comtypes.gen as g
2524

@@ -28,8 +27,7 @@ def _my_import(fullname):
2827
return importlib.import_module(fullname)
2928

3029

31-
def _resolve_filename(tlib_string, dirpath):
32-
# type: (str, str) -> Tuple[str, bool]
30+
def _resolve_filename(tlib_string: str, dirpath: str) -> Tuple[str, bool]:
3331
"""Tries to make sense of a type library specified as a string.
3432
3533
Args:
@@ -58,8 +56,7 @@ def _resolve_filename(tlib_string, dirpath):
5856
return tlib_string, False
5957

6058

61-
def GetModule(tlib):
62-
# type: (_UnionT[Any, typeinfo.ITypeLib]) -> types.ModuleType
59+
def GetModule(tlib: _UnionT[Any, typeinfo.ITypeLib]) -> types.ModuleType:
6360
"""Create a module wrapping a COM typelibrary on demand.
6461
6562
'tlib' must be ...
@@ -106,9 +103,9 @@ def GetModule(tlib):
106103
# if a relative pathname is used, we try to interpret it relative to
107104
# the directory of the calling module (if not from command line)
108105
frame = sys._getframe(1)
109-
_file_ = frame.f_globals.get("__file__", None) # type: str
106+
_file_: Optional[str] = frame.f_globals.get("__file__", None)
110107
pathname, is_abs = _resolve_filename(
111-
tlib_string, _file_ and os.path.dirname(_file_)
108+
tlib_string, _file_ and os.path.dirname(_file_) # type: ignore
112109
)
113110
logger.debug("GetModule(%s), resolved: %s", pathname, is_abs)
114111
tlib = _load_tlib(pathname) # don't register
@@ -134,8 +131,7 @@ def GetModule(tlib):
134131
return _create_friendly_module(tlib, modulename)
135132

136133

137-
def _load_tlib(obj):
138-
# type: (Any) -> typeinfo.ITypeLib
134+
def _load_tlib(obj: Any) -> typeinfo.ITypeLib:
139135
"""Load a pointer of ITypeLib on demand."""
140136
# obj is a filepath or a ProgID
141137
if isinstance(obj, str):
@@ -171,8 +167,7 @@ def _load_tlib(obj):
171167
raise TypeError("'%r' is not supported type for loading typelib" % obj)
172168

173169

174-
def _create_module_in_file(modulename, code):
175-
# type: (str, str) -> types.ModuleType
170+
def _create_module_in_file(modulename: str, code: str) -> types.ModuleType:
176171
"""create module in file system, and import it"""
177172
# `modulename` is 'comtypes.gen.xxx'
178173
filename = "%s.py" % modulename.split(".")[-1]
@@ -184,8 +179,7 @@ def _create_module_in_file(modulename, code):
184179
return _my_import(modulename)
185180

186181

187-
def _create_module_in_memory(modulename, code):
188-
# type: (str, str) -> types.ModuleType
182+
def _create_module_in_memory(modulename: str, code: str) -> types.ModuleType:
189183
"""create module in memory system, and import it"""
190184
# `modulename` is 'comtypes.gen.xxx'
191185
import comtypes.gen as g
@@ -199,8 +193,9 @@ def _create_module_in_memory(modulename, code):
199193
return mod
200194

201195

202-
def _create_friendly_module(tlib, modulename):
203-
# type: (typeinfo.ITypeLib, str) -> types.ModuleType
196+
def _create_friendly_module(
197+
tlib: typeinfo.ITypeLib, modulename: str
198+
) -> types.ModuleType:
204199
"""helper which creates and imports the friendly-named module."""
205200
try:
206201
mod = _my_import(modulename)
@@ -220,8 +215,9 @@ def _create_friendly_module(tlib, modulename):
220215
return _create_module_in_file(modulename, code)
221216

222217

223-
def _create_wrapper_module(tlib, pathname):
224-
# type: (typeinfo.ITypeLib, Optional[str]) -> types.ModuleType
218+
def _create_wrapper_module(
219+
tlib: typeinfo.ITypeLib, pathname: Optional[str]
220+
) -> types.ModuleType:
225221
"""helper which creates and imports the real typelib wrapper module."""
226222
modulename = codegenerator.name_wrapper_module(tlib)
227223
if modulename in sys.modules:
@@ -245,9 +241,8 @@ def _create_wrapper_module(tlib, pathname):
245241
return _create_module_in_file(modulename, code)
246242

247243

248-
def _get_known_symbols():
249-
# type: () -> Dict[str, str]
250-
known_symbols = {} # type: Dict[str, str]
244+
def _get_known_symbols() -> Dict[str, str]:
245+
known_symbols: Dict[str, str] = {}
251246
for mod_name in (
252247
"comtypes.persist",
253248
"comtypes.typeinfo",
@@ -258,7 +253,7 @@ def _get_known_symbols():
258253
):
259254
mod = importlib.import_module(mod_name)
260255
if hasattr(mod, "__known_symbols__"):
261-
names = mod.__known_symbols__ # type: List[str]
256+
names: List[str] = mod.__known_symbols__
262257
else:
263258
names = list(mod.__dict__)
264259
for name in names:

0 commit comments

Comments
 (0)