Skip to content

Commit 95962e9

Browse files
authored
Update type annotations in typedesc and typedesc_base (#461)
* update `typedesc` type annotations * update `typedesc_base` type annotations
1 parent 0b4dbca commit 95962e9

File tree

2 files changed

+111
-79
lines changed

2 files changed

+111
-79
lines changed

comtypes/tools/typedesc.py

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010

1111
class TypeLib(object):
12-
def __init__(self, name, guid, major, minor, doc=None):
13-
# type: (str, str, int, int, Optional[str]) -> None
12+
def __init__(
13+
self, name: str, guid: str, major: int, minor: int, doc: Optional[str] = None
14+
) -> None:
1415
self.name = name
1516
self.guid = guid
1617
self.major = major
@@ -27,16 +28,23 @@ def __repr__(self):
2728

2829

2930
class Constant(object):
30-
def __init__(self, name, typ, value):
31-
# type: (str, _UnionT[Typedef, FundamentalType], Any) -> None
31+
def __init__(
32+
self, name: str, typ: _UnionT[Typedef, FundamentalType], value: Any
33+
) -> None:
3234
self.name = name
3335
self.typ = typ
3436
self.value = value
3537

3638

3739
class External(object):
38-
def __init__(self, tlib, name, size, align, docs=None):
39-
# type: (ITypeLib, str, int, int, Optional[Tuple[str, str]]) -> None
40+
def __init__(
41+
self,
42+
tlib: ITypeLib,
43+
name: str,
44+
size: int,
45+
align: int,
46+
docs: Optional[Tuple[str, str]] = None,
47+
) -> None:
4048
# the type library containing the symbol
4149
self.tlib = tlib
4250
# name of symbol
@@ -46,57 +54,72 @@ def __init__(self, tlib, name, size, align, docs=None):
4654
# type lib description
4755
self.docs = docs
4856

49-
def get_head(self):
50-
# type: () -> External
57+
def get_head(self) -> "External":
5158
# codegen might call this
5259
return self
5360

5461

5562
class SAFEARRAYType(object):
56-
def __init__(self, typ):
57-
# type: (Any) -> None
63+
def __init__(self, typ: Any) -> None:
5864
self.typ = typ
5965
self.align = self.size = ctypes.sizeof(ctypes.c_void_p) * 8
6066

6167

6268
class ComMethod(object):
6369
# custom COM method, parsed from typelib
64-
def __init__(self, invkind, memid, name, returns, idlflags, doc):
65-
# type: (int, int, str, Any, List[str], Optional[str]) -> None
70+
def __init__(
71+
self,
72+
invkind: int,
73+
memid: int,
74+
name: str,
75+
returns: Any,
76+
idlflags: List[str],
77+
doc: Optional[str],
78+
) -> None:
6679
self.invkind = invkind
6780
self.name = name
6881
self.returns = returns
6982
self.idlflags = idlflags
7083
self.memid = memid
7184
self.doc = doc
72-
self.arguments = [] # type: List[Tuple[Any, str, List[str], Optional[Any]]]
85+
self.arguments: List[Tuple[Any, str, List[str], Optional[Any]]] = []
7386

74-
def add_argument(self, typ, name, idlflags, default):
75-
# type: (Any, str, List[str], Optional[Any]) -> None
87+
def add_argument(
88+
self, typ: Any, name: str, idlflags: List[str], default: Optional[Any]
89+
) -> None:
7690
self.arguments.append((typ, name, idlflags, default))
7791

7892

7993
class DispMethod(object):
8094
# dispatchable COM method, parsed from typelib
81-
def __init__(self, dispid, invkind, name, returns, idlflags, doc):
82-
# type: (int, int, str, Any, List[str], Optional[str]) -> None
95+
def __init__(
96+
self,
97+
dispid: int,
98+
invkind: int,
99+
name: str,
100+
returns: Any,
101+
idlflags: List[str],
102+
doc: Optional[str],
103+
) -> None:
83104
self.dispid = dispid
84105
self.invkind = invkind
85106
self.name = name
86107
self.returns = returns
87108
self.idlflags = idlflags
88109
self.doc = doc
89-
self.arguments = [] # type: List[Tuple[Any, str, List[str], Optional[Any]]]
110+
self.arguments: List[Tuple[Any, str, List[str], Optional[Any]]] = []
90111

91-
def add_argument(self, typ, name, idlflags, default):
92-
# type: (Any, str, List[str], Optional[Any]) -> None
112+
def add_argument(
113+
self, typ: Any, name: str, idlflags: List[str], default: Optional[Any]
114+
) -> None:
93115
self.arguments.append((typ, name, idlflags, default))
94116

95117

96118
class DispProperty(object):
97119
# dispatchable COM property, parsed from typelib
98-
def __init__(self, dispid, name, typ, idlflags, doc):
99-
# type: (int, str, Any, List[str], Optional[Any]) -> None
120+
def __init__(
121+
self, dispid: int, name: str, typ: Any, idlflags: List[str], doc: Optional[Any]
122+
) -> None:
100123
self.dispid = dispid
101124
self.name = name
102125
self.typ = typ
@@ -105,20 +128,24 @@ def __init__(self, dispid, name, typ, idlflags, doc):
105128

106129

107130
class DispInterfaceHead(object):
108-
def __init__(self, itf):
109-
# type: (DispInterface) -> None
131+
def __init__(self, itf: "DispInterface") -> None:
110132
self.itf = itf
111133

112134

113135
class DispInterfaceBody(object):
114-
def __init__(self, itf):
115-
# type: (DispInterface) -> None
136+
def __init__(self, itf: "DispInterface") -> None:
116137
self.itf = itf
117138

118139

119140
class DispInterface(object):
120-
def __init__(self, name, members, base, iid, idlflags):
121-
# type: (str, List[_UnionT[DispMethod, DispProperty]], Any, str, List[str]) -> None
141+
def __init__(
142+
self,
143+
name: str,
144+
members: List[_UnionT[DispMethod, DispProperty]],
145+
base: Any,
146+
iid: str,
147+
idlflags: List[str],
148+
) -> None:
122149
self.name = name
123150
self.members = members
124151
self.base = base
@@ -127,30 +154,32 @@ def __init__(self, name, members, base, iid, idlflags):
127154
self.itf_head = DispInterfaceHead(self)
128155
self.itf_body = DispInterfaceBody(self)
129156

130-
def get_body(self):
131-
# type: () -> DispInterfaceBody
157+
def get_body(self) -> DispInterfaceBody:
132158
return self.itf_body
133159

134-
def get_head(self):
135-
# type: () -> DispInterfaceHead
160+
def get_head(self) -> DispInterfaceHead:
136161
return self.itf_head
137162

138163

139164
class ComInterfaceHead(object):
140-
def __init__(self, itf):
141-
# type: (ComInterface) -> None
165+
def __init__(self, itf: "ComInterface") -> None:
142166
self.itf = itf
143167

144168

145169
class ComInterfaceBody(object):
146-
def __init__(self, itf):
147-
# type: (ComInterface) -> None
170+
def __init__(self, itf: "ComInterface") -> None:
148171
self.itf = itf
149172

150173

151174
class ComInterface(object):
152-
def __init__(self, name, members, base, iid, idlflags):
153-
# type: (str, List[ComMethod], Any, str, List[str]) -> None
175+
def __init__(
176+
self,
177+
name: str,
178+
members: List[ComMethod],
179+
base: Any,
180+
iid: str,
181+
idlflags: List[str],
182+
) -> None:
154183
self.name = name
155184
self.members = members
156185
self.base = base
@@ -159,24 +188,22 @@ def __init__(self, name, members, base, iid, idlflags):
159188
self.itf_head = ComInterfaceHead(self)
160189
self.itf_body = ComInterfaceBody(self)
161190

162-
def get_body(self):
163-
# type: () -> ComInterfaceBody
191+
def get_body(self) -> ComInterfaceBody:
164192
return self.itf_body
165193

166-
def get_head(self):
167-
# type: () -> ComInterfaceHead
194+
def get_head(self) -> ComInterfaceHead:
168195
return self.itf_head
169196

170197

171198
class CoClass(object):
172-
def __init__(self, name, clsid, idlflags, tlibattr):
173-
# type: (str, str, List[str], TLIBATTR) -> None
199+
def __init__(
200+
self, name: str, clsid: str, idlflags: List[str], tlibattr: TLIBATTR
201+
) -> None:
174202
self.name = name
175203
self.clsid = clsid
176204
self.idlflags = idlflags
177205
self.tlibattr = tlibattr
178-
self.interfaces = [] # type: List[Tuple[Any, int]]
206+
self.interfaces: List[Tuple[Any, int]] = []
179207

180-
def add_interface(self, itf, idlflags):
181-
# type: (Any, int) -> None
208+
def add_interface(self, itf: Any, idlflags: int) -> None:
182209
self.interfaces.append((itf, idlflags))

comtypes/tools/typedesc_base.py

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Any,
44
List,
55
Optional,
6-
TYPE_CHECKING,
76
Tuple,
87
Union as _UnionT,
98
SupportsInt,
@@ -144,8 +143,7 @@ def __init__(self, name, typ):
144143
class ArrayType(object):
145144
location = None
146145

147-
def __init__(self, typ, min, max):
148-
# type: (Any, int, int) -> None
146+
def __init__(self, typ: Any, min: int, max: int) -> None:
149147
self.typ = typ
150148
self.min = min
151149
self.max = max
@@ -154,47 +152,49 @@ def __init__(self, typ, min, max):
154152
class StructureHead(object):
155153
location = None
156154

157-
def __init__(self, struct):
158-
# type: (_Struct_Union_Base) -> None
155+
def __init__(self, struct: "_Struct_Union_Base") -> None:
159156
self.struct = struct
160157

161158

162159
class StructureBody(object):
163160
location = None
164161

165-
def __init__(self, struct):
166-
# type: (_Struct_Union_Base) -> None
162+
def __init__(self, struct: "_Struct_Union_Base") -> None:
167163
self.struct = struct
168164

169165

170166
class _Struct_Union_Base(object):
171-
if TYPE_CHECKING:
172-
name: str
173-
align: int
174-
members: List[_UnionT["Field", Method, Constructor]]
175-
bases: List["_Struct_Union_Base"]
176-
artificial: Optional[Any]
177-
size: Optional[int]
178-
_recordinfo_: Tuple[str, int, int, int, str]
167+
name: str
168+
align: int
169+
members: List[_UnionT["Field", Method, Constructor]]
170+
bases: List["_Struct_Union_Base"]
171+
artificial: Optional[Any]
172+
size: Optional[int]
173+
_recordinfo_: Tuple[str, int, int, int, str]
179174

180175
location = None
181176

182177
def __init__(self):
183178
self.struct_body = StructureBody(self)
184179
self.struct_head = StructureHead(self)
185180

186-
def get_body(self):
187-
# type: () -> StructureBody
181+
def get_body(self) -> StructureBody:
188182
return self.struct_body
189183

190-
def get_head(self):
191-
# type: () -> StructureHead
184+
def get_head(self) -> StructureHead:
192185
return self.struct_head
193186

194187

195188
class Structure(_Struct_Union_Base):
196-
def __init__(self, name, align, members, bases, size, artificial=None):
197-
# type: (str, SupportsInt, List[Field], List[Any], Optional[SupportsInt], Optional[Any]) -> None
189+
def __init__(
190+
self,
191+
name: str,
192+
align: SupportsInt,
193+
members: List["Field"],
194+
bases: List[Any],
195+
size: Optional[SupportsInt],
196+
artificial: Optional[Any] = None,
197+
) -> None:
198198
self.name = name
199199
self.align = int(align)
200200
self.members = members
@@ -208,8 +208,15 @@ def __init__(self, name, align, members, bases, size, artificial=None):
208208

209209

210210
class Union(_Struct_Union_Base):
211-
def __init__(self, name, align, members, bases, size, artificial=None):
212-
# type: (str, SupportsInt, List[Field], List[Any], Optional[SupportsInt], Optional[Any]) -> None
211+
def __init__(
212+
self,
213+
name: str,
214+
align: SupportsInt,
215+
members: List["Field"],
216+
bases: List[Any],
217+
size: Optional[SupportsInt],
218+
artificial: Optional[Any] = None,
219+
) -> None:
213220
self.name = name
214221
self.align = int(align)
215222
self.members = members
@@ -223,8 +230,9 @@ def __init__(self, name, align, members, bases, size, artificial=None):
223230

224231

225232
class Field(object):
226-
def __init__(self, name, typ, bits, offset):
227-
# type: (str, Any, Optional[Any], SupportsInt) -> None
233+
def __init__(
234+
self, name: str, typ: Any, bits: Optional[Any], offset: SupportsInt
235+
) -> None:
228236
self.name = name
229237
self.typ = typ
230238
self.bits = bits
@@ -241,21 +249,18 @@ def __init__(self, typ, const, volatile):
241249
class Enumeration(object):
242250
location = None
243251

244-
def __init__(self, name, size, align):
245-
# type: (str, SupportsInt, SupportsInt) -> None
252+
def __init__(self, name: str, size: SupportsInt, align: SupportsInt) -> None:
246253
self.name = name
247254
self.size = int(size)
248255
self.align = int(align)
249-
self.values = [] # type: List[EnumValue]
256+
self.values: List[EnumValue] = []
250257

251-
def add_value(self, v):
252-
# type: (EnumValue) -> None
258+
def add_value(self, v: "EnumValue") -> None:
253259
self.values.append(v)
254260

255261

256262
class EnumValue(object):
257-
def __init__(self, name, value, enumeration):
258-
# type: (str, int, Enumeration) -> None
263+
def __init__(self, name: str, value: int, enumeration: Enumeration) -> None:
259264
self.name = name
260265
self.value = value
261266
self.enumeration = enumeration

0 commit comments

Comments
 (0)