Skip to content

Commit dcc45dc

Browse files
feat(Python): Format docstrings (#586)
Format docstrings. See https://aws-cryptographic-material-providers-library.readthedocs.io/en/latest/generated/aws_cryptographic_materialproviders.smithygenerated.aws_cryptography_keystore.models.html for an example. Before, the parameters and function descriptions were not displaying correctly because the docstrings weren't formatted. Now, they are appearing correctly.
1 parent 779d38d commit dcc45dc

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

codegen/smithy-dafny-codegen-modules/smithy-python/codegen/smithy-python-codegen/src/main/java/software/amazon/smithy/python/codegen/DirectedPythonCodegen.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ public void customizeAfterIntegrations(CustomizeDirective<GenerationContext, Pyt
338338
CodegenUtils.runCommand("python3 " + file, fileManifest.getBaseDir());
339339
}
340340
formatCode(fileManifest);
341+
formatDocstrings(fileManifest);
341342
runMypy(fileManifest);
342343
}
343344

@@ -352,6 +353,17 @@ private void formatCode(FileManifest fileManifest) {
352353
CodegenUtils.runCommand("python3 -m black . --exclude \"\"", fileManifest.getBaseDir());
353354
}
354355

356+
private void formatDocstrings(FileManifest fileManifest) {
357+
try {
358+
CodegenUtils.runCommand("python3 -m docformatter -h", fileManifest.getBaseDir());
359+
} catch (CodegenException e) {
360+
LOGGER.warning("Unable to find the python package docformatter. Skipping formatting.");
361+
return;
362+
}
363+
LOGGER.info("Running docformatter on generated code");
364+
CodegenUtils.runCommand("python3 -m docformatter --recursive .", fileManifest.getBaseDir());
365+
}
366+
355367
private void runMypy(FileManifest fileManifest) {
356368
try {
357369
CodegenUtils.runCommand("python3 -m mypy -h", fileManifest.getBaseDir());

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithypython/localservice/extensions/DafnyPythonLocalServiceStructureGenerator.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,6 @@ protected void writeFromDict(boolean isError) {
374374
() -> {
375375
writer.writeDocs(() -> {
376376
writer.write("Creates a $L from a dictionary.\n", shapeName);
377-
writer.write(
378-
writer.formatDocs(
379-
"""
380-
The dictionary is expected to use the modeled shape names rather \
381-
than the parameter names as keys to be mostly compatible with boto3."""
382-
)
383-
);
384377
});
385378

386379
if (shape.members().isEmpty() && !isError) {
@@ -523,13 +516,6 @@ protected void writeAsDict(boolean isError) {
523516
"Converts the $L to a dictionary.\n",
524517
symbolProvider.toSymbol(shape).getName()
525518
);
526-
writer.write(
527-
writer.formatDocs(
528-
"""
529-
The dictionary uses the modeled shape names rather than the parameter names \
530-
as keys to be mostly compatible with boto3."""
531-
)
532-
);
533519
});
534520

535521
// If there aren't any optional members, it's best to return immediately.

0 commit comments

Comments
 (0)