Skip to content

Commit 80c817a

Browse files
Prepare Python Codegen for mypyc (#988)
* Modernize type annotations for Python This commit modernizes the type annotations for the Python codegen, substituting `Optional` types with the `None` union syntax. * Enable mypyc
1 parent 59fa91c commit 80c817a

File tree

4 files changed

+404
-341
lines changed

4 files changed

+404
-341
lines changed

schema_salad/codegen.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def codegen(
3838
"""Generate classes with loaders for the given Schema Salad description."""
3939
j = schema.extend_and_specialize(i, loader)
4040

41-
gen: CodeGenBase | None = None
41+
gen: CodeGenBase
4242
base = schema_metadata.get("$base", schema_metadata.get("id"))
4343
# ``urlsplit`` decides whether to return an encoded result based
4444
# on the object type. To ensure the code behaves the same for Py
@@ -54,53 +54,55 @@ def codegen(
5454
info = parser_info or pkg
5555
salad_version = schema_metadata.get("saladVersion", "v1.1")
5656

57-
if lang in {"python", "cpp", "dlang"}:
58-
if target:
59-
dest: TextIOWrapper | TextIO = open(target, mode="w", encoding="utf-8")
60-
else:
61-
dest = sys.stdout
62-
if lang == "cpp":
63-
gen = CppCodeGen(
64-
base,
65-
dest,
66-
examples,
67-
pkg,
68-
copyright,
69-
spdx_copyright_text,
70-
spdx_license_identifier,
71-
)
72-
gen.parse(j)
73-
return
74-
if lang == "dlang":
75-
gen = DlangCodeGen(
57+
match lang:
58+
case "python" | "cpp" | "dlang":
59+
if target:
60+
dest: TextIOWrapper | TextIO = open(target, mode="w", encoding="utf-8")
61+
else:
62+
dest = sys.stdout
63+
match lang:
64+
case "cpp":
65+
gen = CppCodeGen(
66+
base,
67+
dest,
68+
examples,
69+
pkg,
70+
copyright,
71+
spdx_copyright_text,
72+
spdx_license_identifier,
73+
)
74+
gen.parse(j)
75+
return
76+
case "dlang":
77+
gen = DlangCodeGen(
78+
base,
79+
dest,
80+
examples,
81+
pkg,
82+
copyright,
83+
info,
84+
salad_version,
85+
)
86+
gen.parse(j)
87+
return
88+
case "python":
89+
gen = PythonCodeGen(
90+
dest, copyright=copyright, parser_info=info, salad_version=salad_version
91+
)
92+
case "java":
93+
gen = JavaCodeGen(
7694
base,
77-
dest,
78-
examples,
79-
pkg,
80-
copyright,
81-
info,
82-
salad_version,
95+
target=target,
96+
examples=examples,
97+
package=pkg,
98+
copyright=copyright,
8399
)
84-
gen.parse(j)
85-
return
86-
gen = PythonCodeGen(
87-
dest, copyright=copyright, parser_info=info, salad_version=salad_version
88-
)
89-
90-
elif lang == "java":
91-
gen = JavaCodeGen(
92-
base,
93-
target=target,
94-
examples=examples,
95-
package=pkg,
96-
copyright=copyright,
97-
)
98-
elif lang == "typescript":
99-
gen = TypeScriptCodeGen(base, target=target, package=pkg, examples=examples)
100-
elif lang == "dotnet":
101-
gen = DotNetCodeGen(base, target=target, package=pkg, examples=examples)
102-
else:
103-
raise SchemaSaladException(f"Unsupported code generation language {lang!r}")
100+
case "typescript":
101+
gen = TypeScriptCodeGen(base, target=target, package=pkg, examples=examples)
102+
case "dotnet":
103+
gen = DotNetCodeGen(base, target=target, package=pkg, examples=examples)
104+
case _:
105+
raise SchemaSaladException(f"Unsupported code generation language {lang!r}")
104106

105107
gen.prologue()
106108

0 commit comments

Comments
 (0)