Skip to content

Commit 97571c7

Browse files
Use ruff formatter in generated clients
1 parent 7f53a5e commit 97571c7

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

codegen/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ clients.
1717
* Use of a
1818
[Python virtual environment](https://docs.python.org/3/library/venv.html) is
1919
highly recommended.
20-
* If `black` is installed in the version of python found on the path, it will be
20+
* If `ruff` is installed in the version of python found on the path, it will be
2121
used to format the generated code.
22-
* If `mypy` is installed in the version of python found on the path, it will be
23-
used to check the generated code. For mypy to pass, the `smithy_core` and
24-
`smithy_http` packages will need to be installed. To install those into your
25-
active environment, run `make install-python-components` from the repository
26-
root.
22+
* If `pyright` is installed in the version of python found on the path, it will be
23+
used to check the generated code.
2724

2825
### Building the generator
2926

codegen/core/src/main/java/software/amazon/smithy/python/codegen/PythonFormatter.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,28 @@ public void run() {
6161
CodegenUtils.runCommand("python3 " + file, fileManifest.getBaseDir());
6262
}
6363
format(fileManifest);
64-
check(fileManifest);
64+
typeCheck(fileManifest);
6565
}
6666

6767
private void format(FileManifest fileManifest) {
6868
try {
69-
CodegenUtils.runCommand("python3 -m black -h", fileManifest.getBaseDir());
69+
CodegenUtils.runCommand("python3 -m ruff -h", fileManifest.getBaseDir());
7070
} catch (CodegenException e) {
71-
LOGGER.warning("Unable to find the python package black. Skipping formatting.");
71+
LOGGER.warning("Unable to find the python package ruff. Skipping formatting.");
7272
return;
7373
}
7474
LOGGER.info("Running code formatter on generated code");
75-
CodegenUtils.runCommand("python3 -m black . --exclude \"\"", fileManifest.getBaseDir());
75+
CodegenUtils.runCommand("python3 -m ruff format", fileManifest.getBaseDir());
7676
}
7777

78-
private void check(FileManifest fileManifest) {
78+
private void typeCheck(FileManifest fileManifest) {
7979
try {
8080
CodegenUtils.runCommand("python3 -m pyright -h", fileManifest.getBaseDir());
8181
} catch (CodegenException e) {
8282
LOGGER.warning("Unable to find the pyright package. Skipping type checking.");
8383
return;
8484
}
85-
LOGGER.info("Running mypy on generated code");
85+
LOGGER.info("Running type checker on generated code");
8686
CodegenUtils.runCommand("python3 -m pyright .", fileManifest.getBaseDir());
8787
}
8888
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/SetupGenerator.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
@SmithyInternalApi
3737
public final class SetupGenerator {
3838

39-
private SetupGenerator() {
40-
}
39+
private SetupGenerator() {}
4140

4241
public static void generateSetup(
4342
PythonSettings settings,
@@ -123,7 +122,7 @@ private static void writePyproject(
123122
[build-system]
124123
requires = ["setuptools", "setuptools-scm", "wheel"]
125124
build-backend = "setuptools.build_meta"
126-
125+
127126
[project]
128127
name = $1S
129128
version = $2S
@@ -159,18 +158,18 @@ private static void writePyproject(
159158
writer.write("""
160159
[tool.setuptools.packages.find]
161160
exclude=["tests*"]
162-
161+
163162
[tool.pyright]
164163
typeCheckingMode = "strict"
165164
reportPrivateUsage = false
166165
reportUnusedFunction = false
167166
reportUnusedVariable = false
168167
reportUnnecessaryComparison = false
169168
reportUnusedClass = false
170-
171-
[tool.black]
172-
target-version = ["py311"]
173-
169+
170+
[tool.ruff]
171+
target-version = "py312"
172+
174173
[tool.pytest.ini_options]
175174
python_classes = ["!Test"]
176175
asyncio_mode = "auto"
@@ -181,17 +180,17 @@ private static void writePyproject(
181180
}
182181

183182
private static void writeDependencyList(PythonWriter writer, Collection<SymbolDependency> dependencies) {
184-
for (var iter = dependencies.iterator(); iter.hasNext(); ) {
183+
for (var iter = dependencies.iterator(); iter.hasNext();) {
185184
writer.pushState();
186185
var dependency = iter.next();
187186
writer.putContext("deps", getOptionalDependencies(dependency));
188187
writer.putContext("isLink", dependency.getProperty(SymbolProperties.IS_LINK).orElse(false));
189188
writer.putContext("last", !iter.hasNext());
190189
writer.write("""
191-
"$L\
192-
${?deps}[${#deps}${value:L}${^key.last}, ${/key.last}${/deps}]${/deps}\
193-
${?isLink} @ ${/isLink}$L"\
194-
${^last},${/last}""",
190+
"$L\
191+
${?deps}[${#deps}${value:L}${^key.last}, ${/key.last}${/deps}]${/deps}\
192+
${?isLink} @ ${/isLink}$L"\
193+
${^last},${/last}""",
195194
dependency.getPackageName(),
196195
dependency.getVersion());
197196
writer.popState();
@@ -236,7 +235,7 @@ private static void writeReadme(
236235
writer.pushState(new ReadmeSection());
237236
writer.write("""
238237
## $L Client
239-
238+
240239
$L
241240
""", title, description);
242241

@@ -249,7 +248,7 @@ private static void writeReadme(
249248
// since the python code docs are RST format.
250249
writer.write("""
251250
### Documentation
252-
251+
253252
$L
254253
""", documentation);
255254
});

codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/RestJsonProtocolGenerator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,9 @@ public void wrapEventStream(GenerationContext context, PythonWriter writer) {
412412
payload_codec=codec,
413413
initial_response=operation_output,
414414
async_writer=execution_context.transport_request.body, # type: ignore
415-
async_reader=AsyncBytesReader(execution_context.transport_response.body), # type: ignore
415+
async_reader=AsyncBytesReader(
416+
execution_context.transport_response.body # type: ignore
417+
),
416418
deserializer=event_deserializer, # type: ignore
417419
)
418420
else:
@@ -425,7 +427,9 @@ public void wrapEventStream(GenerationContext context, PythonWriter writer) {
425427
return AWSOutputEventStream[Any, Any](
426428
payload_codec=codec,
427429
initial_response=operation_output,
428-
async_reader=AsyncBytesReader(execution_context.transport_response.body), # type: ignore
430+
async_reader=AsyncBytesReader(
431+
execution_context.transport_response.body # type: ignore
432+
),
429433
deserializer=event_deserializer, # type: ignore
430434
)
431435
""");

0 commit comments

Comments
 (0)