feat: add card/delegation guard-rails to Python SDK#147
Conversation
Add `card_id` and `delegation_id` fields to CardDelegationConfig for reusing existing delegations and targeting specific cards. Add `DelegationSummary` model and `list_delegations()` method to DelegationAPI. Refs: nevermined-io/internal#882 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR extends the Python X402 card-delegation support to allow reusing existing cards/delegations when generating access tokens, and adds an API method to list existing delegations from the backend.
Changes:
- Expanded
CardDelegationConfigto supportcard_id/delegation_idreuse paths and made the “new delegation” fields optional. - Added
DelegationSummaryplusDelegationAPI.list_delegations()for retrieving existing delegations. - Re-exported
DelegationSummaryfrom thepayments_py.x402module.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
payments_py/x402/types.py |
Extends CardDelegationConfig for delegation/card reuse and relaxes required fields. |
payments_py/x402/delegation_api.py |
Adds DelegationSummary model and list_delegations() API call. |
payments_py/x402/__init__.py |
Exposes DelegationSummary via module exports. |
Comments suppressed due to low confidence (1)
payments_py/x402/delegation_api.py:9
AnyandDictare imported but not used in this module. Removing unused imports helps avoid lint failures and keeps the public surface clean.
from typing import Any, Dict, List, Optional
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| def list_delegations(self) -> List[DelegationSummary]: | ||
| """ | ||
| List the user's existing card delegations. | ||
|
|
There was a problem hiding this comment.
list_delegations() and DelegationSummary are new behavior, but there are existing unit tests for list_payment_methods() in this repo and no corresponding coverage for delegations. Please add unit tests that mock the GET call and assert successful parsing and HTTP error handling for list_delegations().
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| To reuse an existing delegation supply ``delegation_id``. | ||
| To reuse an existing card (PaymentMethod entity) supply ``card_id``. | ||
| When creating a brand-new delegation provide ``provider_payment_method_id``, | ||
| ``spending_limit_cents``, and ``duration_secs``. | ||
|
|
There was a problem hiding this comment.
CardDelegationConfig now documents that certain fields are required for “brand-new” delegations, but the model allows constructing/sending an empty delegationConfig (all fields optional). This makes it easy for callers to create invalid configs that only fail at the backend. Consider adding a Pydantic model_validator to enforce valid combinations (e.g., either delegation_id is set, or card_id is set, or provider_payment_method_id+spending_limit_cents+duration_secs are set), and clarify in the docstring which additional fields are required when using card_id.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
The test_non_blocking_execution_with_polling test was failing because the final settle_called.wait(timeout=10.0) blocked for 10 seconds without making any requests to the TestClient, starving the ASGI event loop of ticks needed to run the background settlement task. Fix: increase polling iterations from 5 to 50 and keep driving the event loop throughout the entire wait period instead of doing a single long blocking wait at the end.
The settle_permissions call in the streaming A2A handler was hanging forever when the staging backend was slow/unresponsive, causing e2e test timeouts. Add a default (10s connect, 30s read) timeout to both get_backend_http_options and get_public_http_options so all HTTP requests fail fast instead of blocking indefinitely.
Summary
card_idanddelegation_idfields toCardDelegationConfigfor reusing existing delegations and targeting specific cardsprovider_payment_method_id,spending_limit_cents,duration_secsoptionalDelegationSummarymodel andlist_delegations()method toDelegationAPIRelated
Test plan
poetry run pytest tests/unit/to verify unit tests passpoetry run black --check payments_py/to verify formattinglist_delegations()against stagingget_x402_access_tokenwithdelegation_idagainst staging🤖 Generated with Claude Code