From 76440315ef186e9d027828c2c9f24ebd484a1018 Mon Sep 17 00:00:00 2001 From: Lucas McDonald Date: Tue, 6 Aug 2024 11:06:32 -0700 Subject: [PATCH] Python: 4.7 runtime and Union fixes (#489) --- .../Aggregate/runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../Errors/runtimes/python/pyproject.toml | 2 +- .../Extendable/runtimes/python/pyproject.toml | 2 +- .../Extern/runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../Refinement/runtimes/python/pyproject.toml | 2 +- .../Resource/runtimes/python/pyproject.toml | 2 +- .../SimpleBlob/runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../SimpleEnum/runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../SimpleLong/runtimes/python/pyproject.toml | 2 +- .../runtimes/python/pyproject.toml | 2 +- .../Union/runtimes/python/pyproject.toml | 2 +- .../ddb/runtimes/python/pyproject.toml | 2 +- .../kms/runtimes/python/pyproject.toml | 2 +- .../smithy/python/codegen/UnionGenerator.java | 30 ++++++++++++++++--- .../python-packages/smithy-python/BUILD | 27 ----------------- 24 files changed, 48 insertions(+), 53 deletions(-) delete mode 100644 codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python/BUILD diff --git a/TestModels/Aggregate/runtimes/python/pyproject.toml b/TestModels/Aggregate/runtimes/python/pyproject.toml index a427131bb..4fe123173 100644 --- a/TestModels/Aggregate/runtimes/python/pyproject.toml +++ b/TestModels/Aggregate/runtimes/python/pyproject.toml @@ -12,7 +12,7 @@ include = ["**/smithygenerated/**/*.py", "**/internaldafny/generated/*.py"] [tool.poetry.dependencies] python = "^3.11.0" -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} diff --git a/TestModels/Constraints/runtimes/python/pyproject.toml b/TestModels/Constraints/runtimes/python/pyproject.toml index 30b81dec9..1620a76ab 100644 --- a/TestModels/Constraints/runtimes/python/pyproject.toml +++ b/TestModels/Constraints/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Constructor/runtimes/python/pyproject.toml b/TestModels/Constructor/runtimes/python/pyproject.toml index 268de6b19..124522862 100644 --- a/TestModels/Constructor/runtimes/python/pyproject.toml +++ b/TestModels/Constructor/runtimes/python/pyproject.toml @@ -12,7 +12,7 @@ include = ["**/smithygenerated/**/*.py", "**/internaldafny/generated/*.py"] [tool.poetry.dependencies] python = "^3.11.0" -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} diff --git a/TestModels/Dependencies/runtimes/python/pyproject.toml b/TestModels/Dependencies/runtimes/python/pyproject.toml index 034254e27..24d7ae324 100644 --- a/TestModels/Dependencies/runtimes/python/pyproject.toml +++ b/TestModels/Dependencies/runtimes/python/pyproject.toml @@ -20,7 +20,7 @@ simple-resources = { path = "../../../Resource/runtimes/python", develop = false simple-errors = { path = "../../../Errors/runtimes/python", develop = false} simple-extendable-resources = { path = "../../../Extendable/runtimes/python", develop = false} simple-constraints = { path = "../../../Constraints/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Errors/runtimes/python/pyproject.toml b/TestModels/Errors/runtimes/python/pyproject.toml index 7210ada00..f75fa9dd1 100644 --- a/TestModels/Errors/runtimes/python/pyproject.toml +++ b/TestModels/Errors/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Extendable/runtimes/python/pyproject.toml b/TestModels/Extendable/runtimes/python/pyproject.toml index f308b5421..68973752c 100644 --- a/TestModels/Extendable/runtimes/python/pyproject.toml +++ b/TestModels/Extendable/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Extern/runtimes/python/pyproject.toml b/TestModels/Extern/runtimes/python/pyproject.toml index e4eae1721..498fefee1 100644 --- a/TestModels/Extern/runtimes/python/pyproject.toml +++ b/TestModels/Extern/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/LanguageSpecificLogic/runtimes/python/pyproject.toml b/TestModels/LanguageSpecificLogic/runtimes/python/pyproject.toml index c6b75b9d8..b14c23827 100644 --- a/TestModels/LanguageSpecificLogic/runtimes/python/pyproject.toml +++ b/TestModels/LanguageSpecificLogic/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/LocalService/runtimes/python/pyproject.toml b/TestModels/LocalService/runtimes/python/pyproject.toml index 877fd7702..b5a4415dc 100644 --- a/TestModels/LocalService/runtimes/python/pyproject.toml +++ b/TestModels/LocalService/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/MultipleModels/runtimes/python/pyproject.toml b/TestModels/MultipleModels/runtimes/python/pyproject.toml index e30bcc428..7b5982ac9 100644 --- a/TestModels/MultipleModels/runtimes/python/pyproject.toml +++ b/TestModels/MultipleModels/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Refinement/runtimes/python/pyproject.toml b/TestModels/Refinement/runtimes/python/pyproject.toml index 366b8ed99..6d50d9164 100644 --- a/TestModels/Refinement/runtimes/python/pyproject.toml +++ b/TestModels/Refinement/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Resource/runtimes/python/pyproject.toml b/TestModels/Resource/runtimes/python/pyproject.toml index ddcb96842..7a8237f31 100644 --- a/TestModels/Resource/runtimes/python/pyproject.toml +++ b/TestModels/Resource/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleBlob/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleBlob/runtimes/python/pyproject.toml index db7638ba4..3b2b3958c 100644 --- a/TestModels/SimpleTypes/SimpleBlob/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleBlob/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleDouble/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleDouble/runtimes/python/pyproject.toml index d40c6fe2d..34743fb74 100644 --- a/TestModels/SimpleTypes/SimpleDouble/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleDouble/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleEnum/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleEnum/runtimes/python/pyproject.toml index 15d242992..ee545ac84 100644 --- a/TestModels/SimpleTypes/SimpleEnum/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleEnum/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleEnumV2/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleEnumV2/runtimes/python/pyproject.toml index 3720c6078..193575dcf 100644 --- a/TestModels/SimpleTypes/SimpleEnumV2/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleEnumV2/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleInteger/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleInteger/runtimes/python/pyproject.toml index 374d18a8d..f9cd1f100 100644 --- a/TestModels/SimpleTypes/SimpleInteger/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleInteger/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleLong/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleLong/runtimes/python/pyproject.toml index cced1bc4f..c6d4eff19 100644 --- a/TestModels/SimpleTypes/SimpleLong/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleLong/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/SimpleTypes/SimpleString/runtimes/python/pyproject.toml b/TestModels/SimpleTypes/SimpleString/runtimes/python/pyproject.toml index c97385d3e..66633d229 100644 --- a/TestModels/SimpleTypes/SimpleString/runtimes/python/pyproject.toml +++ b/TestModels/SimpleTypes/SimpleString/runtimes/python/pyproject.toml @@ -15,7 +15,7 @@ python = "^3.11.0" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/Union/runtimes/python/pyproject.toml b/TestModels/Union/runtimes/python/pyproject.toml index 43a0f709f..489fe057e 100644 --- a/TestModels/Union/runtimes/python/pyproject.toml +++ b/TestModels/Union/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ python = "^3.11.0" smithy-python = { path = "../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/aws-sdks/ddb/runtimes/python/pyproject.toml b/TestModels/aws-sdks/ddb/runtimes/python/pyproject.toml index 862e739d7..1a6b4e0b7 100644 --- a/TestModels/aws-sdks/ddb/runtimes/python/pyproject.toml +++ b/TestModels/aws-sdks/ddb/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ boto3 = "^1.28.38" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/TestModels/aws-sdks/kms/runtimes/python/pyproject.toml b/TestModels/aws-sdks/kms/runtimes/python/pyproject.toml index b0927f374..0492d5af9 100644 --- a/TestModels/aws-sdks/kms/runtimes/python/pyproject.toml +++ b/TestModels/aws-sdks/kms/runtimes/python/pyproject.toml @@ -16,7 +16,7 @@ boto3 = "^1.28.38" # TODO: Depend on PyPi once Smithy-Python publishes their Python package there smithy-python = { path = "../../../../../codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python", develop = false} standard-library = {path = "../../../../dafny-dependencies/StandardLibrary/runtimes/python", develop = false} -DafnyRuntimePython = "^4.4.0" +DafnyRuntimePython = "^4.7.0" [tool.poetry.group.test.dependencies] pytest = "^7.4.0" diff --git a/codegen/smithy-dafny-codegen-modules/smithy-python/codegen/smithy-python-codegen/src/main/java/software/amazon/smithy/python/codegen/UnionGenerator.java b/codegen/smithy-dafny-codegen-modules/smithy-python/codegen/smithy-python-codegen/src/main/java/software/amazon/smithy/python/codegen/UnionGenerator.java index ad6a5159c..9fb8ec256 100644 --- a/codegen/smithy-dafny-codegen-modules/smithy-python/codegen/smithy-python-codegen/src/main/java/software/amazon/smithy/python/codegen/UnionGenerator.java +++ b/codegen/smithy-dafny-codegen-modules/smithy-python/codegen/smithy-python-codegen/src/main/java/software/amazon/smithy/python/codegen/UnionGenerator.java @@ -26,6 +26,8 @@ import software.amazon.smithy.model.traits.DocumentationTrait; import software.amazon.smithy.utils.StringUtils; +import static java.lang.String.format; + /** * Renders unions. */ @@ -55,6 +57,17 @@ protected void writeInitMethodConstraintsChecksForMember(MemberShape member, Str // Stub method that can be overridden by other codegens. } + protected void writeInitMethodForMember(MemberShape member, Symbol memberSymbol, Shape targetShape, Symbol targetSymbol) { + String formatString = format("def __init__(self, value: %s):", getTargetFormat(member)); + writer.openBlock(formatString, + "", + targetSymbol, + () -> { + writeInitMethodConstraintsChecksForMember(member, memberSymbol.getName()); + writer.write("self.value = value"); + }); + } + @Override public void run() { var parentName = symbolProvider.toSymbol(shape).getName(); @@ -73,10 +86,8 @@ public void run() { member.getMemberTrait(model, DocumentationTrait.class).ifPresent(trait -> { writer.writeDocs(trait.getValue()); }); - writer.openBlock("def __init__(self, value: $T):", "", targetSymbol, () -> { - writeInitMethodConstraintsChecksForMember(member, memberSymbol.getName()); - writer.write("self.value = value"); - }); + + writeInitMethodForMember(member, memberSymbol, target, targetSymbol); writer.openBlock("def as_dict(self) -> Dict[str, Any]:", "", () -> { if (target.isStructureShape() || target.isUnionShape()) { @@ -161,6 +172,17 @@ def __repr__(self) -> str: writeGlobalFromDict(); } + private String getTargetFormat(MemberShape member) { + Shape target = model.expectShape(member.getTarget()); + // Recursive shapes may be referenced before they're defined even with + // a topological sort. So forward references need to be used when + // referencing them. + if (recursiveShapes.contains(target)) { + return "'$T'"; + } + return "$T"; + } + private void writeGlobalFromDict() { var parentSymbol = symbolProvider.toSymbol(shape); var fromDictSymbol = parentSymbol.expectProperty("fromDict", Symbol.class); diff --git a/codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python/BUILD b/codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python/BUILD deleted file mode 100644 index 8f4298bb8..000000000 --- a/codegen/smithy-dafny-codegen-modules/smithy-python/python-packages/smithy-python/BUILD +++ /dev/null @@ -1,27 +0,0 @@ -resource(name="pyproject", source="pyproject.toml") -resource(name="readme", source="README.md") -resource(name="notice", source="NOTICE") - -python_distribution( - name="dist", - dependencies=[ - ":pyproject", - ":readme", - ":notice", - ":requirements", - "python-packages/smithy-python/smithy_python:source", - ], - provides=python_artifact( - name="smithy_python", - version="0.0.1", - ) -) - -python_requirements( - name="dev-requirements", - source="requirements-dev.txt", -) - -python_requirements( - name="requirements", -)