diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerAttributeUtils.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerAttributeUtils.kt deleted file mode 100644 index f65eb27d10..0000000000 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerAttributeUtils.kt +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.rust.codegen.server.python.smithy.generators - -import software.amazon.smithy.model.shapes.Shape -import software.amazon.smithy.model.traits.ErrorTrait -import software.amazon.smithy.rust.codegen.rustlang.RustWriter -import software.amazon.smithy.rust.codegen.rustlang.asType -import software.amazon.smithy.rust.codegen.rustlang.rustTemplate -import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency -import software.amazon.smithy.rust.codegen.util.hasTrait - -/** - * This module contains utilities to render PyO3 attributes. - * - * TODO(https://github.com/awslabs/smithy-rs/issues/1465): Switch to `Attribute.Custom` and get rid of this class. - */ - -private val codegenScope = arrayOf( - "pyo3" to PythonServerCargoDependency.PyO3.asType(), -) - -// Renders #[pyo3::pyclass] attribute, inheriting from `Exception` if the shape has the `ErrorTrait` attached. -fun RustWriter.renderPyClass(shape: Shape) { - if (shape.hasTrait()) { - rustTemplate("##[#{pyo3}::pyclass(extends = #{pyo3}::exceptions::PyException)]", *codegenScope) - } else { - rustTemplate("##[#{pyo3}::pyclass]", *codegenScope) - } -} - -// Renders #[pyo3::pymethods] attribute. -fun RustWriter.renderPyMethods() { - rustTemplate("##[#{pyo3}::pymethods]", *codegenScope) -} - -// Renders #[pyo3(get, set)] attribute. -fun RustWriter.renderPyGetterSetter() { - rustTemplate("##[#{pyo3}(get, set)]", *codegenScope) -} diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerEnumGenerator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerEnumGenerator.kt index 4cb1708a77..e7f3eced53 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerEnumGenerator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerEnumGenerator.kt @@ -8,8 +8,11 @@ package software.amazon.smithy.rust.codegen.server.python.smithy.generators import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.traits.EnumTrait +import software.amazon.smithy.rust.codegen.rustlang.Attribute import software.amazon.smithy.rust.codegen.rustlang.RustWriter +import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.rust +import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerEnumGenerator import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider @@ -28,19 +31,26 @@ class PythonServerEnumGenerator( runtimeConfig: RuntimeConfig, ) : ServerEnumGenerator(model, symbolProvider, writer, shape, enumTrait, runtimeConfig) { + private val pyo3Symbols = listOf(PythonServerCargoDependency.PyO3.asType()) + override fun render() { - writer.renderPyClass(shape) + renderPyClass() super.render() renderPyO3Methods() } + private fun renderPyClass() { + Attribute.Custom("pyo3::pyclass", symbols = pyo3Symbols).render(writer) + } + override fun renderFromForStr() { - writer.renderPyClass(shape) + renderPyClass() super.renderFromForStr() } private fun renderPyO3Methods() { - writer.renderPyMethods() + Attribute.Custom("pyo3::pymethods", symbols = pyo3Symbols).render(writer) + writer.rust( """ impl $enumName { diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerStructureGenerator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerStructureGenerator.kt index 7c3b59d2e0..8e15cf86aa 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerStructureGenerator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerStructureGenerator.kt @@ -10,12 +10,15 @@ import software.amazon.smithy.model.Model import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.traits.ErrorTrait +import software.amazon.smithy.rust.codegen.rustlang.Attribute import software.amazon.smithy.rust.codegen.rustlang.RustWriter import software.amazon.smithy.rust.codegen.rustlang.Writable +import software.amazon.smithy.rust.codegen.rustlang.asType import software.amazon.smithy.rust.codegen.rustlang.render import software.amazon.smithy.rust.codegen.rustlang.rust import software.amazon.smithy.rust.codegen.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.rustlang.writable +import software.amazon.smithy.rust.codegen.server.python.smithy.PythonServerCargoDependency import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider import software.amazon.smithy.rust.codegen.smithy.generators.StructureGenerator import software.amazon.smithy.rust.codegen.smithy.rustType @@ -33,20 +36,25 @@ open class PythonServerStructureGenerator( private val shape: StructureShape ) : StructureGenerator(model, symbolProvider, writer, shape) { + private val pyo3Symbols = listOf(PythonServerCargoDependency.PyO3.asType()) + override fun renderStructure() { - writer.renderPyClass(shape) + if (shape.hasTrait()) + Attribute.Custom("pyo3::pyclass(extends = pyo3::exceptions::PyException)", symbols = pyo3Symbols).render(writer) + else + Attribute.Custom("pyo3::pyclass", symbols = pyo3Symbols).render(writer) super.renderStructure() renderPyO3Methods() } override fun renderStructureMember(writer: RustWriter, member: MemberShape, memberName: String, memberSymbol: Symbol) { - writer.renderPyGetterSetter() + Attribute.Custom("pyo3(get, set)", symbols = pyo3Symbols).render(writer) super.renderStructureMember(writer, member, memberName, memberSymbol) } private fun renderPyO3Methods() { if (shape.hasTrait() || accessorMembers.isNotEmpty()) { - writer.renderPyMethods() + Attribute.Custom("pyo3::pymethods", symbols = pyo3Symbols).render(writer) writer.rustTemplate( """ impl $name {