script_bindings(python): Add ruff rule for strict typing in function (#38694)

This is part of introducing the Ruff rule [flake8-annotations
(ANN)](https://docs.astral.sh/ruff/rules/#flake8-annotations-ann) into
the script_bindings folder.

Since `codegen.py` has 10k lines of code, the strategy is to introduce
the rule first while ignoring the `ANN` Ruff rule for `codegen.py`. We
will then gradually add type annotations slice by slice to ensure no new
errors occur outside of `ANN`.

Testing: `./mach test-wpt webidl`

---------

Signed-off-by: Jerens Lensun <jerensslensun@gmail.com>
This commit is contained in:
Jerens Lensun 2025-08-18 15:26:30 +08:00 committed by GitHub
parent 27dededa65
commit 788d6db94d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 73 additions and 54 deletions

View file

@ -2,16 +2,25 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
# fmt: off
from __future__ import annotations
import os
import sys
import json
import re
from typing import TYPE_CHECKING
from collections.abc import Iterator
SCRIPT_PATH = os.path.abspath(os.path.dirname(__file__))
SERVO_ROOT = os.path.abspath(os.path.join(SCRIPT_PATH, "..", "..", ".."))
FILTER_PATTERN = re.compile("// skip-unless ([A-Z_]+)\n")
if TYPE_CHECKING:
from configuration import Configuration
from WebIDL import Parser
def main() -> None:
os.chdir(os.path.join(os.path.dirname(__file__)))
@ -84,13 +93,13 @@ def main() -> None:
f.write(module.encode("utf-8"))
def make_dir(path: str):
def make_dir(path: str)-> str:
if not os.path.exists(path):
os.makedirs(path)
return path
def generate(config, name: str, filename: str) -> None:
def generate(config: Configuration, name: str, filename: str) -> None:
from codegen import GlobalGenRoots
root = getattr(GlobalGenRoots, name)(config)
code = root.define()
@ -98,7 +107,7 @@ def generate(config, name: str, filename: str) -> None:
f.write(code.encode("utf-8"))
def add_css_properties_attributes(css_properties_json: str, parser) -> None:
def add_css_properties_attributes(css_properties_json: str, parser: Parser) -> None:
def map_preference_name(preference_name: str) -> str:
"""Map between Stylo preference names and Servo preference names as the
`css-properties.json` file is generated by Stylo. This should be kept in sync with the
@ -132,7 +141,7 @@ def add_css_properties_attributes(css_properties_json: str, parser) -> None:
parser.parse(idl, "CSSStyleDeclaration_generated.webidl")
def attribute_names(property_name: str):
def attribute_names(property_name: str) -> Iterator[str]:
# https://drafts.csswg.org/cssom/#dom-cssstyledeclaration-dashed-attribute
if property_name != "float":
yield property_name
@ -149,7 +158,7 @@ def attribute_names(property_name: str):
# https://drafts.csswg.org/cssom/#css-property-to-idl-attribute
def camel_case(chars: str, webkit_prefixed: bool = False):
def camel_case(chars: str, webkit_prefixed: bool = False) -> Iterator[str]:
if webkit_prefixed:
chars = chars[1:]
next_is_uppercase = False