Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-749.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: order union properties to avoid type collision
links:
- https://github.com/palantir/conjure-python/pull/749
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,16 @@ default void emit(PythonPoetWriter poetWriter) {
poetWriter.decreaseIndent();

// python @builtins.property for each member of the union
options().forEach(option -> {
poetWriter.writeLine();

poetWriter.writeIndentedLine("@builtins.property");
poetWriter.writeIndentedLine(
String.format("def %s(self) -> Optional[%s]:", propertyName(option), option.myPyType()));
options().stream()
.filter(option -> !propertyName(option).equals("float"))
.forEach(option -> emitProperty(poetWriter, option));

poetWriter.increaseIndent();
option.docs().ifPresent(docs -> {
poetWriter.writeIndentedLine("\"\"\"");
poetWriter.writeIndentedLine(docs.get().trim());
poetWriter.writeIndentedLine("\"\"\"");
});
poetWriter.writeIndentedLine(String.format("return self.%s", fieldName(option)));
poetWriter.decreaseIndent();
});
// properties with builtin names must come after other properties
// can be removed once these names are properly sanitized but
// that will require a breaking change and major version bump
options().stream()
.filter(option -> propertyName(option).equals("float"))
.forEach(option -> emitProperty(poetWriter, option));

String visitorName = String.format("%sVisitor", className());
String definitionVisitorName = String.format("%sVisitor", definitionName());
Expand Down Expand Up @@ -246,6 +240,23 @@ default void emit(PythonPoetWriter poetWriter) {
});
}

private static void emitProperty(PythonPoetWriter poetWriter, PythonField option) {
poetWriter.writeLine();

poetWriter.writeIndentedLine("@builtins.property");
poetWriter.writeIndentedLine(
String.format("def %s(self) -> Optional[%s]:", propertyName(option), option.myPyType()));

poetWriter.increaseIndent();
option.docs().ifPresent(docs -> {
poetWriter.writeIndentedLine("\"\"\"");
poetWriter.writeIndentedLine(docs.get().trim());
poetWriter.writeIndentedLine("\"\"\"");
});
poetWriter.writeIndentedLine(String.format("return self.%s", fieldName(option)));
poetWriter.decreaseIndent();
}

class Builder extends ImmutableUnionSnippet.Builder {}

static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,9 @@ types:
alias: RecursiveObjectAlias
CollectionAliasExample:
alias: map<StringAliasExample, RecursiveObjectAlias>
UnionWithBuiltinVariantName:
union:
# the name of this variant is 'float' which conflicts with the python type 'float'
float: double
# the python type for this field is 'float' which conflicts with the variant name above
double: double

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.