From 242a833e183f860ce6993c80d52fc8f33f4c7162 Mon Sep 17 00:00:00 2001 From: arandito Date: Tue, 4 Nov 2025 11:19:39 -0500 Subject: [PATCH 1/3] Restrict init file gen to package directories --- .../smithy/python/codegen/generators/InitGenerator.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java index 22b7e156d..5c4827e78 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Set; import java.util.stream.Collectors; import software.amazon.smithy.python.codegen.GenerationContext; import software.amazon.smithy.utils.SmithyInternalApi; @@ -15,6 +16,8 @@ */ @SmithyInternalApi public final class InitGenerator implements Runnable { + // Set of directories that need __init__.py files + private static final Set PACKAGE_DIRECTORIES = Set.of("src", "tests"); private final GenerationContext context; @@ -31,6 +34,7 @@ public void run() { .stream() .map(Paths::get) .filter(path -> !path.getParent().equals(context.fileManifest().getBaseDir())) + .filter(path -> PACKAGE_DIRECTORIES.contains(path.getName(0).toString())) .collect(Collectors.groupingBy(Path::getParent, Collectors.toSet())); for (var entry : directories.entrySet()) { var initPath = entry.getKey().resolve("__init__.py"); From c26999c89d42c5533e62badcc4cb2df2c51fa424 Mon Sep 17 00:00:00 2001 From: arandito Date: Tue, 4 Nov 2025 11:54:06 -0500 Subject: [PATCH 2/3] Remove new line after params in operation docstrings --- .../software/amazon/smithy/python/codegen/ClientGenerator.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java index 026e4dfe2..1cac89621 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java @@ -168,8 +168,7 @@ private void writeSharedOperationInit(PythonWriter writer, OperationShape operat writer.write(""" :param plugins: A list of callables that modify the configuration dynamically. Changes made by these plugins only apply for the duration of the operation - execution and will not affect any other operation invocations. - """); + execution and will not affect any other operation invocations."""); }); From b538bd1cf801b5a9f1e13593125d17853289b997 Mon Sep 17 00:00:00 2001 From: arandito Date: Wed, 5 Nov 2025 17:55:04 -0500 Subject: [PATCH 3/3] Add helper to remove trailing spaces --- .../codegen/generators/ConfigGenerator.java | 24 +++++++------- .../generators/StructureGenerator.java | 3 +- .../python/codegen/writer/PythonWriter.java | 31 +++++++++++++++++++ 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java index 4aae8893f..fcd6b9c79 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/ConfigGenerator.java @@ -345,10 +345,7 @@ def __init__( *, ${C|} ): - \"""Constructor. - ${C|} - \""" ${C|} """, configSymbol.getName(), @@ -376,17 +373,20 @@ private void writeInitParams(PythonWriter writer, Collection pro } private void documentProperties(PythonWriter writer, Collection properties) { - var iter = properties.iterator(); - while (iter.hasNext()) { - var property = iter.next(); - var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation())); + writer.writeDocs(() ->{ + var iter = properties.iterator(); + writer.write("\nConstructor.\n"); + while (iter.hasNext()) { + var property = iter.next(); + var docs = writer.formatDocs(String.format(":param %s: %s", property.name(), property.documentation())); + + if (iter.hasNext()) { + docs += "\n"; + } - if (iter.hasNext()) { - docs += "\n"; + writer.write(docs); } - - writer.write(docs); - } + }); } private void initializeProperties(PythonWriter writer, Collection properties) { diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java index 1cfa9c2cf..498938f36 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java @@ -242,7 +242,8 @@ private void writeProperties() { ${?useField}\ field(${?sensitive}repr=False, ${/sensitive}${defaultKey:L}=${defaultValue:L})\ ${/useField} - ${?docs}""\"${docs:L}""\"${/docs}""", memberName, symbolProvider.toSymbol(member)); + ${?docs}""\"${docs:L}""\"${/docs} + """, memberName, symbolProvider.toSymbol(member)); writer.popState(); } } diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/writer/PythonWriter.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/writer/PythonWriter.java index 6d3ecac94..5ac42637e 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/writer/PythonWriter.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/writer/PythonWriter.java @@ -127,6 +127,8 @@ public PythonWriter writeDocs(Runnable runnable) { pushState(); writeInline("\"\"\""); runnable.run(); + trimTrailingWhitespaces(); + ensureNewline(); write("\"\"\""); popState(); return this; @@ -143,6 +145,35 @@ public PythonWriter writeDocs(String docs) { return this; } + /** + * Trims all trailing whitespace from the writer buffer. + * + * @return Returns the writer. + */ + public PythonWriter trimTrailingWhitespaces() { + // Disable the writer formatting config to ensure toString() + // returns the unmodified state of the underlying StringBuilder + trimBlankLines(-1); + trimTrailingSpaces(false); + + String current = super.toString(); + int end = current.length() - 1; + while (end >= 0 && Character.isWhitespace(current.charAt(end))) { + end--; + } + + String trailing = current.substring(end + 1); + if (!trailing.isEmpty()) { + unwrite(trailing); + } + + // Re-enable the formatting config + trimBlankLines(); + trimTrailingSpaces(true); + + return this; + } + private static final int MAX_LINE_LENGTH = CodegenUtils.MAX_PREFERRED_LINE_LENGTH - 8; /**