88
99import json
1010import re
11+ from typing import Literal , TypedDict
1112
1213MAPPINGS = {
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
215228with open (
216229 "modules/cldr-json/cldr-json/cldr-localenames-full/main/en/languages.json" ,
0 commit comments