Skip to content

Add return type hints #697

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 8 commits into from
Apr 9, 2025
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
16 changes: 14 additions & 2 deletions codegen/apipatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def get_property_def(self, classname, propname):
line = "async " + line
return " " + line

def get_method_def(self, classname, methodname):
def get_method_def(self, classname, methodname) -> str:
functions = self.idl.classes[classname].functions
name_idl = self.name2idl(classname, methodname)
assert name_idl in functions
Expand All @@ -432,6 +432,15 @@ def get_method_def(self, classname, methodname):
idl_line = functions[name_idl]
args = idl_line.split("(", 1)[1].split(")", 1)[0].split(",")
args = [Attribute(arg) for arg in args if arg.strip()]
return_type = idl_line.split()[0]
if return_type.startswith("[NewObject]"):
# [NewObject] can be skipped: https://webidl.spec.whatwg.org/#NewObject
return_type = idl_line.split()[1]
if return_type.startswith("constructor"):
# this means __init__ which essentially returns None (used with Error Messages)
return_type = None
if return_type:
return_type = self.idl.resolve_type(return_type)

# If one arg that is a dict, flatten dict to kwargs
if len(args) == 1 and args[0].typename.endswith(
Expand Down Expand Up @@ -465,7 +474,10 @@ def get_method_def(self, classname, methodname):

# Construct final def
line = preamble + ", ".join(py_args) + "): pass\n"
line = format_code(line, True).split("):")[0] + "):"
line = (
format_code(line, True).split("):")[0]
+ f"){f' -> {return_type}' if return_type else ''}:"
)
return " " + line

def _arg_from_attribute(self, methodname, attribute, force_optional=False):
Expand Down
7 changes: 6 additions & 1 deletion codegen/idlparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def resolve_type(self, typename):
"GPUPipelineConstantValue": "float",
"GPUExternalTexture": "object",
"undefined": "None",
"ArrayBuffer": "memoryview",
}
name = pythonmap.get(name, name)

Expand All @@ -235,6 +236,10 @@ def resolve_type(self, typename):
names = [self.resolve_type(t).strip("'") for t in name.split(" or ")]
names = sorted(set(names))
return f"Union[{', '.join(names)}]"
if name.startswith("Promise<") and name.endswith(">"):
name = name.split("<")[-1].rstrip(">")
# recursive call if there are any of the above situations?
return self.resolve_type(name)

# Triage
if name in __builtins__:
Expand All @@ -248,7 +253,7 @@ def resolve_type(self, typename):
elif name in ["PredefinedColorSpace"]:
return "str"
else:
assert name.startswith("GPU")
assert name.startswith("GPU"), f"Unknown type: {name}"
name = name[3:]
name = name[:-4] if name.endswith("Dict") else name
if name in self.flags:
Expand Down
Loading