124124
125125def load_pinned () -> list [dict [str , str ]]:
126126 """Read `schema/PINNED.json` and verify each vendored file's sha256."""
127- entries : list [dict [str , str ]] = json .loads ((SCHEMA_DIR / "PINNED.json" ).read_text ())
127+ entries : list [dict [str , str ]] = json .loads ((SCHEMA_DIR / "PINNED.json" ).read_text (encoding = "utf-8" ))
128128 for entry in entries :
129129 path = SCHEMA_DIR / f"{ entry ['protocol_version' ]} .json"
130130 actual = hashlib .sha256 (path .read_bytes ()).hexdigest ()
@@ -189,7 +189,7 @@ def run_codegen(schema_path: Path, output_path: Path) -> None:
189189 "--type-mappings" , "byte=string" , "uri=string" , "uri-template=string" ,
190190 "--disable-timestamp" ,
191191 ],
192- capture_output = True , text = True ,
192+ capture_output = True , encoding = "utf-8" , errors = "replace" ,
193193 )
194194 # fmt: on
195195 if result .returncode != 0 :
@@ -222,16 +222,16 @@ def patch(match: re.Match[str]) -> str:
222222def build (entry : dict [str , str ]) -> str :
223223 """Generate, post-process, and format one version's surface module text."""
224224 version = entry ["protocol_version" ]
225- schema = json .loads ((SCHEMA_DIR / f"{ version } .json" ).read_text ())
225+ schema = json .loads ((SCHEMA_DIR / f"{ version } .json" ).read_text (encoding = "utf-8" ))
226226 patch_schema (schema , SCHEMA_PATCHES .get (version , []))
227227 make_server_info_opaque (schema )
228228
229229 with tempfile .TemporaryDirectory () as tmp :
230230 patched = Path (tmp ) / "schema.json"
231- patched .write_text (json .dumps (schema ))
231+ patched .write_text (json .dumps (schema ), encoding = "utf-8" )
232232 raw = Path (tmp ) / "raw.py"
233233 run_codegen (patched , raw )
234- source = raw .read_text ()
234+ source = raw .read_text (encoding = "utf-8" )
235235
236236 source = re .sub (r"\A# generated by datamodel-codegen:\n#[^\n]*\n" , "" , source )
237237 source = re .sub (r"^class Model\(RootModel\[Any\]\):\n {4}root: Any\n+" , "" , source , count = 1 , flags = re .MULTILINE )
@@ -253,12 +253,12 @@ def build(entry: dict[str, str]) -> str:
253253
254254 staging = TYPES_DIR / f"_staging_{ version } .py"
255255 try :
256- staging .write_text (source )
256+ staging .write_text (source , encoding = "utf-8" )
257257 subprocess .run (
258258 ["uv" , "run" , "--frozen" , "ruff" , "format" , "--no-cache" , str (staging )],
259259 cwd = REPO_ROOT , capture_output = True , check = True ,
260260 ) # fmt: skip
261- return staging .read_text ()
261+ return staging .read_text (encoding = "utf-8" )
262262 finally :
263263 staging .unlink (missing_ok = True )
264264
@@ -275,10 +275,10 @@ def main(argv: list[str] | None = None) -> int:
275275 candidate = build (entry )
276276 if not args .check :
277277 target .parent .mkdir (parents = True , exist_ok = True )
278- target .write_text (candidate )
278+ target .write_text (candidate , encoding = "utf-8" )
279279 print (f"{ entry ['protocol_version' ]} : wrote { target .relative_to (REPO_ROOT )} ({ len (candidate )} bytes)" )
280280 continue
281- committed = target .read_text () if target .is_file () else ""
281+ committed = target .read_text (encoding = "utf-8" ) if target .is_file () else ""
282282 if committed != candidate :
283283 drift = True
284284 sys .stderr .writelines (
0 commit comments