Skip to content

Commit

Permalink
Python: 4.7 runtime and Union fixes (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmcdonald3 authored Aug 6, 2024
1 parent 4c32e93 commit 7644031
Show file tree
Hide file tree
Showing 24 changed files with 48 additions and 53 deletions.
2 changes: 1 addition & 1 deletion TestModels/Aggregate/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion TestModels/Constraints/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Constructor/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion TestModels/Dependencies/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Errors/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Extendable/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Extern/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/LocalService/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/MultipleModels/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Refinement/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Resource/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/Union/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/aws-sdks/ddb/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion TestModels/aws-sdks/kms/runtimes/python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down

This file was deleted.

0 comments on commit 7644031

Please sign in to comment.