From ecbb9631fbf85e2306974b2067e7289a8595f130 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 7 Jan 2023 21:48:54 +0900 Subject: [PATCH 1/2] update `typedesc` type annotations --- comtypes/tools/typedesc.py | 121 +++++++++++++++++++++++-------------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/comtypes/tools/typedesc.py b/comtypes/tools/typedesc.py index 5fefb6b45..3085f0ea2 100644 --- a/comtypes/tools/typedesc.py +++ b/comtypes/tools/typedesc.py @@ -9,8 +9,9 @@ class TypeLib(object): - def __init__(self, name, guid, major, minor, doc=None): - # type: (str, str, int, int, Optional[str]) -> None + def __init__( + self, name: str, guid: str, major: int, minor: int, doc: Optional[str] = None + ) -> None: self.name = name self.guid = guid self.major = major @@ -27,16 +28,23 @@ def __repr__(self): class Constant(object): - def __init__(self, name, typ, value): - # type: (str, _UnionT[Typedef, FundamentalType], Any) -> None + def __init__( + self, name: str, typ: _UnionT[Typedef, FundamentalType], value: Any + ) -> None: self.name = name self.typ = typ self.value = value class External(object): - def __init__(self, tlib, name, size, align, docs=None): - # type: (ITypeLib, str, int, int, Optional[Tuple[str, str]]) -> None + def __init__( + self, + tlib: ITypeLib, + name: str, + size: int, + align: int, + docs: Optional[Tuple[str, str]] = None, + ) -> None: # the type library containing the symbol self.tlib = tlib # name of symbol @@ -46,57 +54,72 @@ def __init__(self, tlib, name, size, align, docs=None): # type lib description self.docs = docs - def get_head(self): - # type: () -> External + def get_head(self) -> "External": # codegen might call this return self class SAFEARRAYType(object): - def __init__(self, typ): - # type: (Any) -> None + def __init__(self, typ: Any) -> None: self.typ = typ self.align = self.size = ctypes.sizeof(ctypes.c_void_p) * 8 class ComMethod(object): # custom COM method, parsed from typelib - def __init__(self, invkind, memid, name, returns, idlflags, doc): - # type: (int, int, str, Any, List[str], Optional[str]) -> None + def __init__( + self, + invkind: int, + memid: int, + name: str, + returns: Any, + idlflags: List[str], + doc: Optional[str], + ) -> None: self.invkind = invkind self.name = name self.returns = returns self.idlflags = idlflags self.memid = memid self.doc = doc - self.arguments = [] # type: List[Tuple[Any, str, List[str], Optional[Any]]] + self.arguments: List[Tuple[Any, str, List[str], Optional[Any]]] = [] - def add_argument(self, typ, name, idlflags, default): - # type: (Any, str, List[str], Optional[Any]) -> None + def add_argument( + self, typ: Any, name: str, idlflags: List[str], default: Optional[Any] + ) -> None: self.arguments.append((typ, name, idlflags, default)) class DispMethod(object): # dispatchable COM method, parsed from typelib - def __init__(self, dispid, invkind, name, returns, idlflags, doc): - # type: (int, int, str, Any, List[str], Optional[str]) -> None + def __init__( + self, + dispid: int, + invkind: int, + name: str, + returns: Any, + idlflags: List[str], + doc: Optional[str], + ) -> None: self.dispid = dispid self.invkind = invkind self.name = name self.returns = returns self.idlflags = idlflags self.doc = doc - self.arguments = [] # type: List[Tuple[Any, str, List[str], Optional[Any]]] + self.arguments: List[Tuple[Any, str, List[str], Optional[Any]]] = [] - def add_argument(self, typ, name, idlflags, default): - # type: (Any, str, List[str], Optional[Any]) -> None + def add_argument( + self, typ: Any, name: str, idlflags: List[str], default: Optional[Any] + ) -> None: self.arguments.append((typ, name, idlflags, default)) class DispProperty(object): # dispatchable COM property, parsed from typelib - def __init__(self, dispid, name, typ, idlflags, doc): - # type: (int, str, Any, List[str], Optional[Any]) -> None + def __init__( + self, dispid: int, name: str, typ: Any, idlflags: List[str], doc: Optional[Any] + ) -> None: self.dispid = dispid self.name = name self.typ = typ @@ -105,20 +128,24 @@ def __init__(self, dispid, name, typ, idlflags, doc): class DispInterfaceHead(object): - def __init__(self, itf): - # type: (DispInterface) -> None + def __init__(self, itf: "DispInterface") -> None: self.itf = itf class DispInterfaceBody(object): - def __init__(self, itf): - # type: (DispInterface) -> None + def __init__(self, itf: "DispInterface") -> None: self.itf = itf class DispInterface(object): - def __init__(self, name, members, base, iid, idlflags): - # type: (str, List[_UnionT[DispMethod, DispProperty]], Any, str, List[str]) -> None + def __init__( + self, + name: str, + members: List[_UnionT[DispMethod, DispProperty]], + base: Any, + iid: str, + idlflags: List[str], + ) -> None: self.name = name self.members = members self.base = base @@ -127,30 +154,32 @@ def __init__(self, name, members, base, iid, idlflags): self.itf_head = DispInterfaceHead(self) self.itf_body = DispInterfaceBody(self) - def get_body(self): - # type: () -> DispInterfaceBody + def get_body(self) -> DispInterfaceBody: return self.itf_body - def get_head(self): - # type: () -> DispInterfaceHead + def get_head(self) -> DispInterfaceHead: return self.itf_head class ComInterfaceHead(object): - def __init__(self, itf): - # type: (ComInterface) -> None + def __init__(self, itf: "ComInterface") -> None: self.itf = itf class ComInterfaceBody(object): - def __init__(self, itf): - # type: (ComInterface) -> None + def __init__(self, itf: "ComInterface") -> None: self.itf = itf class ComInterface(object): - def __init__(self, name, members, base, iid, idlflags): - # type: (str, List[ComMethod], Any, str, List[str]) -> None + def __init__( + self, + name: str, + members: List[ComMethod], + base: Any, + iid: str, + idlflags: List[str], + ) -> None: self.name = name self.members = members self.base = base @@ -159,24 +188,22 @@ def __init__(self, name, members, base, iid, idlflags): self.itf_head = ComInterfaceHead(self) self.itf_body = ComInterfaceBody(self) - def get_body(self): - # type: () -> ComInterfaceBody + def get_body(self) -> ComInterfaceBody: return self.itf_body - def get_head(self): - # type: () -> ComInterfaceHead + def get_head(self) -> ComInterfaceHead: return self.itf_head class CoClass(object): - def __init__(self, name, clsid, idlflags, tlibattr): - # type: (str, str, List[str], TLIBATTR) -> None + def __init__( + self, name: str, clsid: str, idlflags: List[str], tlibattr: TLIBATTR + ) -> None: self.name = name self.clsid = clsid self.idlflags = idlflags self.tlibattr = tlibattr - self.interfaces = [] # type: List[Tuple[Any, int]] + self.interfaces: List[Tuple[Any, int]] = [] - def add_interface(self, itf, idlflags): - # type: (Any, int) -> None + def add_interface(self, itf: Any, idlflags: int) -> None: self.interfaces.append((itf, idlflags)) From f6e82534717abe1880e67e1701d519d19b6e1718 Mon Sep 17 00:00:00 2001 From: junkmd Date: Sat, 7 Jan 2023 21:55:43 +0900 Subject: [PATCH 2/2] update `typedesc_base` type annotations --- comtypes/tools/typedesc_base.py | 69 ++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/comtypes/tools/typedesc_base.py b/comtypes/tools/typedesc_base.py index 11bf5b6e9..be45f61a3 100644 --- a/comtypes/tools/typedesc_base.py +++ b/comtypes/tools/typedesc_base.py @@ -3,7 +3,6 @@ Any, List, Optional, - TYPE_CHECKING, Tuple, Union as _UnionT, SupportsInt, @@ -144,8 +143,7 @@ def __init__(self, name, typ): class ArrayType(object): location = None - def __init__(self, typ, min, max): - # type: (Any, int, int) -> None + def __init__(self, typ: Any, min: int, max: int) -> None: self.typ = typ self.min = min self.max = max @@ -154,28 +152,25 @@ def __init__(self, typ, min, max): class StructureHead(object): location = None - def __init__(self, struct): - # type: (_Struct_Union_Base) -> None + def __init__(self, struct: "_Struct_Union_Base") -> None: self.struct = struct class StructureBody(object): location = None - def __init__(self, struct): - # type: (_Struct_Union_Base) -> None + def __init__(self, struct: "_Struct_Union_Base") -> None: self.struct = struct class _Struct_Union_Base(object): - if TYPE_CHECKING: - name: str - align: int - members: List[_UnionT["Field", Method, Constructor]] - bases: List["_Struct_Union_Base"] - artificial: Optional[Any] - size: Optional[int] - _recordinfo_: Tuple[str, int, int, int, str] + name: str + align: int + members: List[_UnionT["Field", Method, Constructor]] + bases: List["_Struct_Union_Base"] + artificial: Optional[Any] + size: Optional[int] + _recordinfo_: Tuple[str, int, int, int, str] location = None @@ -183,18 +178,23 @@ def __init__(self): self.struct_body = StructureBody(self) self.struct_head = StructureHead(self) - def get_body(self): - # type: () -> StructureBody + def get_body(self) -> StructureBody: return self.struct_body - def get_head(self): - # type: () -> StructureHead + def get_head(self) -> StructureHead: return self.struct_head class Structure(_Struct_Union_Base): - def __init__(self, name, align, members, bases, size, artificial=None): - # type: (str, SupportsInt, List[Field], List[Any], Optional[SupportsInt], Optional[Any]) -> None + def __init__( + self, + name: str, + align: SupportsInt, + members: List["Field"], + bases: List[Any], + size: Optional[SupportsInt], + artificial: Optional[Any] = None, + ) -> None: self.name = name self.align = int(align) self.members = members @@ -208,8 +208,15 @@ def __init__(self, name, align, members, bases, size, artificial=None): class Union(_Struct_Union_Base): - def __init__(self, name, align, members, bases, size, artificial=None): - # type: (str, SupportsInt, List[Field], List[Any], Optional[SupportsInt], Optional[Any]) -> None + def __init__( + self, + name: str, + align: SupportsInt, + members: List["Field"], + bases: List[Any], + size: Optional[SupportsInt], + artificial: Optional[Any] = None, + ) -> None: self.name = name self.align = int(align) self.members = members @@ -223,8 +230,9 @@ def __init__(self, name, align, members, bases, size, artificial=None): class Field(object): - def __init__(self, name, typ, bits, offset): - # type: (str, Any, Optional[Any], SupportsInt) -> None + def __init__( + self, name: str, typ: Any, bits: Optional[Any], offset: SupportsInt + ) -> None: self.name = name self.typ = typ self.bits = bits @@ -241,21 +249,18 @@ def __init__(self, typ, const, volatile): class Enumeration(object): location = None - def __init__(self, name, size, align): - # type: (str, SupportsInt, SupportsInt) -> None + def __init__(self, name: str, size: SupportsInt, align: SupportsInt) -> None: self.name = name self.size = int(size) self.align = int(align) - self.values = [] # type: List[EnumValue] + self.values: List[EnumValue] = [] - def add_value(self, v): - # type: (EnumValue) -> None + def add_value(self, v: "EnumValue") -> None: self.values.append(v) class EnumValue(object): - def __init__(self, name, value, enumeration): - # type: (str, int, Enumeration) -> None + def __init__(self, name: str, value: int, enumeration: Enumeration) -> None: self.name = name self.value = value self.enumeration = enumeration