Skip to content

Commit

Permalink
Fix type annotations referenced by tlbparser. (#621)
Browse files Browse the repository at this point in the history
* Fix type annotation for `typedesc.External.docs`.

* Fix type annotations in `typeinfo`.
- `N11tagTYPEDESC5DOLLAR_203E`
- `N10tagVARDESC5DOLLAR_205E`

* Remove redundant temporary variables in `tlbparser`.
At the same time, the variable annotations attached to them were also removed.

* To f-string.

* To f-string.

* To f-string.
  • Loading branch information
junkmd committed Sep 21, 2024
1 parent 0ef1d74 commit ffaf502
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
19 changes: 8 additions & 11 deletions comtypes/tools/tlbparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,18 @@ def make_type(self, tdesc: typeinfo.TYPEDESC, tinfo: typeinfo.ITypeInfo) -> Any:
)
return typ
elif tdesc.vt == automation.VT_PTR:
ptrdesc: typeinfo.TYPEDESC = tdesc._.lptdesc[0]
typ = self.make_type(ptrdesc, tinfo)
return PTR(typ)
return PTR(self.make_type(tdesc._.lptdesc[0], tinfo))
elif tdesc.vt == automation.VT_USERDEFINED:
try:
ti = tinfo.GetRefTypeInfo(tdesc._.hreftype)
except COMError as details:
type_name = "__error_hreftype_%d__" % tdesc._.hreftype
type_name = f"__error_hreftype_{tdesc._.hreftype:d}__"
tlib_name = get_tlib_filename(self.tlib)
if tlib_name is None:
tlib_name = "unknown typelib"
message = (
"\n\tGetRefTypeInfo failed in %s: %s\n\tgenerating type '%s' instead"
% (tlib_name, details, type_name)
f"\n\tGetRefTypeInfo failed in {tlib_name}: {details}"
f"\n\tgenerating type '{type_name}' instead"
)
import warnings

Expand All @@ -154,8 +152,7 @@ def make_type(self, tdesc: typeinfo.TYPEDESC, tinfo: typeinfo.ITypeInfo) -> Any:
return result
elif tdesc.vt == automation.VT_SAFEARRAY:
# SAFEARRAY(<type>), see Don Box pp.331f
safearraydesc: typeinfo.TYPEDESC = tdesc._.lptdesc[0]
return midlSAFEARRAY(self.make_type(safearraydesc, tinfo))
return midlSAFEARRAY(self.make_type(tdesc._.lptdesc[0], tinfo))
raise NotImplementedError(tdesc.vt)

################################################################
Expand Down Expand Up @@ -272,7 +269,7 @@ def ParseInterface(
# Don't known what artefact that is - we ignore it.
# It's an interface without methods anyway.
if itf_name != "IOleControlTypes":
message = "Ignoring interface %s which has no base interface" % itf_name
message = f"Ignoring interface {itf_name} which has no base interface"
import warnings

warnings.warn(message, UserWarning)
Expand Down Expand Up @@ -592,13 +589,13 @@ def _register(
self, name: Optional[str], value: Any, tlib: Optional[typeinfo.ITypeLib] = None
) -> None:
modname = self._typelib_module(tlib)
fullname = "%s.%s" % (modname, name)
fullname = f"{modname}.{name}"
if fullname in self.items:
# XXX Can we really allow this? It happens, at least.
if isinstance(value, typedesc.External):
return
# BUG: We try to register an item that's already registered.
raise ValueError("Bug: Multiple registered name '%s': %r" % (name, value))
raise ValueError(f"Bug: Multiple registered name '{name}': {value!r}")
self.items[fullname] = value

def parse_typeinfo(self, tinfo: typeinfo.ITypeInfo) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion comtypes/tools/typedesc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
name: str,
size: int,
align: int,
docs: Optional[Tuple[str, str]] = None,
docs: Optional[Tuple[str, Optional[str]]] = None,
) -> None:
# the type library containing the symbol
self.tlib = tlib
Expand Down
6 changes: 3 additions & 3 deletions comtypes/typeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,8 @@ def GetGUID(self, dwGuidKind: int) -> GUID:
class N11tagTYPEDESC5DOLLAR_203E(ctypes.Union):
# C:/Programme/gccxml/bin/Vc71/PlatformSDK/oaidl.h 584
if TYPE_CHECKING:
lptdesc: TYPEDESC
lpadesc: tagARRAYDESC
lptdesc: _Pointer[tagTYPEDESC]
lpadesc: _Pointer[tagARRAYDESC]
hreftype: int


Expand Down Expand Up @@ -1175,7 +1175,7 @@ class N10tagVARDESC5DOLLAR_205E(ctypes.Union):
# C:/Programme/gccxml/bin/Vc71/PlatformSDK/oaidl.h 807
if TYPE_CHECKING:
oInst: int
lpvarValue: VARIANT
lpvarValue: _Pointer[VARIANT]


N10tagVARDESC5DOLLAR_205E._fields_ = [
Expand Down

0 comments on commit ffaf502

Please sign in to comment.