Skip to content

Commit

Permalink
add _to_outtype to typeannotator (enthought#539)
Browse files Browse the repository at this point in the history
  • Loading branch information
junkmd authored May 10, 2024
1 parent 133d19b commit 2591b82
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions comtypes/tools/typeannotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ def _gen_prop_put_putref(self, name: str, fput: _T_MTD, fputref: _T_MTD) -> None
self._define_normal_prop(name, setter=setter)


def _to_outtype(typ: Any) -> str:
if isinstance(typ, typedesc.PointerType):
return _to_outtype(typ.typ)
elif isinstance(typ, typedesc.DispInterface):
return f"'{typ.name}'"
elif isinstance(typ, typedesc.ComInterface):
return f"'{typ.name}'"
return "hints.Incomplete"


class ComMethodAnnotator(_MethodAnnotator[typedesc.ComMethod]):
def _iter_outarg_specs(self) -> Iterator[Tuple[Any, str]]:
for typ, name, flags, _ in self.method.arguments:
Expand All @@ -240,7 +250,7 @@ def getvalue(self, name: str) -> str:
else:
inargs.append(f"{argname}: hints.Incomplete = ...")
has_optional = True
outargs = ["hints.Incomplete" for _ in self._iter_outarg_specs()]
outargs = [_to_outtype(ot) for ot, _ in self._iter_outarg_specs()]
if not outargs:
out = "hints.Hresult"
elif len(outargs) == 1:
Expand Down Expand Up @@ -283,7 +293,7 @@ def getvalue(self, name: str) -> str:
else:
inargs.append(f"{argname}: hints.Incomplete = ...")
has_optional = True
out = "hints.Incomplete"
out = _to_outtype(self.method.returns)
in_ = ("self, " + ", ".join(inargs)) if inargs else "self"
return f"def {name}({in_}) -> {out}: ..."

Expand Down Expand Up @@ -314,7 +324,7 @@ def generate(self) -> str:
property_lines: List[str] = []
for mem in props:
property_lines.append("@property # dispprop")
out = "hints.Incomplete"
out = _to_outtype(mem.typ)
property_lines.append(f"def {mem.name}(self) -> {out}: ...")
dispprops = "\n".join(f" {p}" for p in property_lines)
dispmethods = DispMethodsAnnotator().generate(methods)
Expand Down

0 comments on commit 2591b82

Please sign in to comment.