Skip to content

Commit

Permalink
imprv: generate: Hint callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
matperc committed May 26, 2023
1 parent a69db0d commit d21432b
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions tools/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,12 @@ def _type_to_python(
else:
return_type = f"Tuple[{', '.join(return_args)}]"

# FIXME, how to express Callable with variable arguments?
if (len(names) > 0 and names[-1].startswith("*")) or varargs:
return f"Callable[..., {return_type}]"
args = args[:-1] # Closure data is always the last argument
if len(args) > 0:
return f"Callable[Concatenate[{', '.join(args)}, _VarArgs], {return_type}]"
else:
return f"Callable[Concatenate[_VarArgs], {return_type}]"
else:
return f"Callable[[{', '.join(args)}], {return_type}]"
else:
Expand Down Expand Up @@ -326,9 +329,25 @@ def _build(parent: ObjectT, namespace: str, overrides: dict[str, str]) -> str:
ns = set()
ret = _gi_build_stub(parent, namespace, dir(parent), ns, overrides, None, "")

typings = "from typing import Any, Callable, Literal, Optional, Tuple, Type, TypeVar, Sequence"
typings_list = [
"Any",
"Callable",
"Literal",
"Optional",
"Tuple",
"Type",
"TypeVar",
"Sequence",
]

typing_extensions_list = ["Concatenate", "ParamSpec"]

typings = f"from typing import {' ,'.join(typings_list)}"
typing_extensions = (
f"from typing_extensions import {' ,'.join(typing_extensions_list)}"
)

typevars: list[str] = []
typevars: list[str] = ["_VarArgs = ParamSpec('_VarArgs')"]
imports: list[str] = []
if "cairo" in ns:
imports = ["import cairo"]
Expand All @@ -340,6 +359,8 @@ def _build(parent: ObjectT, namespace: str, overrides: dict[str, str]) -> str:
return (
typings
+ "\n\n"
+ typing_extensions
+ "\n\n"
+ "\n".join(imports)
+ "\n"
+ "\n".join(typevars)
Expand Down

0 comments on commit d21432b

Please sign in to comment.