Skip to content

Commit b60f5a8

Browse files
chore: use property declarations for resource members (#124)
This will speedup client instantiation in certain cases.
1 parent 823f95c commit b60f5a8

23 files changed

+242
-325
lines changed

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ dependencies = [
1414
"anyio>=3.5.0, <5",
1515
"distro>=1.7.0, <2",
1616
"sniffio",
17+
"cached-property; python_version < '3.8'",
1718

1819
]
1920
requires-python = ">= 3.7"

src/orb/_compat.py

+10
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,13 @@ class GenericModel(pydantic.BaseModel):
173173

174174
class GenericModel(pydantic.generics.GenericModel, pydantic.BaseModel):
175175
...
176+
177+
178+
# cached properties
179+
if TYPE_CHECKING:
180+
cached_property = property
181+
else:
182+
try:
183+
from functools import cached_property as cached_property
184+
except ImportError:
185+
from cached_property import cached_property as cached_property

src/orb/resources/coupons/coupons.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Optional
5+
from typing import Optional
66

77
import httpx
88

@@ -15,6 +15,7 @@
1515
NotGiven,
1616
)
1717
from ..._utils import maybe_transform
18+
from ..._compat import cached_property
1819
from ..._resource import SyncAPIResource, AsyncAPIResource
1920
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2021
from ...pagination import SyncPage, AsyncPage
@@ -29,20 +30,17 @@
2930
make_request_options,
3031
)
3132

32-
if TYPE_CHECKING:
33-
from ..._client import Orb, AsyncOrb
34-
3533
__all__ = ["Coupons", "AsyncCoupons"]
3634

3735

3836
class Coupons(SyncAPIResource):
39-
subscriptions: Subscriptions
40-
with_raw_response: CouponsWithRawResponse
37+
@cached_property
38+
def subscriptions(self) -> Subscriptions:
39+
return Subscriptions(self._client)
4140

42-
def __init__(self, client: Orb) -> None:
43-
super().__init__(client)
44-
self.subscriptions = Subscriptions(client)
45-
self.with_raw_response = CouponsWithRawResponse(self)
41+
@cached_property
42+
def with_raw_response(self) -> CouponsWithRawResponse:
43+
return CouponsWithRawResponse(self)
4644

