Skip to content

Commit 86ba92e

Browse files
committed
feat(ci): run mypy over scripts
1 parent 63ec8f0 commit 86ba92e

File tree

7 files changed

+41
-16
lines changed

7 files changed

+41
-16
lines changed

.github/workflows/mypy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ jobs:
3838
- name: Run mypy
3939
run: |
4040
echo "::add-matcher::.github/matchers/mypy.json"
41-
mypy --show-column-numbers weblate_language_data
41+
mypy --show-column-numbers weblate_language_data scripts/*.py
4242
echo "::remove-matcher owner=mypy::"

scripts/add-iso.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def convert_name(name: str) -> str:
3939
item.get("inverted_name", item["name"])
4040
)
4141

42+
lines: list[list[str]]
43+
4244
with open("languages.csv", newline="") as handle:
4345
reader = csv.reader(handle, delimiter=",")
4446
header = next(reader)
@@ -70,12 +72,17 @@ def convert_name(name: str) -> str:
7072
[
7173
code,
7274
names[code].split(";")[-1].strip(),
73-
plurals,
75+
str(plurals),
7476
formula,
7577
],
7678
)
7779

7880
with open("languages.csv", "w", newline="") as handle:
7981
writer = csv.writer(handle, delimiter=",", lineterminator="\n")
8082
writer.writerow(header)
81-
writer.writerows(sorted(lines, key=itemgetter(0)))
83+
writer.writerows(
84+
sorted(
85+
lines,
86+
key=itemgetter(0), # type: ignore[arg-type]
87+
)
88+
)

scripts/export-cldr-case.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
#
55
# SPDX-License-Identifier: MIT
66

7+
from __future__ import annotations
8+
79
import csv
810
import json
911

1012
# Read languages
1113
with open("languages.csv") as csvfile:
1214
reader = csv.reader(csvfile, delimiter=",")
1315
next(reader)
14-
LANGUAGES = list(reader)
15-
LANGUAGE_CODES = {lang[0] for lang in LANGUAGES}
16+
LANGUAGE_CODES = {lang[0] for lang in reader}
1617

1718
# Read
1819
with open("case-insensitive.csv") as csvfile:
@@ -27,8 +28,12 @@
2728
with open(
2829
"modules/cldr-json/cldr-json/cldr-core/supplemental/languageData.json",
2930
) as handle:
30-
LANGUAGES = json.load(handle)["supplemental"]["languageData"]
31+
LANGUAGES: dict[str, dict[str, str]] = json.load(handle)["supplemental"][
32+
"languageData"
33+
]
3134

35+
base: str
36+
script: str | None
3237
for code in LANGUAGE_CODES:
3338
if "_" in code:
3439
base, script = code.split("_", 1)

scripts/export-cldr-population.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
with open(
1515
"modules/cldr-json/cldr-json/cldr-core/supplemental/territoryInfo.json",
1616
) as handle:
17-
languages = defaultdict(int)
17+
languages: dict[str, float] = defaultdict(float)
1818
for code, territory in json.load(handle)["supplemental"]["territoryInfo"].items():
1919
population = int(territory["_population"])
2020
if "languagePopulation" not in territory:

scripts/export-cldr.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import json
1010
import re
11+
from typing import Literal, TypedDict
1112

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

114115

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

171172

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

196-
raise ValueError(f"Unable to reverse the formula '{formula}'")
199+
raise ValueError(f"Unable to reverse the formula {formula!r}")
197200

198201

199-
def merge_formulas(formulas: list[str]) -> str:
202+
def merge_formulas(formulas: list[str | Literal[True]]) -> str:
200203
max_n = len(formulas) - 1
201204
formula = f"{max_n}"
202205
for n in range(max_n - 1, -1, -1):
203206
part = formulas[n]
207+
if isinstance(part, bool):
208+
raise TypeError(f"Not supported part {part!r}")
204209

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

213218

219+
class LanguageDict(TypedDict, total=False):
220+
code: str
221+
name: str
222+
plurals: int
223+
formula: str
224+
225+
226+
LANGUAGES: dict[str, LanguageDict]
214227
# Load language names
215228
with open(
216229
"modules/cldr-json/cldr-json/cldr-localenames-full/main/en/languages.json",

scripts/export-plural-tags.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import pprint
1010
import subprocess
1111

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

1515
HEADER = '''# Copyright © Michal Čihař <[email protected]>
1616
#
@@ -93,7 +93,7 @@
9393
result = {}
9494
decimals = {}
9595

96-
with open(BASE) as handle:
96+
with open(BASE_FILE) as handle:
9797
data = json.load(handle)
9898

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

107107
# Process CLDR
108-
with open(ALIASES) as handle:
108+
with open(ALIASES_FILE) as handle:
109109
aliases = json.load(handle)
110110

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

scripts/generate-language-data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def get_population(code):
135135
except KeyError:
136136
continue
137137
if existing != number:
138-
CLDRPLURALS.append((code, LANGUAGE_NAMES[code], number, equation))
138+
CLDRPLURALS.append([code, LANGUAGE_NAMES[code], number, equation])
139139

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

0 commit comments

Comments
 (0)