Skip to content

Commit ab2b95e

Browse files
authored
Update tools/**/*.py, server/*.py and client/*.py files for PEP585 compliance. (#879)
* Update `comtypes/client/_activeobj.py` for PEP585 compliance. * Update `comtypes/client/_create.py` for PEP585 compliance. * Update `comtypes/client/_events.py` for PEP585 compliance. * Update `comtypes/client/_generate.py` for PEP585 compliance. * Update `comtypes/client/_managing.py` for PEP585 compliance. * Update `comtypes/client/dynamic.py` for PEP585 compliance. * Update `comtypes/server/__init__.py` for PEP585 compliance. * Update `comtypes/server/connectionpoints.py` for PEP585 compliance. * Update `comtypes/server/inprocserver.py` for PEP585 compliance. * Update `comtypes/server/localserver.py` for PEP585 compliance. * Update `comtypes/server/register.py` for PEP585 compliance. * Update `comtypes/server/w_getopt.py` for PEP585 compliance. * Update `comtypes/tools/codegenerator/codegenerator.py` for PEP585 compliance. * Update `comtypes/tools/codegenerator/heads.py` for PEP585 compliance. * Update `comtypes/tools/codegenerator/helpers.py` for PEP585 compliance. * Update `comtypes/tools/codegenerator/namespaces.py` for PEP585 compliance. * Update `comtypes/tools/codegenerator/typeannotator.py` for PEP585 compliance. * Update `comtypes/tools/tlbparser.py` for PEP585 compliance. * Update `comtypes/tools/typedesc.py` for PEP585 compliance. * Update `comtypes/tools/typedesc_base.py` for PEP585 compliance.
1 parent 4134455 commit ab2b95e

20 files changed

+174
-185
lines changed

comtypes/client/_activeobj.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Optional, Type, TypeVar, overload
1+
from typing import Any, Optional, TypeVar, overload
22
from typing import Union as _UnionT
33

44
import comtypes
@@ -14,14 +14,14 @@
1414
# Object creation
1515
#
1616
@overload
17-
def GetActiveObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: ...
17+
def GetActiveObject(progid: _UnionT[str, type[CoClass], GUID]) -> Any: ...
1818
@overload
1919
def GetActiveObject(
20-
progid: _UnionT[str, Type[CoClass], GUID], interface: Type[_T_IUnknown]
20+
progid: _UnionT[str, type[CoClass], GUID], interface: type[_T_IUnknown]
2121
) -> _T_IUnknown: ...
2222
def GetActiveObject(
23-
progid: _UnionT[str, Type[CoClass], GUID],
24-
interface: Optional[Type[IUnknown]] = None,
23+
progid: _UnionT[str, type[CoClass], GUID],
24+
interface: Optional[type[IUnknown]] = None,
2525
dynamic: bool = False,
2626
) -> Any:
2727
"""Return a pointer to a running COM object that has been
@@ -47,7 +47,7 @@ def GetActiveObject(
4747

4848

4949
def RegisterActiveObject(
50-
punk: IUnknown, progid: _UnionT[str, Type[CoClass], GUID], weak: bool = True
50+
punk: IUnknown, progid: _UnionT[str, type[CoClass], GUID], weak: bool = True
5151
) -> int:
5252
clsid = GUID.from_progid(progid)
5353
flags = comtypes.ACTIVEOBJECT_WEAK if weak else comtypes.ACTIVEOBJECT_STRONG

comtypes/client/_create.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar, overload
2+
from typing import TYPE_CHECKING, Any, Optional, TypeVar, overload
33
from typing import Union as _UnionT
44

55
import comtypes
@@ -23,22 +23,22 @@
2323

2424
@overload
2525
def GetClassObject(
26-
progid: _UnionT[str, Type[CoClass], GUID],
26+
progid: _UnionT[str, type[CoClass], GUID],
2727
clsctx: Optional[int] = None,
2828
pServerInfo: Optional[COSERVERINFO] = None,
2929
interface: None = None,
3030
) -> hints.IClassFactory: ...
3131
@overload
3232
def GetClassObject(
33-
progid: _UnionT[str, Type[CoClass], GUID],
33+
progid: _UnionT[str, type[CoClass], GUID],
3434
clsctx: Optional[int] = None,
3535
pServerInfo: Optional[COSERVERINFO] = None,
36-
interface: Type[_T_IUnknown] = hints.IClassFactory,
36+
interface: type[_T_IUnknown] = hints.IClassFactory,
3737
) -> _T_IUnknown: ...
3838

3939

4040
def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
41-
# type: (_UnionT[str, Type[CoClass], GUID], Optional[int], Optional[COSERVERINFO], Optional[Type[IUnknown]]) -> IUnknown
41+
# type: (_UnionT[str, type[CoClass], GUID], Optional[int], Optional[COSERVERINFO], Optional[type[IUnknown]]) -> IUnknown
4242
"""Create and return the class factory for a COM object.
4343
4444
'clsctx' specifies how to create the object, use the CLSCTX_... constants.
@@ -50,21 +50,21 @@ def GetClassObject(progid, clsctx=None, pServerInfo=None, interface=None):
5050

5151

5252
@overload
53-
def CreateObject(progid: _UnionT[str, Type[CoClass], GUID]) -> Any: ...
53+
def CreateObject(progid: _UnionT[str, type[CoClass], GUID]) -> Any: ...
5454
@overload
5555
def CreateObject(
56-
progid: _UnionT[str, Type[CoClass], GUID],
56+
progid: _UnionT[str, type[CoClass], GUID],
5757
clsctx: Optional[int] = None,
5858
machine: Optional[str] = None,
59-
interface: Optional[Type[_T_IUnknown]] = None,
59+
interface: Optional[type[_T_IUnknown]] = None,
6060
dynamic: bool = ...,
6161
pServerInfo: Optional[COSERVERINFO] = None,
6262
) -> _T_IUnknown: ...
6363
def CreateObject(
64-
progid: _UnionT[str, Type[CoClass], GUID], # which object to create
64+
progid: _UnionT[str, type[CoClass], GUID], # which object to create
6565
clsctx: Optional[int] = None, # how to create the object
6666
machine: Optional[str] = None, # where to create the object
67-
interface: Optional[Type[IUnknown]] = None, # the interface we want
67+
interface: Optional[type[IUnknown]] = None, # the interface we want
6868
dynamic: bool = False, # use dynamic dispatch
6969
pServerInfo: Optional[COSERVERINFO] = None, # server info struct for remoting
7070
) -> Any:
@@ -123,14 +123,14 @@ def CreateObject(
123123

124124

125125
@overload
126-
def CoGetObject(displayname: str, interface: Type[_T_IUnknown]) -> _T_IUnknown: ...
126+
def CoGetObject(displayname: str, interface: type[_T_IUnknown]) -> _T_IUnknown: ...
127127
@overload
128128
def CoGetObject(
129129
displayname: str, interface: None = None, dynamic: bool = False
130130
) -> Any: ...
131131
def CoGetObject(
132132
displayname: str,
133-
interface: Optional[Type[IUnknown]] = None,
133+
interface: Optional[type[IUnknown]] = None,
134134
dynamic: bool = False,
135135
) -> Any:
136136
"""Create an object by calling CoGetObject(displayname).

comtypes/client/_events.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import traceback
44
from _ctypes import COMError
5+
from collections.abc import Callable
56
from ctypes import HRESULT, POINTER, WINFUNCTYPE, OleDLL, Structure, WinDLL, byref
67
from ctypes.wintypes import (
78
BOOL,
@@ -13,7 +14,7 @@
1314
LPVOID,
1415
ULONG,
1516
)
16-
from typing import Any, Callable, Optional, Type
17+
from typing import Any, Optional
1718
from typing import Union as _UnionT
1819

1920
import comtypes
@@ -70,7 +71,7 @@ class _AdviseConnection:
7071
receiver: Optional[_ReceiverType]
7172

7273
def __init__(
73-
self, source: IUnknown, interface: Type[IUnknown], receiver: _ReceiverType
74+
self, source: IUnknown, interface: type[IUnknown], receiver: _ReceiverType
7475
) -> None:
7576
# Pre-initializing attributes to avoid AttributeError after failed connection.
7677
self.cp = None
@@ -79,7 +80,7 @@ def __init__(
7980
self._connect(source, interface, receiver)
8081

8182
def _connect(
82-
self, source: IUnknown, interface: Type[IUnknown], receiver: _ReceiverType
83+
self, source: IUnknown, interface: type[IUnknown], receiver: _ReceiverType
8384
) -> None:
8485
cpc = source.QueryInterface(IConnectionPointContainer)
8586
self.cp = cpc.FindConnectionPoint(byref(interface._iid_))
@@ -109,7 +110,7 @@ def __del__(self) -> None:
109110
pass
110111

111112

112-
def FindOutgoingInterface(source: IUnknown) -> Type[IUnknown]:
113+
def FindOutgoingInterface(source: IUnknown) -> type[IUnknown]:
113114
"""XXX Describe the strategy that is used..."""
114115
# If the COM object implements IProvideClassInfo2, it is easy to
115116
# find the default outgoing interface.
@@ -237,11 +238,11 @@ def _find_method(self, fq_name: str, mthname: str) -> Callable[..., Any]:
237238
return getattr(self.sink, mthname)
238239

239240

240-
def CreateEventReceiver(interface: Type[IUnknown], handler: Any) -> COMObject:
241+
def CreateEventReceiver(interface: type[IUnknown], handler: Any) -> COMObject:
241242
class Sink(COMObject):
242243
_com_interfaces_ = [interface]
243244

244-
def _get_method_finder_(self, itf: Type[IUnknown]) -> _MethodFinder:
245+
def _get_method_finder_(self, itf: type[IUnknown]) -> _MethodFinder:
245246
# Use a special MethodFinder that will first try 'self',
246247
# then the sink.
247248
return _SinkMethodFinder(self, handler)
@@ -267,7 +268,7 @@ def _get_method_finder_(self, itf: Type[IUnknown]) -> _MethodFinder:
267268

268269

269270
def GetEvents(
270-
source: IUnknown, sink: Any, interface: Optional[Type[IUnknown]] = None
271+
source: IUnknown, sink: Any, interface: Optional[type[IUnknown]] = None
271272
) -> _AdviseConnection:
272273
"""Receive COM events from 'source'. Events will call methods on
273274
the 'sink' object. 'interface' is the source interface to use.
@@ -301,7 +302,7 @@ def handler(self, this, *args, **kw):
301302

302303

303304
def ShowEvents(
304-
source: IUnknown, interface: Optional[Type[IUnknown]] = None
305+
source: IUnknown, interface: Optional[type[IUnknown]] = None
305306
) -> _AdviseConnection:
306307
"""Receive COM events from 'source'. A special event sink will be
307308
used that first prints the names of events that are found in the

comtypes/client/_generate.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import sys
77
import types
88
import winreg
9-
from typing import Any, Dict, List, Mapping, Optional, Tuple
9+
from collections.abc import Mapping
10+
from typing import Any, Optional
1011
from typing import Union as _UnionT
1112

1213
import comtypes.client
@@ -25,7 +26,7 @@ def _my_import(fullname: str) -> types.ModuleType:
2526
return importlib.import_module(fullname)
2627

2728

28-
def _resolve_filename(tlib_string: str, dirpath: str) -> Tuple[str, bool]:
29+
def _resolve_filename(tlib_string: str, dirpath: str) -> tuple[str, bool]:
2930
"""Tries to make sense of a type library specified as a string.
3031
3132
Args:
@@ -225,7 +226,7 @@ def generate(self) -> types.ModuleType:
225226
"""Generates wrapper and friendly modules."""
226227
known_symbols, known_interfaces = _get_known_namespaces()
227228
codegen = codegenerator.CodeGenerator(known_symbols, known_interfaces)
228-
codebases: List[Tuple[str, str]] = []
229+
codebases: list[tuple[str, str]] = []
229230
logger.info("# Generating %s", self.wrapper_name)
230231
items = list(tlbparser.TypeLibParser(self.tlib).parse().values())
231232
wrp_code = codegen.generate_wrapper_code(items, filename=self.pathname)
@@ -246,7 +247,7 @@ def generate(self) -> types.ModuleType:
246247

247248

248249
def _get_known_namespaces() -> (
249-
Tuple[Mapping[_SymbolName, _ModuleName], Mapping[_ItfName, _ItfIid]]
250+
tuple[Mapping[_SymbolName, _ModuleName], Mapping[_ItfName, _ItfIid]]
250251
):
251252
"""Returns symbols and interfaces that are already statically defined in `ctypes`
252253
and `comtypes`.
@@ -260,8 +261,8 @@ def _get_known_namespaces() -> (
260261
`comtypes` does NOT aim to statically define all COM object interfaces in
261262
its repository.
262263
"""
263-
known_symbols: Dict[_SymbolName, _ModuleName] = {}
264-
known_interfaces: Dict[_ItfName, _ItfIid] = {}
264+
known_symbols: dict[_SymbolName, _ModuleName] = {}
265+
known_interfaces: dict[_ItfName, _ItfIid] = {}
265266
for mod_name in (
266267
"comtypes.persist",
267268
"comtypes.typeinfo",
@@ -273,7 +274,7 @@ def _get_known_namespaces() -> (
273274
):
274275
mod = importlib.import_module(mod_name)
275276
if hasattr(mod, "__known_symbols__"):
276-
names: List[str] = mod.__known_symbols__
277+
names: list[str] = mod.__known_symbols__
277278
for name in names:
278279
tgt = getattr(mod, name)
279280
if inspect.isclass(tgt) and issubclass(tgt, comtypes.IUnknown):

comtypes/client/_managing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from _ctypes import COMError
3-
from typing import Any, Optional, Type
3+
from typing import Any, Optional
44

55
import comtypes
66
import comtypes.client.dynamic
@@ -106,7 +106,7 @@ def GetBestInterface(punk: Any) -> Any:
106106

107107

108108
def _manage(
109-
obj: Any, clsid: Optional[GUID], interface: Optional[Type[IUnknown]]
109+
obj: Any, clsid: Optional[GUID], interface: Optional[type[IUnknown]]
110110
) -> Any:
111111
obj.__dict__["__clsid"] = str(clsid)
112112
if interface is None:

comtypes/client/dynamic.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ctypes
2-
from typing import Any, Dict, Optional, Set, Type, TypeVar
2+
from typing import Any, Optional, TypeVar
33

44
from comtypes import GUID, COMError, IUnknown, _is_object, automation
55
from comtypes import hresult as hres
@@ -63,8 +63,8 @@ class _Dispatch:
6363
"""Expose methods and properties via fully dynamic dispatch."""
6464

6565
_comobj: automation.IDispatch
66-
_ids: Dict[str, int]
67-
_methods: Set[str]
66+
_ids: dict[str, int]
67+
_methods: set[str]
6868

6969
def __init__(self, comobj: "ctypes._Pointer[automation.IDispatch]"):
7070
self.__dict__["_comobj"] = comobj
@@ -90,7 +90,7 @@ def __getitem__(self, index: Any) -> Any:
9090
return item
9191

9292
def QueryInterface(
93-
self, interface: Type[_T_IUnknown], iid: Optional[GUID] = None
93+
self, interface: type[_T_IUnknown], iid: Optional[GUID] = None
9494
) -> _T_IUnknown:
9595
"""QueryInterface is forwarded to the real com object."""
9696
return self._comobj.QueryInterface(interface, iid)

comtypes/server/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ctypes
22
from ctypes import HRESULT, POINTER, byref
3-
from typing import TYPE_CHECKING, Any, Optional, Type
3+
from typing import TYPE_CHECKING, Any, Optional
44

55
import comtypes
66
import comtypes.client
@@ -30,8 +30,8 @@ class IClassFactory(IUnknown):
3030

3131
def CreateInstance(
3232
self,
33-
punkouter: Optional[Type["_Pointer[IUnknown]"]] = None,
34-
interface: Optional[Type[IUnknown]] = None,
33+
punkouter: Optional[type["_Pointer[IUnknown]"]] = None,
34+
interface: Optional[type[IUnknown]] = None,
3535
dynamic: bool = False,
3636
) -> Any:
3737
if dynamic:

comtypes/server/connectionpoints.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import functools
22
import logging
33
from _ctypes import COMError
4+
from collections.abc import Callable, Iterator
45
from ctypes import c_void_p, pointer
56
from ctypes.wintypes import DWORD
6-
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterator, List, Tuple, Type
7+
from typing import TYPE_CHECKING, Any
78
from typing import Union as _UnionT
89

910
from comtypes import GUID, COMObject, IUnknown
@@ -29,10 +30,10 @@ class ConnectionPointImpl(COMObject):
2930
_com_interfaces_ = [IConnectionPoint]
3031

3132
def __init__(
32-
self, sink_interface: Type[IUnknown], sink_typeinfo: ITypeInfo
33+
self, sink_interface: type[IUnknown], sink_typeinfo: ITypeInfo
3334
) -> None:
3435
super().__init__()
35-
self._connections: Dict[int, IUnknown] = {}
36+
self._connections: dict[int, IUnknown] = {}
3637
self._cookie = 0
3738
self._sink_interface = sink_interface
3839
self._typeinfo = sink_typeinfo
@@ -72,7 +73,7 @@ def IConnectionPoint_GetConnectionInterface(
7273
) -> "hints.Hresult":
7374
return E_NOTIMPL
7475

75-
def _call_sinks(self, name: str, *args: Any, **kw: Any) -> List[Any]:
76+
def _call_sinks(self, name: str, *args: Any, **kw: Any) -> list[Any]:
7677
results = []
7778
logger.debug("_call_sinks(%s, %s, *%s, **%s)", self, name, args, kw)
7879
# Is it an IDispatch derived interface? Then, events have to be delivered
@@ -118,12 +119,12 @@ class ConnectableObjectMixin:
118119
"""
119120

120121
if TYPE_CHECKING:
121-
_outgoing_interfaces_: ClassVar[List[Type[IDispatch]]]
122-
_reg_typelib_: ClassVar[Tuple[str, int, int]]
122+
_outgoing_interfaces_: ClassVar[list[type[IDispatch]]]
123+
_reg_typelib_: ClassVar[tuple[str, int, int]]
123124

124125
def __init__(self) -> None:
125126
super().__init__()
126-
self.__connections: Dict[Type[IDispatch], ConnectionPointImpl] = {}
127+
self.__connections: dict[type[IDispatch], ConnectionPointImpl] = {}
127128

128129
tlib = LoadRegTypeLib(*self._reg_typelib_)
129130
for itf in self._outgoing_interfaces_:
@@ -161,7 +162,7 @@ def IConnectionPointContainer_FindConnectionPoint(
161162
return CONNECT_E_NOCONNECTION
162163

163164
def Fire_Event(
164-
self, itf: _UnionT[int, Type[IDispatch]], name: str, *args: Any, **kw: Any
165+
self, itf: _UnionT[int, type[IDispatch]], name: str, *args: Any, **kw: Any
165166
) -> Any:
166167
# Fire event 'name' with arguments *args and **kw.
167168
# Accepts either an interface index or an interface as first argument.

0 commit comments

Comments
 (0)