4745
def create(
4846
self,
@@ -242,13 +240,13 @@ def fetch(
242240

243241

244242
class AsyncCoupons(AsyncAPIResource):
245-
subscriptions: AsyncSubscriptions
246-
with_raw_response: AsyncCouponsWithRawResponse
243+
@cached_property
244+
def subscriptions(self) -> AsyncSubscriptions:
245+
return AsyncSubscriptions(self._client)
247246

248-
def __init__(self, client: AsyncOrb) -> None:
249-
super().__init__(client)
250-
self.subscriptions = AsyncSubscriptions(client)
251-
self.with_raw_response = AsyncCouponsWithRawResponse(self)
247+
@cached_property
248+
def with_raw_response(self) -> AsyncCouponsWithRawResponse:
249+
return AsyncCouponsWithRawResponse(self)
252250

253251
async def create(
254252
self,

src/orb/resources/coupons/subscriptions.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Optional
5+
from typing import Optional
66

77
import httpx
88

@@ -15,6 +15,7 @@
1515
NotGiven,
1616
)
1717
from ..._utils import maybe_transform
18+
from ..._compat import cached_property
1819
from ..._resource import SyncAPIResource, AsyncAPIResource
1920
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2021
from ...pagination import SyncPage, AsyncPage
@@ -24,18 +25,13 @@
2425
)
2526
from ...types.coupons import subscription_list_params
2627

27-
if TYPE_CHECKING:
28-
from ..._client import Orb, AsyncOrb
29-
3028
__all__ = ["Subscriptions", "AsyncSubscriptions"]
3129

3230

3331
class Subscriptions(SyncAPIResource):
34-
with_raw_response: SubscriptionsWithRawResponse
35-
36-
def __init__(self, client: Orb) -> None:
37-
super().__init__(client)
38-
self.with_raw_response = SubscriptionsWithRawResponse(self)
32+
@cached_property
33+
def with_raw_response(self) -> SubscriptionsWithRawResponse:
34+
return SubscriptionsWithRawResponse(self)
3935

4036
def list(
4137
self,
@@ -91,11 +87,9 @@ def list(
9187

9288

9389
class AsyncSubscriptions(AsyncAPIResource):
94-
with_raw_response: AsyncSubscriptionsWithRawResponse
95-
96-
def __init__(self, client: AsyncOrb) -> None:
97-
super().__init__(client)
98-
self.with_raw_response = AsyncSubscriptionsWithRawResponse(self)
90+
@cached_property
91+
def with_raw_response(self) -> AsyncSubscriptionsWithRawResponse:
92+
return AsyncSubscriptionsWithRawResponse(self)
9993

10094
def list(
10195
self,

src/orb/resources/credit_notes.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Optional
5+
from typing import Optional
66

77
import httpx
88

@@ -15,6 +15,7 @@
1515
NotGiven,
1616
)
1717
from .._utils import maybe_transform
18+
from .._compat import cached_property
1819
from .._resource import SyncAPIResource, AsyncAPIResource
1920
from .._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2021
from ..pagination import SyncPage, AsyncPage
@@ -23,18 +24,13 @@
2324
make_request_options,
2425
)
2526

26-
if TYPE_CHECKING:
27-
from .._client import Orb, AsyncOrb
28-
2927
__all__ = ["CreditNotes", "AsyncCreditNotes"]
3028

3129

3230
class CreditNotes(SyncAPIResource):
33-
with_raw_response: CreditNotesWithRawResponse
34-
35-
def __init__(self, client: Orb) -> None:
36-
super().__init__(client)
37-
self.with_raw_response = CreditNotesWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> CreditNotesWithRawResponse:
33+
return CreditNotesWithRawResponse(self)
3834

3935
def list(
4036
self,
@@ -121,11 +117,9 @@ def fetch(
121117

122118

123119
class AsyncCreditNotes(AsyncAPIResource):
124-
with_raw_response: AsyncCreditNotesWithRawResponse
125-
126-
def __init__(self, client: AsyncOrb) -> None:
127-
super().__init__(client)
128-
self.with_raw_response = AsyncCreditNotesWithRawResponse(self)
120+
@cached_property
121+
def with_raw_response(self) -> AsyncCreditNotesWithRawResponse:
122+
return AsyncCreditNotesWithRawResponse(self)
129123

130124
def list(
131125
self,

src/orb/resources/customers/balance_transactions.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union, Optional
5+
from typing import Union, Optional
66
from datetime import datetime
77
from typing_extensions import Literal
88

@@ -16,6 +16,7 @@
1616
NotGiven,
1717
)
1818
from ..._utils import maybe_transform
19+
from ..._compat import cached_property
1920
from ..._resource import SyncAPIResource, AsyncAPIResource
2021
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2122
from ...pagination import SyncPage, AsyncPage
@@ -30,18 +31,13 @@
3031
balance_transaction_create_params,
3132
)
3233

33-
if TYPE_CHECKING:
34-
from ..._client import Orb, AsyncOrb
35-
3634
__all__ = ["BalanceTransactions", "AsyncBalanceTransactions"]
3735

3836

3937
class BalanceTransactions(SyncAPIResource):
40-
with_raw_response: BalanceTransactionsWithRawResponse
41-
42-
def __init__(self, client: Orb) -> None:
43-
super().__init__(client)
44-
self.with_raw_response = BalanceTransactionsWithRawResponse(self)
38+
@cached_property
39+
def with_raw_response(self) -> BalanceTransactionsWithRawResponse:
40+
return BalanceTransactionsWithRawResponse(self)
4541

4642
def create(
4743
self,
@@ -181,11 +177,9 @@ def list(
181177

182178

183179
class AsyncBalanceTransactions(AsyncAPIResource):
184-
with_raw_response: AsyncBalanceTransactionsWithRawResponse
185-
186-
def __init__(self, client: AsyncOrb) -> None:
187-
super().__init__(client)
188-
self.with_raw_response = AsyncBalanceTransactionsWithRawResponse(self)
180+
@cached_property
181+
def with_raw_response(self) -> AsyncBalanceTransactionsWithRawResponse:
182+
return AsyncBalanceTransactionsWithRawResponse(self)
189183

190184
async def create(
191185
self,

src/orb/resources/customers/costs.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Union, Optional
5+
from typing import Union, Optional
66
from datetime import datetime
77
from typing_extensions import Literal
88

@@ -16,6 +16,7 @@
1616
NotGiven,
1717
)
1818
from ..._utils import maybe_transform
19+
from ..._compat import cached_property
1920
from ..._resource import SyncAPIResource, AsyncAPIResource
2021
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2122
from ..._base_client import (
@@ -28,18 +29,13 @@
2829
cost_list_by_external_id_params,
2930
)
3031

31-
if TYPE_CHECKING:
32-
from ..._client import Orb, AsyncOrb
33-
3432
__all__ = ["Costs", "AsyncCosts"]
3533

3634

3735
class Costs(SyncAPIResource):
38-
with_raw_response: CostsWithRawResponse
39-
40-
def __init__(self, client: Orb) -> None:
41-
super().__init__(client)
42-
self.with_raw_response = CostsWithRawResponse(self)
36+
@cached_property
37+
def with_raw_response(self) -> CostsWithRawResponse:
38+
return CostsWithRawResponse(self)
4339

4440
def list(
4541
self,
@@ -425,11 +421,9 @@ def list_by_external_id(
425421

426422

427423
class AsyncCosts(AsyncAPIResource):
428-
with_raw_response: AsyncCostsWithRawResponse
429-
430-
def __init__(self, client: AsyncOrb) -> None:
431-
super().__init__(client)
432-
self.with_raw_response = AsyncCostsWithRawResponse(self)
424+
@cached_property
425+
def with_raw_response(self) -> AsyncCostsWithRawResponse:
426+
return AsyncCostsWithRawResponse(self)
433427

434428
async def list(
435429
self,

src/orb/resources/customers/credits/credits.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, Optional
5+
from typing import Optional
66

77
import httpx
88

@@ -15,6 +15,7 @@
1515
NotGiven,
1616
)
1717
from ...._utils import maybe_transform
18+
from ...._compat import cached_property
1819
from ...._resource import SyncAPIResource, AsyncAPIResource
1920
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2021
from ....pagination import SyncPage, AsyncPage
@@ -29,20 +30,17 @@
2930
credit_list_by_external_id_params,
3031
)
3132

32-
if TYPE_CHECKING:
33-
from ...._client import Orb, AsyncOrb
34-
3533
__all__ = ["Credits", "AsyncCredits"]
3634

3735

3836
class Credits(SyncAPIResource):
39-
ledger: Ledger
40-
with_raw_response: CreditsWithRawResponse
37+
@cached_property
38+
def ledger(self) -> Ledger:
39+
return Ledger(self._client)
4140

42-
def __init__(self, client: Orb) -> None:
43-
super().__init__(client)
44-
self.ledger = Ledger(client)
45-
self.with_raw_response = CreditsWithRawResponse(self)
41+
@cached_property
42+
def with_raw_response(self) -> CreditsWithRawResponse:
43+
return CreditsWithRawResponse(self)
4644

4745
def list(
4846
self,
@@ -152,13 +150,13 @@ def list_by_external_id(
152150

153151

154152
class AsyncCredits(AsyncAPIResource):
155-
ledger: AsyncLedger
156-
with_raw_response: AsyncCreditsWithRawResponse
153+
@cached_property
154+
def ledger(self) -> AsyncLedger:
155+
return AsyncLedger(self._client)
157156

158-
def __init__(self, client: AsyncOrb) -> None:
159-
super().__init__(client)
160-
self.ledger = AsyncLedger(client)
161-
self.with_raw_response = AsyncCreditsWithRawResponse(self)
157+
@cached_property
158+
def with_raw_response(self) -> AsyncCreditsWithRawResponse:
159+
return AsyncCreditsWithRawResponse(self)
162160

163161
def list(
164162
self,

0 commit comments

Comments
 (0)