feat(ci): run mypy over scripts

This commit is contained in:
Michal Čihař 2025-04-01 10:06:20 +02:00
parent 63ec8f0e10
commit 86ba92ebe0
7 changed files with 41 additions and 16 deletions

View file

@ -38,5 +38,5 @@ jobs:
- name: Run mypy
run: |
echo "::add-matcher::.github/matchers/mypy.json"
mypy --show-column-numbers weblate_language_data
mypy --show-column-numbers weblate_language_data scripts/*.py
echo "::remove-matcher owner=mypy::"

View file

@ -39,6 +39,8 @@ with open("modules/iso-codes/data/iso_639-3.json") as handle:
item.get("inverted_name", item["name"])
)

lines: list[list[str]]

with open("languages.csv", newline="") as handle:
reader = csv.reader(handle, delimiter=",")
header = next(reader)
@ -70,7 +72,7 @@ for code in sys.argv[1:]:
[
code,
names[code].split(";")[-1].strip(),
plurals,
str(plurals),
formula,
],
)
@ -78,4 +80,9 @@ for code in sys.argv[1:]:
with open("languages.csv", "w", newline="") as handle:
writer = csv.writer(handle, delimiter=",", lineterminator="\n")
writer.writerow(header)
writer.writerows(sorted(lines, key=itemgetter(0)))
writer.writerows(
sorted(
lines,
key=itemgetter(0), # type: ignore[arg-type]
)
)

View file

@ -4,6 +4,8 @@
#
# SPDX-License-Identifier: MIT

from __future__ import annotations

import csv
import json

@ -11,8 +13,7 @@ import json
with open("languages.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
next(reader)
LANGUAGES = list(reader)
LANGUAGE_CODES = {lang[0] for lang in LANGUAGES}
LANGUAGE_CODES = {lang[0] for lang in reader}

# Read
with open("case-insensitive.csv") as csvfile:
@ -27,8 +28,12 @@ with open("modules/cldr-json/cldr-json/cldr-core/scriptMetadata.json") as handle
with open(
"modules/cldr-json/cldr-json/cldr-core/supplemental/languageData.json",
) as handle:
LANGUAGES = json.load(handle)["supplemental"]["languageData"]
LANGUAGES: dict[str, dict[str, str]] = json.load(handle)["supplemental"][
"languageData"
]

base: str
script: str | None
for code in LANGUAGE_CODES:
if "_" in code:
base, script = code.split("_", 1)

View file

@ -14,7 +14,7 @@ MAPPING = {
with open(
"modules/cldr-json/cldr-json/cldr-core/supplemental/territoryInfo.json",
) as handle:
languages = defaultdict(int)
languages: dict[str, float] = defaultdict(float)
for code, territory in json.load(handle)["supplemental"]["territoryInfo"].items():
population = int(territory["_population"])
if "languagePopulation" not in territory:

View file

@ -8,6 +8,7 @@ from __future__ import annotations

import json
import re
from typing import Literal, TypedDict

MAPPINGS = {
"ar_001": "ar",
@ -112,7 +113,7 @@ def convert_atom(atom: str) -> str | bool:
)


def convert_formula(cldr_formula_and_examples: str) -> str:
def convert_formula(cldr_formula_and_examples: str) -> str | bool:
# Skip formulas which do not trigger integer
if "@integer" not in cldr_formula_and_examples:
return False
@ -169,7 +170,9 @@ def convert_formula(cldr_formula_and_examples: str) -> str:
return " || ".join(chunks)


def reverse_formula(formula: str) -> str:
def reverse_formula(formula: str | bool) -> str:
if isinstance(formula, bool):
raise TypeError(f"Unable to reverse the formula {formula!r}")
if re.match(r"^n( % \d+)? == \d+(\.\.\d+|,\d+)*?$", formula):
return formula.replace(" == ", " != ")
if re.match(r"^n( % \d+)? != \d+(\.\.\d+|,\d+)*?$", formula):
@ -193,14 +196,16 @@ def reverse_formula(formula: str) -> str:
if formula == "(n == 0 || n == 1) || n >= 11 && n <= 99":
return "n >= 2 && (n < 11 || n > 99)"

raise ValueError(f"Unable to reverse the formula '{formula}'")
raise ValueError(f"Unable to reverse the formula {formula!r}")


def merge_formulas(formulas: list[str]) -> str:
def merge_formulas(formulas: list[str | Literal[True]]) -> str:
max_n = len(formulas) - 1
formula = f"{max_n}"
for n in range(max_n - 1, -1, -1):
part = formulas[n]
if isinstance(part, bool):
raise TypeError(f"Not supported part {part!r}")

if not re.match(r"^\([^()]+\)$", part):
part = f"({part})"
@ -211,6 +216,14 @@ def merge_formulas(formulas: list[str]) -> str:
return formula


class LanguageDict(TypedDict, total=False):
code: str
name: str
plurals: int
formula: str


LANGUAGES: dict[str, LanguageDict]
# Load language names
with open(
"modules/cldr-json/cldr-json/cldr-localenames-full/main/en/languages.json",

View file

@ -9,8 +9,8 @@ import json
import pprint
import subprocess

BASE = "modules/cldr-json/cldr-json/cldr-core/supplemental/plurals.json"
ALIASES = "modules/cldr-json/cldr-json/cldr-core/supplemental/aliases.json"
BASE_FILE = "modules/cldr-json/cldr-json/cldr-core/supplemental/plurals.json"
ALIASES_FILE = "modules/cldr-json/cldr-json/cldr-core/supplemental/aliases.json"

HEADER = '''# Copyright © Michal Čihař <michal@weblate.org>
#
@ -93,7 +93,7 @@ TAG_MAP = {
result = {}
decimals = {}

with open(BASE) as handle:
with open(BASE_FILE) as handle:
data = json.load(handle)

for locale, rules in data["supplemental"]["plurals-type-cardinal"].items():
@ -105,7 +105,7 @@ for locale, rules in data["supplemental"]["plurals-type-cardinal"].items():
decimals[locale] = [name.replace("pluralRule-count-", "") for name in rules]

# Process CLDR
with open(ALIASES) as handle:
with open(ALIASES_FILE) as handle:
aliases = json.load(handle)

for code, alias in aliases["supplemental"]["metadata"]["alias"][

View file

@ -135,7 +135,7 @@ with open("cldr.csv") as csvfile:
except KeyError:
continue
if existing != number:
CLDRPLURALS.append((code, LANGUAGE_NAMES[code], number, equation))
CLDRPLURALS.append([code, LANGUAGE_NAMES[code], number, equation])

# Read default countries
with open("default_countries.csv") as csvfile: