11import ctypes
2- from typing import (
3- TYPE_CHECKING ,
4- Any ,
5- Callable ,
6- Dict ,
7- Iterator ,
8- List ,
9- Literal ,
10- NamedTuple ,
11- Optional ,
12- Sequence ,
13- Tuple ,
14- Type ,
15- )
2+ from collections .abc import Callable , Iterator , Sequence
3+ from typing import TYPE_CHECKING , Any , Literal , NamedTuple , Optional
164from typing import Union as _UnionT
175
186import comtypes
@@ -61,17 +49,17 @@ def _encode_idl(names: Sequence[str]) -> int:
6149
6250
6351def _unpack_argspec (
64- idl : List [str ],
65- typ : Type ["_CDataType" ],
52+ idl : list [str ],
53+ typ : type ["_CDataType" ],
6654 name : Optional [str ] = None ,
6755 defval : Any = _NOTHING ,
68- ) -> Tuple [ List [str ], Type ["_CDataType" ], Optional [str ], Any ]:
56+ ) -> tuple [ list [str ], type ["_CDataType" ], Optional [str ], Any ]:
6957 return idl , typ , name , defval
7058
7159
7260def _resolve_argspec (
73- items : Tuple ["hints.ArgSpecElmType" , ...],
74- ) -> Tuple [ Tuple ["hints.ParamFlagType" , ...], Tuple [ Type ["_CDataType" ], ...]]:
61+ items : tuple ["hints.ArgSpecElmType" , ...],
62+ ) -> tuple [ tuple ["hints.ParamFlagType" , ...], tuple [ type ["_CDataType" ], ...]]:
7563 """Unpacks and converts from argspec to paramflags and argtypes.
7664
7765 - paramflags is a sequence of `(pflags: int, argname: str, | None[, defval: Any])`.
@@ -103,21 +91,21 @@ def _resolve_argspec(
10391
10492
10593if TYPE_CHECKING :
106- _VarFlags = Tuple [str , ...]
107- _VarFlagsWithDispIdHelpstr = Tuple ["dispid" , "helpstring" , hints .Unpack [_VarFlags ]]
108- _VarFlagsWithDispId = Tuple ["dispid" , hints .Unpack [_VarFlags ]]
109- _VarFlagsWithHelpstr = Tuple ["helpstring" , hints .Unpack [_VarFlags ]]
94+ _VarFlags = tuple [str , ...]
95+ _VarFlagsWithDispIdHelpstr = tuple ["dispid" , "helpstring" , hints .Unpack [_VarFlags ]]
96+ _VarFlagsWithDispId = tuple ["dispid" , hints .Unpack [_VarFlags ]]
97+ _VarFlagsWithHelpstr = tuple ["helpstring" , hints .Unpack [_VarFlags ]]
11098 _DispIdlFlags = _UnionT [_VarFlagsWithDispIdHelpstr , _VarFlagsWithDispId ]
11199 _ComIdlFlags = _UnionT [_VarFlags , _VarFlagsWithHelpstr ]
112100
113101
114102class _ComMemberSpec (NamedTuple ):
115103 """Specifier for a slot of COM method or property."""
116104
117- restype : Optional [Type ["_CDataType" ]]
105+ restype : Optional [type ["_CDataType" ]]
118106 name : str
119- argtypes : Tuple [ Type ["_CDataType" ], ...]
120- paramflags : Optional [Tuple ["hints.ParamFlagType" , ...]]
107+ argtypes : tuple [ type ["_CDataType" ], ...]
108+ paramflags : Optional [tuple ["hints.ParamFlagType" , ...]]
121109 idlflags : _UnionT ["_ComIdlFlags" , "_DispIdlFlags" ]
122110 doc : Optional [str ]
123111
@@ -131,8 +119,8 @@ class _DispMemberSpec(NamedTuple):
131119 what : Literal ["DISPMETHOD" , "DISPPROPERTY" ]
132120 name : str
133121 idlflags : "_DispIdlFlags"
134- restype : Optional [Type ["_CDataType" ]]
135- argspec : Tuple ["hints.ArgSpecElmType" , ...]
122+ restype : Optional [type ["_CDataType" ]]
123+ argspec : tuple ["hints.ArgSpecElmType" , ...]
136124
137125 @property
138126 def memid (self ) -> int :
@@ -228,7 +216,7 @@ def COMMETHOD(idlflags, restype, methodname, *argspec) -> _ComMemberSpec:
228216# workarounds for ctypes functions and parameters
229217
230218
231- def _prepare_parameter (value : Any , atyp : Type ["_CDataType" ]) -> "_CDataType" :
219+ def _prepare_parameter (value : Any , atyp : type ["_CDataType" ]) -> "_CDataType" :
232220 # parameter was passed, call `from_param()` to
233221 # convert it to a `ctypes` type.
234222 if getattr (value , "_type_" , None ) is atyp :
@@ -249,8 +237,8 @@ def _prepare_parameter(value: Any, atyp: Type["_CDataType"]) -> "_CDataType":
249237
250238def _fix_inout_args (
251239 func : Callable [..., Any ],
252- argtypes : Tuple [ Type ["_CDataType" ], ...],
253- paramflags : Tuple ["hints.ParamFlagType" , ...],
240+ argtypes : tuple [ type ["_CDataType" ], ...],
241+ paramflags : tuple ["hints.ParamFlagType" , ...],
254242) -> Callable [..., Any ]:
255243 """This function provides a workaround for a bug in `ctypes`.
256244
@@ -266,7 +254,7 @@ def _fix_inout_args(
266254 def call_with_inout (self , * args , ** kw ):
267255 args = list (args )
268256 # Indexed by order in the output
269- outargs : Dict [int , "_CDataType" ] = {}
257+ outargs : dict [int , "_CDataType" ] = {}
270258 outnum = 0
271259 param_index = 0
272260 # Go through all expected arguments and match them to the provided arguments.
@@ -292,7 +280,7 @@ def call_with_inout(self, *args, **kw):
292280 name = info [1 ]
293281 # [in, out] parameters are passed as pointers,
294282 # this is the pointed-to type:
295- atyp : Type ["_CDataType" ] = getattr (argtypes [i ], "_type_" )
283+ atyp : type ["_CDataType" ] = getattr (argtypes [i ], "_type_" )
296284
297285 # Get the actual parameter, either as positional or
298286 # keyword arg.
@@ -355,7 +343,7 @@ def call_with_inout(self, *args, **kw):
355343
356344class PropertyMapping :
357345 def __init__ (self ):
358- self ._data : Dict [ Tuple [str , _DocType , int ], List [_PropFunc ]] = {}
346+ self ._data : dict [ tuple [str , _DocType , int ], list [_PropFunc ]] = {}
359347
360348 def add_propget (
361349 self , name : str , doc : _DocType , nargs : int , func : Callable [..., Any ]
@@ -372,7 +360,7 @@ def add_propputref(
372360 ) -> None :
373361 self ._data .setdefault ((name , doc , nargs ), [None , None , None ])[2 ] = func
374362
375- def __iter__ (self ) -> Iterator [Tuple [str , _DocType , int , _PropFunc , _PropFunc ]]:
363+ def __iter__ (self ) -> Iterator [tuple [str , _DocType , int , _PropFunc , _PropFunc ]]:
376364 for (name , doc , nargs ), (fget , propput , propputref ) in self ._data .items ():
377365 if propput is not None and propputref is not None :
378366 # Create a setter method that examines the argument type
@@ -414,7 +402,7 @@ def add(self, m: _MemberSpec, func: Callable[..., Any]) -> None:
414402
415403 # The following code assumes that the docstrings for
416404 # propget and propput are identical.
417- def __iter__ (self ) -> Iterator [Tuple [str , _UnionT [property , "named_property" ]]]:
405+ def __iter__ (self ) -> Iterator [tuple [str , _UnionT [property , "named_property" ]]]:
418406 for name , doc , nargs , fget , fset in self ._mapping :
419407 if nargs == 0 :
420408 prop = property (fget , fset , None , doc )
@@ -425,49 +413,49 @@ def __iter__(self) -> Iterator[Tuple[str, _UnionT[property, "named_property"]]]:
425413 prop = named_property (f"{ self ._cls_name } .{ name } " , fget , fset , doc )
426414 yield (name , prop )
427415
428- def to_propget_keys (self , m : _MemberSpec ) -> Tuple [str , _DocType , int ]:
416+ def to_propget_keys (self , m : _MemberSpec ) -> tuple [str , _DocType , int ]:
429417 raise NotImplementedError
430418
431- def to_propput_keys (self , m : _MemberSpec ) -> Tuple [str , _DocType , int ]:
419+ def to_propput_keys (self , m : _MemberSpec ) -> tuple [str , _DocType , int ]:
432420 raise NotImplementedError
433421
434- def to_propputref_keys (self , m : _MemberSpec ) -> Tuple [str , _DocType , int ]:
422+ def to_propputref_keys (self , m : _MemberSpec ) -> tuple [str , _DocType , int ]:
435423 raise NotImplementedError
436424
437425
438426class ComPropertyGenerator (PropertyGenerator ):
439427 # XXX Hm. What, when paramflags is None?
440428 # Or does have '0' values?
441429 # Seems we loose then, at least for properties...
442- def to_propget_keys (self , m : _ComMemberSpec ) -> Tuple [str , _DocType , int ]:
430+ def to_propget_keys (self , m : _ComMemberSpec ) -> tuple [str , _DocType , int ]:
443431 assert m .name .startswith ("_get_" )
444432 assert m .paramflags is not None
445433 nargs = len ([f for f in m .paramflags if f [0 ] & 7 in (0 , 1 )])
446434 # XXX or should we do this?
447435 # nargs = len([f for f in paramflags if (f[0] & 1) or (f[0] == 0)])
448436 return m .name [len ("_get_" ) :], m .doc , nargs
449437
450- def to_propput_keys (self , m : _ComMemberSpec ) -> Tuple [str , _DocType , int ]:
438+ def to_propput_keys (self , m : _ComMemberSpec ) -> tuple [str , _DocType , int ]:
451439 assert m .name .startswith ("_set_" )
452440 assert m .paramflags is not None
453441 nargs = len ([f for f in m .paramflags if f [0 ] & 7 in (0 , 1 )]) - 1
454442 return m .name [len ("_set_" ) :], m .doc , nargs
455443
456- def to_propputref_keys (self , m : _ComMemberSpec ) -> Tuple [str , _DocType , int ]:
444+ def to_propputref_keys (self , m : _ComMemberSpec ) -> tuple [str , _DocType , int ]:
457445 assert m .name .startswith ("_setref_" )
458446 assert m .paramflags is not None
459447 nargs = len ([f for f in m .paramflags if f [0 ] & 7 in (0 , 1 )]) - 1
460448 return m .name [len ("_setref_" ) :], m .doc , nargs
461449
462450
463451class DispPropertyGenerator (PropertyGenerator ):
464- def to_propget_keys (self , m : _DispMemberSpec ) -> Tuple [str , _DocType , int ]:
452+ def to_propget_keys (self , m : _DispMemberSpec ) -> tuple [str , _DocType , int ]:
465453 return m .name , None , len (m .argspec )
466454
467- def to_propput_keys (self , m : _DispMemberSpec ) -> Tuple [str , _DocType , int ]:
455+ def to_propput_keys (self , m : _DispMemberSpec ) -> tuple [str , _DocType , int ]:
468456 return m .name , None , len (m .argspec ) - 1
469457
470- def to_propputref_keys (self , m : _DispMemberSpec ) -> Tuple [str , _DocType , int ]:
458+ def to_propputref_keys (self , m : _DispMemberSpec ) -> tuple [str , _DocType , int ]:
471459 return m .name , None , len (m .argspec ) - 1
472460
473461
@@ -477,7 +465,7 @@ def __init__(self, cls_name: str, vtbl_offset: int, iid: "comtypes.GUID") -> Non
477465 self ._iid = iid
478466 self ._props = ComPropertyGenerator (cls_name )
479467 # sequence of (name: str, func: Callable, raw_func: Callable, is_prop: bool)
480- self ._mths : List [ Tuple [str , Callable [..., Any ], Callable [..., Any ], bool ]] = []
468+ self ._mths : list [ tuple [str , Callable [..., Any ], Callable [..., Any ], bool ]] = []
481469 self ._member_index = 0
482470
483471 def add (self , m : _ComMemberSpec ) -> None :
@@ -520,7 +508,7 @@ class DispMemberGenerator:
520508 def __init__ (self , cls_name : str ) -> None :
521509 self ._props = DispPropertyGenerator (cls_name )
522510 # sequence of (name: str, func_or_prop: Callable | property, is_prop: bool)
523- self ._items : List [ Tuple [str , _UnionT [Callable [..., Any ], property ], bool ]] = []
511+ self ._items : list [ tuple [str , _UnionT [Callable [..., Any ], property ], bool ]] = []
524512
525513 def add (self , m : _DispMemberSpec ) -> None :
526514 if m .what == "DISPPROPERTY" : # DISPPROPERTY
0 commit comments