diff --git a/changelog/@unreleased/pr-746.v2.yml b/changelog/@unreleased/pr-746.v2.yml new file mode 100644 index 000000000..0723b5f59 --- /dev/null +++ b/changelog/@unreleased/pr-746.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: union with builtin variant name bug + links: + - https://github.com/palantir/conjure-python/pull/746 diff --git a/conjure-python-core/src/main/java/com/palantir/conjure/python/processors/PythonIdentifierSanitizer.java b/conjure-python-core/src/main/java/com/palantir/conjure/python/processors/PythonIdentifierSanitizer.java index 23ec9a510..b4c4d6797 100644 --- a/conjure-python-core/src/main/java/com/palantir/conjure/python/processors/PythonIdentifierSanitizer.java +++ b/conjure-python-core/src/main/java/com/palantir/conjure/python/processors/PythonIdentifierSanitizer.java @@ -37,6 +37,7 @@ public final class PythonIdentifierSanitizer { "except", "exec", "finally", + "float", "for", "from", "global", diff --git a/conjure-python-core/src/test/resources/types/example-types.yml b/conjure-python-core/src/test/resources/types/example-types.yml index aa4814fe4..8c89b4615 100644 --- a/conjure-python-core/src/test/resources/types/example-types.yml +++ b/conjure-python-core/src/test/resources/types/example-types.yml @@ -172,3 +172,9 @@ types: alias: RecursiveObjectAlias CollectionAliasExample: alias: map + 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 diff --git a/conjure-python-core/src/test/resources/types/expected/package_name/_impl.py b/conjure-python-core/src/test/resources/types/expected/package_name/_impl.py index 239b3861d..3687260a1 100644 --- a/conjure-python-core/src/test/resources/types/expected/package_name/_impl.py +++ b/conjure-python-core/src/test/resources/types/expected/package_name/_impl.py @@ -1594,6 +1594,83 @@ def _property(self, property: int) -> Any: product_UnionTypeExampleVisitor.__module__ = "package_name.product" +class product_UnionWithBuiltinVariantName(ConjureUnionType): + _float_: Optional[float] = None + _double: Optional[float] = None + + @builtins.classmethod + def _options(cls) -> Dict[str, ConjureFieldDefinition]: + return { + 'float_': ConjureFieldDefinition('float', float), + 'double': ConjureFieldDefinition('double', float) + } + + def __init__( + self, + float_: Optional[float] = None, + double: Optional[float] = None, + type_of_union: Optional[str] = None + ) -> None: + if type_of_union is None: + if (float_ is not None) + (double is not None) != 1: + raise ValueError('a union must contain a single member') + + if float_ is not None: + self._float_ = float_ + self._type = 'float' + if double is not None: + self._double = double + self._type = 'double' + + elif type_of_union == 'float': + if float_ is None: + raise ValueError('a union value must not be None') + self._float_ = float_ + self._type = 'float' + elif type_of_union == 'double': + if double is None: + raise ValueError('a union value must not be None') + self._double = double + self._type = 'double' + + @builtins.property + def float_(self) -> Optional[float]: + return self._float_ + + @builtins.property + def double(self) -> Optional[float]: + return self._double + + def accept(self, visitor) -> Any: + if not isinstance(visitor, product_UnionWithBuiltinVariantNameVisitor): + raise ValueError('{} is not an instance of product_UnionWithBuiltinVariantNameVisitor'.format(visitor.__class__.__name__)) + if self._type == 'float' and self.float_ is not None: + return visitor._float(self.float_) + if self._type == 'double' and self.double is not None: + return visitor._double(self.double) + + +product_UnionWithBuiltinVariantName.__name__ = "UnionWithBuiltinVariantName" +product_UnionWithBuiltinVariantName.__qualname__ = "UnionWithBuiltinVariantName" +product_UnionWithBuiltinVariantName.__module__ = "package_name.product" + + +class product_UnionWithBuiltinVariantNameVisitor: + + @abstractmethod + def _float(self, float_: float) -> Any: + pass + + @abstractmethod + def _double(self, double: float) -> Any: + pass + + +product_UnionWithBuiltinVariantNameVisitor.__name__ = "UnionWithBuiltinVariantNameVisitor" +product_UnionWithBuiltinVariantNameVisitor.__qualname__ = "UnionWithBuiltinVariantNameVisitor" +product_UnionWithBuiltinVariantNameVisitor.__module__ = "package_name.product" + + class product_UuidExample(ConjureBeanType): @builtins.classmethod diff --git a/conjure-python-core/src/test/resources/types/expected/package_name/product/__init__.py b/conjure-python-core/src/test/resources/types/expected/package_name/product/__init__.py index d92acf564..31cbf3ffd 100644 --- a/conjure-python-core/src/test/resources/types/expected/package_name/product/__init__.py +++ b/conjure-python-core/src/test/resources/types/expected/package_name/product/__init__.py @@ -43,6 +43,8 @@ product_StringExample as StringExample, product_UnionTypeExample as UnionTypeExample, product_UnionTypeExampleVisitor as UnionTypeExampleVisitor, + product_UnionWithBuiltinVariantName as UnionWithBuiltinVariantName, + product_UnionWithBuiltinVariantNameVisitor as UnionWithBuiltinVariantNameVisitor, product_UuidAliasExample as UuidAliasExample, product_UuidExample as UuidExample, )