Skip to content

refactor: hoist per-call status/field set literals to module-level frozenset constants - #733

Open
PratikDhanave wants to merge 1 commit into
aws:mainfrom
PratikDhanaveFork:refactor/hoist-status-field-set-literals
Open

PratikDhanave wants to merge 1 commit into
aws:mainfrom
PratikDhanaveFork:refactor/hoist-status-field-set-literals

Conversation

@PratikDhanave

Copy link
Copy Markdown

Description

Small, behavior-preserving readability refactor. Not a bug fix — the current code is functionally correct. A few helpers construct the same constant set literal on every call; this hoists each to a module-level frozenset constant and references it instead.

Changes

  • src/bedrock_agentcore/policy/client.py — start_policy_generation built {"GENERATE_FAILED", "DELETE_FAILED"} locally for each call. Hoisted to a module-level _GENERATION_FAILED_STATUSES constant (sits next to the existing _FAILED_STATUSES).
  • src/bedrock_agentcore/payments/manager.py — two validation helpers rebuilt required-field sets per call. Hoisted to module-level _PAYMENT_REQUIRED_FIELDS ({"statusCode", "headers", "body"}) and _X402_PAYLOAD_REQUIRED_FIELDS ({"x402Version", "accepts"}).

Rationale

  • Avoids reallocating a fixed set on each invocation.
  • Names the constants and co-locates them with related module-level state, making the "these are the fixed, known values" intent explicit at a glance.

Behavior

Members and membership semantics are unchanged. The constants are frozensets (immutable); set-difference and sorted(...) over them produce identical results to the previous set literals, so all call sites behave exactly as before.

Verification

  • ruff check and ruff format --check on both files: clean.
  • pytest for the covering suites (tests/unit/policy/test_policy_engine_client.py, tests/bedrock_agentcore/payments/test_payment_manager.py, tests/bedrock_agentcore/payments/test_mpp_manager.py): 189 passed.

…ozenset 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.

This branch is waiting to be deployed

1 waiting deployment
manual-approval — d2c76091 Waiting Nov 4, 2026 by PratikDhanave via Test (evaluation) #1691
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/xs PR size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant