Skip to content

Update type annotations in typedesc and typedesc_base #461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 74 additions & 47 deletions comtypes/tools/typedesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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))
69 changes: 37 additions & 32 deletions comtypes/tools/typedesc_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Any,
List,
Optional,
TYPE_CHECKING,
Tuple,
Union as _UnionT,
SupportsInt,
Expand Down Expand Up @@ -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
Expand All @@ -154,47 +152,49 @@ 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

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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down