diff --git a/codegen-output-py/my-handwritten-json-model/.smithy-python-manifest.json b/codegen-output-py/my-handwritten-json-model/.smithy-python-manifest.json index 8b94470d8..ff280c2c7 100644 --- a/codegen-output-py/my-handwritten-json-model/.smithy-python-manifest.json +++ b/codegen-output-py/my-handwritten-json-model/.smithy-python-manifest.json @@ -4,7 +4,7 @@ "pyproject.toml": "321817c332d0760a244c76bedaf7a21c70d92254ab9458b533f347c3860d4db4", "src/my_handwritten_json_model/__init__.py": "619a033058229139a02f912a44a6b8997fb35c5af6736d09edd49927876a49dd", "src/my_handwritten_json_model/_private/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "src/my_handwritten_json_model/_private/schemas.py": "1a1bbc8bafb312919b994a51fc106c76578e1cffa9032fe9bbd9a02c0b102426", + "src/my_handwritten_json_model/_private/schemas.py": "d6a88206b0288f028d923a590d97acb21efc6f5d6d29dacc1393068063ab4435", "src/my_handwritten_json_model/models.py": "c7d9575479a8a6a3580871da76345de34c6ca18a977f271fcf9bd62801715700", "src/my_handwritten_json_model/models.pyi": "8f59462ef2d242ae24b24bbcdb082aa05576aab445d2fa2bc9ae3a9194399bdb", "src/my_handwritten_json_model/py.typed": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" diff --git a/codegen-output-py/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py b/codegen-output-py/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py index b530ffb36..4585f6fc7 100644 --- a/codegen-output-py/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py +++ b/codegen-output-py/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py @@ -1,3 +1,5 @@ +from types import MappingProxyType + from smithy_core._schema_compact import StaticSchema n0 = "smithy.python.tests.handwritten.json" @@ -22,20 +24,21 @@ _s18 = "server" _s19 = "ThrottlingEvent" _s20 = "client" -_s21 = "TradeEvents" -_s22 = "smithy.api#streaming" -_s23 = "alpha" -_s24 = "beta" -_s25 = "gamma" -_s26 = "delta" -_s27 = "throttle" -_s28 = "TradeEventStreamInput" -_s29 = "smithy.api#input" -_s30 = "sessionId" -_s31 = "events" -_s32 = "TradeEventStreamOutput" -_s33 = "smithy.api#output" -_s34 = "greeting" +_s21 = "smithy.api#retryable" +_s22 = "TradeEvents" +_s23 = "smithy.api#streaming" +_s24 = "alpha" +_s25 = "beta" +_s26 = "gamma" +_s27 = "delta" +_s28 = "throttle" +_s29 = "TradeEventStreamInput" +_s30 = "smithy.api#input" +_s31 = "sessionId" +_s32 = "events" +_s33 = "TradeEventStreamOutput" +_s34 = "smithy.api#output" +_s35 = "greeting" ALPHA: StaticSchema = ( 18, @@ -95,7 +98,7 @@ 18, n0, _s19, - {_s17: _s20}, + {_s17: _s20, _s21: MappingProxyType({"throttling": True})}, (_s9,), ((3, 0),), ) @@ -103,26 +106,26 @@ TRADE_EVENTS: StaticSchema = ( 19, n0, - _s21, - {_s22: None}, - (_s23, _s24, _s25, _s26, _s27), + _s22, + {_s23: None}, + (_s24, _s25, _s26, _s27, _s28), ((ALPHA, 0), (-1, 0), (GAMMA, 0), (DIFFERENT_SHAPE_NAME, 0), (THROTTLING_EVENT, 0)), ) TRADE_EVENT_STREAM_INPUT: StaticSchema = ( 18, n0, - _s28, - {_s29: None}, - (_s30, _s31), + _s29, + {_s30: None}, + (_s31, _s32), ((3, 0), (TRADE_EVENTS, 0)), ) TRADE_EVENT_STREAM_OUTPUT: StaticSchema = ( 18, n0, - _s32, - {_s33: None}, - (_s34, _s31), + _s33, + {_s34: None}, + (_s35, _s32), ((3, 0), (TRADE_EVENTS, 0)), ) diff --git a/codegen-output-py/my-handwritten-model/.smithy-python-manifest.json b/codegen-output-py/my-handwritten-model/.smithy-python-manifest.json index c041ea733..2157a8443 100644 --- a/codegen-output-py/my-handwritten-model/.smithy-python-manifest.json +++ b/codegen-output-py/my-handwritten-model/.smithy-python-manifest.json @@ -4,7 +4,7 @@ "pyproject.toml": "88e01463cc0d24fd7ab8bf99d96f0fe3b3f031f3348fc1c371c533c891184053", "src/my_handwritten_model/__init__.py": "619a033058229139a02f912a44a6b8997fb35c5af6736d09edd49927876a49dd", "src/my_handwritten_model/_private/__init__.py": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "src/my_handwritten_model/_private/schemas.py": "dc76275b338c278172c30166e40c964952fdb5e9ae06f49dff33a37a58a39ff7", + "src/my_handwritten_model/_private/schemas.py": "c3078c36bcc780fd15ae3135bc22e8de2415713dadb2b1151a81fd0fd2ba5d0b", "src/my_handwritten_model/models.py": "ca3c2b4d5dab0e7da762ec0feceaeda02fd0656fdeb161e0417686a8b4279e24", "src/my_handwritten_model/models.pyi": "a01e010947193acad3008148301f76d2e5de0dbbe09b149f325d64582b0815a3", "src/my_handwritten_model/py.typed": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" diff --git a/codegen-output-py/my-handwritten-model/src/my_handwritten_model/_private/schemas.py b/codegen-output-py/my-handwritten-model/src/my_handwritten_model/_private/schemas.py index 0acfc6271..0dad2aea1 100644 --- a/codegen-output-py/my-handwritten-model/src/my_handwritten_model/_private/schemas.py +++ b/codegen-output-py/my-handwritten-model/src/my_handwritten_model/_private/schemas.py @@ -1,3 +1,5 @@ +from types import MappingProxyType + from smithy_core._schema_compact import StaticSchema n0 = "smithy.python.tests.handwritten" @@ -47,16 +49,17 @@ _s43 = "subscriptionId" _s44 = "ThrottlingEvent" _s45 = "client" -_s46 = "TradeEvents" -_s47 = "alpha" -_s48 = "beta" -_s49 = "gamma" -_s50 = "delta" -_s51 = "throttle" -_s52 = "TradeEventStreamInput" -_s53 = "sessionId" -_s54 = "TradeEventStreamOutput" -_s55 = "greeting" +_s46 = "smithy.api#retryable" +_s47 = "TradeEvents" +_s48 = "alpha" +_s49 = "beta" +_s50 = "gamma" +_s51 = "delta" +_s52 = "throttle" +_s53 = "TradeEventStreamInput" +_s54 = "sessionId" +_s55 = "TradeEventStreamOutput" +_s56 = "greeting" ALPHA: StaticSchema = ( 18, @@ -206,7 +209,7 @@ 18, n0, _s44, - {_s35: _s45}, + {_s35: _s45, _s46: MappingProxyType({"throttling": True})}, (_s9,), ((3, 0),), ) @@ -214,26 +217,26 @@ TRADE_EVENTS: StaticSchema = ( 19, n0, - _s46, + _s47, {_s24: None}, - (_s47, _s48, _s49, _s50, _s51), + (_s48, _s49, _s50, _s51, _s52), ((ALPHA, 0), (-1, 0), (GAMMA, 0), (DIFFERENT_SHAPE_NAME, 0), (THROTTLING_EVENT, 0)), ) TRADE_EVENT_STREAM_INPUT: StaticSchema = ( 18, n0, - _s52, + _s53, {_s28: None}, - (_s53, _s30), + (_s54, _s30), ((3, 0), (TRADE_EVENTS, 0)), ) TRADE_EVENT_STREAM_OUTPUT: StaticSchema = ( 18, n0, - _s54, + _s55, {_s32: None}, - (_s55, _s30), + (_s56, _s30), ((3, 0), (TRADE_EVENTS, 0)), ) diff --git a/codegen-output/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py b/codegen-output/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py index c66421d04..9d527f6a1 100644 --- a/codegen-output/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py +++ b/codegen-output/my-handwritten-json-model/src/my_handwritten_json_model/_private/schemas.py @@ -27,19 +27,20 @@ _se = "server" _TE = "ThrottlingEvent" _c = "client" +_sm = "smithy.api#retryable" _TEr = "TradeEvents" -_sm = "smithy.api#streaming" +_smi = "smithy.api#streaming" _a = "alpha" _b = "beta" _g = "gamma" _d = "delta" _th = "throttle" _TESI = "TradeEventStreamInput" -_smi = "smithy.api#input" +_smit = "smithy.api#input" _sI = "sessionId" _e = "events" _TESO = "TradeEventStreamOutput" -_smit = "smithy.api#output" +_smith = "smithy.api#output" _gr = "greeting" _TES = "TradeEventStream" _MHJM = "MyHandwrittenJsonModel" @@ -64,13 +65,20 @@ STREAM_SERVICE_ERROR: StaticSchema = (18, n0, _SSE, {_s: _se}, (_me,), ((3, 0),)) -THROTTLING_EVENT: StaticSchema = (18, n0, _TE, {_s: _c}, (_me,), ((3, 0),)) +THROTTLING_EVENT: StaticSchema = ( + 18, + n0, + _TE, + {_s: _c, _sm: MappingProxyType({"throttling": True})}, + (_me,), + ((3, 0),), +) TRADE_EVENTS: StaticSchema = ( 19, n0, _TEr, - {_sm: None}, + {_smi: None}, (_a, _b, _g, _d, _th), ((ALPHA, 0), (-1, 0), (GAMMA, 0), (DIFFERENT_SHAPE_NAME, 0), (THROTTLING_EVENT, 0)), ) @@ -79,7 +87,7 @@ 18, n0, _TESI, - {_smi: None}, + {_smit: None}, (_sI, _e), ((3, 0), (TRADE_EVENTS, 0)), ) @@ -88,7 +96,7 @@ 18, n0, _TESO, - {_smit: None}, + {_smith: None}, (_gr, _e), ((3, 0), (TRADE_EVENTS, 0)), ) diff --git a/codegen-output/my-handwritten-model/src/my_handwritten_model/_private/schemas.py b/codegen-output/my-handwritten-model/src/my_handwritten_model/_private/schemas.py index c7b2d38cd..a1b53a748 100644 --- a/codegen-output/my-handwritten-model/src/my_handwritten_model/_private/schemas.py +++ b/codegen-output/my-handwritten-model/src/my_handwritten_model/_private/schemas.py @@ -54,6 +54,7 @@ _se = "server" _TE = "ThrottlingEvent" _cl = "client" +_smith = "smithy.api#retryable" _TEr = "TradeEvents" _a = "alpha" _b = "beta" @@ -169,7 +170,14 @@ STREAM_SERVICE_ERROR: StaticSchema = (18, n0, _SSE, {_smit: _se}, (_me,), ((3, 0),)) -THROTTLING_EVENT: StaticSchema = (18, n0, _TE, {_smit: _cl}, (_me,), ((3, 0),)) +THROTTLING_EVENT: StaticSchema = ( + 18, + n0, + _TE, + {_smit: _cl, _smith: MappingProxyType({"throttling": True})}, + (_me,), + ((3, 0),), +) TRADE_EVENTS: StaticSchema = ( 19, diff --git a/codegen/core/src/it/java/software/amazon/smithy/python/codegen/test/PythonCodegenTest.java b/codegen/core/src/it/java/software/amazon/smithy/python/codegen/test/PythonCodegenTest.java index 29025d607..5a971d7cb 100644 --- a/codegen/core/src/it/java/software/amazon/smithy/python/codegen/test/PythonCodegenTest.java +++ b/codegen/core/src/it/java/software/amazon/smithy/python/codegen/test/PythonCodegenTest.java @@ -342,6 +342,8 @@ def upload_city_image( assertTrue(syncConfig.contains("from ._private.endpoints import BDD")); assertTrue(schemas.contains("smithy.api#longPoll")); assertTrue(schemas.contains("\"timeoutMillis\": 20000")); + assertTrue(schemas.contains("smithy.api#retryable")); + assertTrue(schemas.contains("\"throttling\": True")); } @Test diff --git a/codegen/core/src/it/resources/META-INF/smithy/main.smithy b/codegen/core/src/it/resources/META-INF/smithy/main.smithy index df2b5c6da..2b86fb4b0 100644 --- a/codegen/core/src/it/resources/META-INF/smithy/main.smithy +++ b/codegen/core/src/it/resources/META-INF/smithy/main.smithy @@ -255,6 +255,7 @@ operation GetCity { errors: [ NoSuchResource EmptyError + ThrottlingError ] } @@ -348,6 +349,13 @@ apply NoSuchResource @httpResponseTests([ @httpError(400) structure EmptyError {} +@error("client") +@retryable(throttling: true) +@httpError(429) +structure ThrottlingError { + message: String +} + // The paginated trait indicates that the operation may // return truncated results. @readonly diff --git a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/SchemaTraitFilterIndex.java b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/SchemaTraitFilterIndex.java index a484e1985..75d8594d7 100644 --- a/codegen/core/src/main/java/software/amazon/smithy/python/codegen/SchemaTraitFilterIndex.java +++ b/codegen/core/src/main/java/software/amazon/smithy/python/codegen/SchemaTraitFilterIndex.java @@ -49,6 +49,7 @@ import software.amazon.smithy.model.traits.RequestCompressionTrait; import software.amazon.smithy.model.traits.RequiredTrait; import software.amazon.smithy.model.traits.RequiresLengthTrait; +import software.amazon.smithy.model.traits.RetryableTrait; import software.amazon.smithy.model.traits.SensitiveTrait; import software.amazon.smithy.model.traits.SparseTrait; import software.amazon.smithy.model.traits.StreamingTrait; @@ -117,6 +118,7 @@ public final class SchemaTraitFilterIndex implements KnowledgeIndex { StreamingTrait.ID, EndpointTrait.ID, ErrorTrait.ID, + RetryableTrait.ID, // runtime sets the retry defaults of the error class RequiresLengthTrait.ID, EventHeaderTrait.ID, EventPayloadTrait.ID, diff --git a/packages/smithy-core/.changes/next-release/smithy-core-bugfix-461117b0f86944e4bc52309c9192c86c.json b/packages/smithy-core/.changes/next-release/smithy-core-bugfix-461117b0f86944e4bc52309c9192c86c.json new file mode 100644 index 000000000..1ef6612f1 --- /dev/null +++ b/packages/smithy-core/.changes/next-release/smithy-core-bugfix-461117b0f86944e4bc52309c9192c86c.json @@ -0,0 +1,4 @@ +{ + "type": "bugfix", + "description": "Fixed modeled errors with the `@retryable` trait not being retry-safe in generated clients." +} \ No newline at end of file diff --git a/packages/smithy-core/.changes/next-release/smithy-core-feature-efc9b16b43be4617bde6512f8b1fa220.json b/packages/smithy-core/.changes/next-release/smithy-core-feature-efc9b16b43be4617bde6512f8b1fa220.json new file mode 100644 index 000000000..42903be63 --- /dev/null +++ b/packages/smithy-core/.changes/next-release/smithy-core-feature-efc9b16b43be4617bde6512f8b1fa220.json @@ -0,0 +1,4 @@ +{ + "type": "feature", + "description": "Added `RetryableTrait` to `smithy_core.traits`." +} \ No newline at end of file diff --git a/packages/smithy-core/src/smithy_core/_dynamic_models.py b/packages/smithy-core/src/smithy_core/_dynamic_models.py index 259dd4c27..454c3190c 100644 --- a/packages/smithy-core/src/smithy_core/_dynamic_models.py +++ b/packages/smithy-core/src/smithy_core/_dynamic_models.py @@ -47,6 +47,7 @@ ErrorTrait, IdempotencyTokenTrait, RequiredTrait, + RetryableTrait, SensitiveTrait, SparseTrait, StreamingTrait, @@ -451,6 +452,11 @@ def _build_struct(self, name: str, schema: Schema) -> type: fields.append( ("fault", str | None, _field(default=error_trait.fault.value)) ) + retryable_trait = schema.get_trait(RetryableTrait) + if retryable_trait is not None: + fields.append(("is_retry_safe", bool | None, _field(default=True))) + if retryable_trait.throttling: + fields.append(("is_throttling_error", bool, _field(default=True))) bases: tuple[type, ...] = (self._error_base(),) if is_error else () # Operation outputs carry a non-modeled response_metadata attribute, excluded # from eq and repr. Errors inherit it from ModeledError instead. diff --git a/packages/smithy-core/src/smithy_core/traits.py b/packages/smithy-core/src/smithy_core/traits.py index 4abd30d33..698db0893 100644 --- a/packages/smithy-core/src/smithy_core/traits.py +++ b/packages/smithy-core/src/smithy_core/traits.py @@ -52,6 +52,7 @@ "OperationContextParamsTrait", "RequiredTrait", "RequiresLengthTrait", + "RetryableTrait", "RpcV2CborTrait", "RpcV2JsonTrait", "SensitiveTrait", @@ -194,6 +195,21 @@ def __init__(self, value: "DocumentValue | DynamicTrait" = None): object.__setattr__(self, "fault", ErrorFault(self.document_value)) +@dataclass(init=False, frozen=True) +class RetryableTrait(Trait, id=ShapeID("smithy.api#retryable")): + """Indicates that an error may be retried by the client.""" + + def __post_init__(self) -> None: + assert self.document_value is None or isinstance(self.document_value, Mapping) + + @property + def throttling(self) -> bool: + """Whether the error is a throttling error.""" + if self.document_value is None: + return False + return self.document_value.get("throttling", False) # type: ignore + + @dataclass(init=False, frozen=True) class RequiredTrait(Trait, id=ShapeID("smithy.api#required")): """Marks a member as required.""" diff --git a/packages/smithy-core/tests/unit/test_dynamic_models.py b/packages/smithy-core/tests/unit/test_dynamic_models.py index e535b0449..16980cf98 100644 --- a/packages/smithy-core/tests/unit/test_dynamic_models.py +++ b/packages/smithy-core/tests/unit/test_dynamic_models.py @@ -61,6 +61,7 @@ def _struct(name: str, members: tuple[tuple[str, object], ...]) -> StaticSchema: _ERROR_TRAIT = "smithy.api#error" +_RETRYABLE_TRAIT = "smithy.api#retryable" def _error_struct( @@ -68,12 +69,13 @@ def _error_struct( members: tuple[tuple[str, object], ...], *, fault: str = "client", + traits: dict[str, object] | None = None, ) -> StaticSchema: return ( ShapeType.STRUCTURE.value, NS, name, - {_ERROR_TRAIT: fault}, + {_ERROR_TRAIT: fault, **(traits or {})}, tuple(m[0] for m in members), tuple((m[1], 0) for m in members), ) @@ -578,6 +580,32 @@ def read_struct(self, schema: Schema, consumer: Any) -> None: assert cls.deserialize(cast(ShapeDeserializer, _De())).fault == "server" +@pytest.mark.parametrize( + "traits, is_retry_safe, is_throttling_error", + [ + ({}, None, False), + ({_RETRYABLE_TRAIT: None}, True, False), + ({_RETRYABLE_TRAIT: {"throttling": True}}, True, True), + ({_RETRYABLE_TRAIT: {"throttling": False}}, True, False), + ], +) +def test_modeled_error_uses_modeled_retry_info( + traits: dict[str, object], is_retry_safe: bool | None, is_throttling_error: bool +) -> None: + _schema_compact._cache.clear() + mod = _module(BOOM=_error_struct("Boom", (), traits=traits)) + registry = ModelRegistry(mod, index={"Boom": "BOOM"}, shape_names={"Boom"}) + cls = cast(_ModeledErrorClass, registry.build("Boom")) + + class _De: + def read_struct(self, schema: Schema, consumer: Any) -> None: + pass + + for error in (cls(), cls.deserialize(cast(ShapeDeserializer, _De()))): + assert error.is_retry_safe is is_retry_safe + assert error.is_throttling_error is is_throttling_error + + def test_enum_member_deserialize_coerces_to_enum_class() -> None: # A member whose target is an enum must deserialize to the enum class, not the # raw wire string (the eager generator wrapped it as EnumClass(read_string(...))). diff --git a/packages/smithy-python/.changes/next-release/smithy-python-bugfix-13eef52d86d444dc8f8f76d762eebca8.json b/packages/smithy-python/.changes/next-release/smithy-python-bugfix-13eef52d86d444dc8f8f76d762eebca8.json new file mode 100644 index 000000000..9e3155201 --- /dev/null +++ b/packages/smithy-python/.changes/next-release/smithy-python-bugfix-13eef52d86d444dc8f8f76d762eebca8.json @@ -0,0 +1,4 @@ +{ + "type": "bugfix", + "description": "Fixed generated schemas not keeping the `@retryable` trait." +} \ No newline at end of file diff --git a/packages/smithy-python/src/smithy_python/schemas.py b/packages/smithy-python/src/smithy_python/schemas.py index bac9538a7..bfc3897dc 100644 --- a/packages/smithy-python/src/smithy_python/schemas.py +++ b/packages/smithy-python/src/smithy_python/schemas.py @@ -332,6 +332,7 @@ def _shape_id(value: str) -> _model.ShapeId: "smithy.api#streaming", "smithy.api#endpoint", "smithy.api#error", + "smithy.api#retryable", "smithy.api#requiresLength", "smithy.api#eventHeader", "smithy.api#eventPayload", diff --git a/scripts/api-snapshot/api.json b/scripts/api-snapshot/api.json index 193b943ae..e301229b2 100644 --- a/scripts/api-snapshot/api.json +++ b/scripts/api-snapshot/api.json @@ -629,6 +629,7 @@ "OperationContextParamsTrait": "class", "RequiredTrait": "class", "RequiresLengthTrait": "class", + "RetryableTrait": "class", "RpcV2CborTrait": "class", "RpcV2JsonTrait": "class", "SensitiveTrait": "class", diff --git a/scripts/codegen-output-py/tests/test_snapshots.py b/scripts/codegen-output-py/tests/test_snapshots.py index f3a97dc55..8f69b0a54 100644 --- a/scripts/codegen-output-py/tests/test_snapshots.py +++ b/scripts/codegen-output-py/tests/test_snapshots.py @@ -64,6 +64,7 @@ "smithy.api#xmlNamespace", "smithy.api#streaming", "smithy.api#error", + "smithy.api#retryable", "smithy.api#httpError", "smithy.api#httpHeader", "smithy.api#httpQuery", @@ -421,6 +422,17 @@ def _model_surface(models: ModuleType) -> dict[str, Any]: return surface +@pytest.mark.parametrize("tree", ["codegen-output", "codegen-output-py"]) +def test_retryable_error_classes_carry_retry_info(tree: str) -> None: + with _import_package( + _ROOT / tree / "my-handwritten-model/src", "my_handwritten_model" + ) as (models, _): + throttling = models.ThrottlingEvent() + assert throttling.is_retry_safe is True + assert throttling.is_throttling_error is True + assert models.StreamServiceError().is_retry_safe is None + + def test_rpcv2_cbor_models_round_trip_without_a_client() -> None: with _import_package(_ROOT / "codegen-output-py/rpcv2-cbor/src", "rpcv2cbor") as ( models,