From d2c7609150f712fb1b968aa165314cac00b984f4 Mon Sep 17 00:00:00 2001 From: Pratik Dhanave Date: Wed, 4 Nov 2026 22:23:33 +0530 Subject: [PATCH] refactor: hoist per-call status/field set literals to module-level frozenset constants Two helpers rebuilt a small, constant set literal on every call. Hoist each to a module-level frozenset constant and reference it instead: - policy/client.py: the GENERATE_FAILED/DELETE_FAILED terminal statuses in the policy-generation wait (now _GENERATION_FAILED_STATUSES). - payments/manager.py: the required-field sets for payment-required request validation and X.402 payload validation (now _PAYMENT_REQUIRED_FIELDS and _X402_PAYLOAD_REQUIRED_FIELDS). Pure readability/allocation refactor. Members and membership semantics are unchanged; behavior is identical. --- src/bedrock_agentcore/payments/manager.py | 11 ++++++----- src/bedrock_agentcore/policy/client.py | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/bedrock_agentcore/payments/manager.py b/src/bedrock_agentcore/payments/manager.py index dc813bc0..fa3fd0cc 100644 --- a/src/bedrock_agentcore/payments/manager.py +++ b/src/bedrock_agentcore/payments/manager.py @@ -27,6 +27,9 @@ logger = logging.getLogger(__name__) +_PAYMENT_REQUIRED_FIELDS = frozenset({"statusCode", "headers", "body"}) +_X402_PAYLOAD_REQUIRED_FIELDS = frozenset({"x402Version", "accepts"}) + class PaymentError(Exception): """Base exception for payment operations.""" @@ -1454,8 +1457,7 @@ def _validate_input_parameters( ) # Validate required fields in payment_required_request - required_fields = {"statusCode", "headers", "body"} - if not all(field in payment_required_request for field in required_fields): + if not all(field in payment_required_request for field in _PAYMENT_REQUIRED_FIELDS): raise PaymentError( "Input Validation: 402 payment required request is missing required fields - " "402 payment required request must contain statusCode, headers, and body" @@ -1553,12 +1555,11 @@ def _extract_x402_payload(self, payment_required_request: Dict[str, Any]) -> tup ) from ve # Validate required fields - required_fields = {"x402Version", "accepts"} - missing_fields = required_fields - set(x402_payload.keys()) + missing_fields = _X402_PAYLOAD_REQUIRED_FIELDS - set(x402_payload.keys()) if missing_fields: raise PaymentError( f"X.402 Validation: Missing required fields - " - f"x402Payload must contain {', '.join(sorted(required_fields))}, " + f"x402Payload must contain {', '.join(sorted(_X402_PAYLOAD_REQUIRED_FIELDS))}, " f"but missing: {', '.join(sorted(missing_fields))}" ) diff --git a/src/bedrock_agentcore/policy/client.py b/src/bedrock_agentcore/policy/client.py index 1b463ca7..c190fd71 100644 --- a/src/bedrock_agentcore/policy/client.py +++ b/src/bedrock_agentcore/policy/client.py @@ -15,6 +15,7 @@ logger = logging.getLogger(__name__) _FAILED_STATUSES = {"CREATE_FAILED", "UPDATE_FAILED", "DELETE_FAILED"} +_GENERATION_FAILED_STATUSES = frozenset({"GENERATE_FAILED", "DELETE_FAILED"}) class PolicyEngineClient: @@ -271,14 +272,13 @@ def generate_policy_asset_and_wait( generation_id = generation["policyGenerationId"] logger.info("Started policy generation %s, waiting for completion...", generation_id) - _generation_failed = {"GENERATE_FAILED", "DELETE_FAILED"} result = wait_until( lambda: self.cp_client.get_policy_generation( policyEngineId=policy_engine_id, policyGenerationId=generation_id, ), "GENERATED", - _generation_failed, + _GENERATION_FAILED_STATUSES, wait_config, )