Skip to content

Add Ref[T] type for typed v2 object references#1790

Closed
jar-stripe wants to merge 3 commits intomasterfrom
jar/ref-type
Closed

Add Ref[T] type for typed v2 object references#1790
jar-stripe wants to merge 3 commits intomasterfrom
jar/ref-type

Conversation

@jar-stripe
Copy link
Copy Markdown
Contributor

@jar-stripe jar-stripe commented Apr 4, 2026

Why?

Custom objects need typed references to other Stripe objects. A reference has the wire shape {type, id, url} and in the SDK becomes Ref[T] — a container that knows the target type and can fetch it.

What?

  • stripe.v2.Ref[T] class (Generic[T], not a StripeObject subclass)
  • Three attributes: type, id, url
  • Stores _requestor: Optional[_APIRequestor] (not StripeClient)
  • fetch() calls self._requestor.request("get", self.url, base_address="api") — same pattern as Event.fetch_related_object()
  • fetch_async() for async support
  • _construct_from classmethod matching StripeObject._construct_from signature so the deserialization pipeline can instantiate Ref via _inner_class_types
  • Requestor is threaded through _convert_to_stripe_object_construct_from automatically
  • Special handling in _util.py for issubclass(klass, Ref) to call Ref's _construct_from (which skips last_response)

See Also

Generated with Claude Code

jar-stripe and others added 2 commits April 4, 2026 15:21
Adds a Ref[T] class that represents a typed reference to a Stripe object
with wire shape {type, id, url}. Provides fetch() and fetch_async()
methods that GET the URL and return a strongly-typed T.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Committed-By-Agent: claude
@jar-stripe jar-stripe marked this pull request as ready for review April 5, 2026 03:22
@jar-stripe jar-stripe requested a review from a team as a code owner April 5, 2026 03:22
@jar-stripe jar-stripe requested review from prathmesh-stripe and removed request for a team April 5, 2026 03:22
Ref now stores _requestor and uses requestor.request() for fetch,
matching the Event.fetch_related_object pattern. Added _construct_from
classmethod so the deserialization pipeline can instantiate Ref with
the requestor. Updated _convert_to_stripe_object to handle Ref types.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Committed-By-Agent: claude
Copy link
Copy Markdown
Member

@xavdid xavdid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comments to the other langs

from stripe._api_requestor import _APIRequestor
from stripe._stripe_response import StripeResponse

T = TypeVar("T")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be bound to StripeObject because that's the root of everything that'll go in here.

def __init__(
self,
parsed_body: Dict[str, Any],
requestor: Optional["_APIRequestor"],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as ruby - in what situations is the Ref useful when the requestor isn't present None?

T = TypeVar("T")


class Ref(Generic[T]):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to ruby, we probably want this to be a StripeObject?

@jar-stripe
Copy link
Copy Markdown
Contributor Author

Changing direction for now.

@jar-stripe jar-stripe closed this Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants