Skip to content

Commit acd29fd

Browse files
chore: use property declarations for resource members (#247)
This will speedup client instantiation in certain cases.
1 parent a3c577c commit acd29fd

18 files changed

+204
-277
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/finch/_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/finch/resources/account.py

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

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
75
import httpx
86

97
from ..types import Introspection, DisconnectResponse
@@ -14,24 +12,20 @@
1412
Headers,
1513
NotGiven,
1614
)
15+
from .._compat import cached_property
1716
from .._resource import SyncAPIResource, AsyncAPIResource
1817
from .._response import to_raw_response_wrapper, async_to_raw_response_wrapper
1918
from .._base_client import (
2019
make_request_options,
2120
)
2221

23-
if TYPE_CHECKING:
24-
from .._client import Finch, AsyncFinch
25-
2622
__all__ = ["Account", "AsyncAccount"]
2723

2824

2925
class Account(SyncAPIResource):
30-
with_raw_response: AccountWithRawResponse
31-
32-
def __init__(self, client: Finch) -> None:
33-
super().__init__(client)
34-
self.with_raw_response = AccountWithRawResponse(self)
26+
@cached_property
27+
def with_raw_response(self) -> AccountWithRawResponse:
28+
return AccountWithRawResponse(self)
3529

3630
def disconnect(
3731
self,
@@ -77,11 +71,9 @@ def introspect(
7771

7872

7973
class AsyncAccount(AsyncAPIResource):
80-
with_raw_response: AsyncAccountWithRawResponse
81-
82-
def __init__(self, client: AsyncFinch) -> None:
83-
super().__init__(client)
84-
self.with_raw_response = AsyncAccountWithRawResponse(self)
74+
@cached_property
75+
def with_raw_response(self) -> AsyncAccountWithRawResponse:
76+
return AsyncAccountWithRawResponse(self)
8577

8678
async def disconnect(
8779
self,

src/finch/resources/hris/benefits/benefits.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

@@ -14,6 +14,7 @@
1414
NotGiven,
1515
)
1616
from ...._utils import maybe_transform
17+
from ...._compat import cached_property
1718
from .individuals import Individuals, AsyncIndividuals, IndividualsWithRawResponse, AsyncIndividualsWithRawResponse
1819
from ...._resource import SyncAPIResource, AsyncAPIResource
1920
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
@@ -33,20 +34,17 @@
3334
make_request_options,
3435
)
3536

36-
if TYPE_CHECKING:
37-
from ...._client import Finch, AsyncFinch
38-
3937
__all__ = ["Benefits", "AsyncBenefits"]
4038

4139

4240
class Benefits(SyncAPIResource):
43-
individuals: Individuals
44-
with_raw_response: BenefitsWithRawResponse
41+
@cached_property
42+
def individuals(self) -> Individuals:
43+
return Individuals(self._client)
4544

46-
def __init__(self, client: Finch) -> None:
47-
super().__init__(client)
48-
self.individuals = Individuals(client)
49-
self.with_raw_response = BenefitsWithRawResponse(self)
45+
@cached_property
46+
def with_raw_response(self) -> BenefitsWithRawResponse:
47+
return BenefitsWithRawResponse(self)
5048

5149
def create(
5250
self,
@@ -215,13 +213,13 @@ def list_supported_benefits(
215213

216214

217215
class AsyncBenefits(AsyncAPIResource):
218-
individuals: AsyncIndividuals
219-
with_raw_response: AsyncBenefitsWithRawResponse
216+
@cached_property
217+
def individuals(self) -> AsyncIndividuals:
218+
return AsyncIndividuals(self._client)
220219

221-
def __init__(self, client: AsyncFinch) -> None:
222-
super().__init__(client)
223-
self.individuals = AsyncIndividuals(client)
224-
self.with_raw_response = AsyncBenefitsWithRawResponse(self)
220+
@cached_property
221+
def with_raw_response(self) -> AsyncBenefitsWithRawResponse:
222+
return AsyncBenefitsWithRawResponse(self)
225223

226224
async def create(
227225
self,

src/finch/resources/hris/benefits/individuals.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, List
5+
from typing import List
66

77
import httpx
88

@@ -14,6 +14,7 @@
1414
NotGiven,
1515
)
1616
from ...._utils import maybe_transform
17+
from ...._compat import cached_property
1718
from ...._resource import SyncAPIResource, AsyncAPIResource
1819
from ...._response import to_raw_response_wrapper, async_to_raw_response_wrapper
1920
from ....pagination import SyncSinglePage, AsyncSinglePage
@@ -31,18 +32,13 @@
3132
individual_retrieve_many_benefits_params,
3233
)
3334

34-
if TYPE_CHECKING:
35-
from ...._client import Finch, AsyncFinch
36-
3735
__all__ = ["Individuals", "AsyncIndividuals"]
3836

3937

4038
class Individuals(SyncAPIResource):
41-
with_raw_response: IndividualsWithRawResponse
42-
43-
def __init__(self, client: Finch) -> None:
44-
super().__init__(client)
45-
self.with_raw_response = IndividualsWithRawResponse(self)
39+
@cached_property
40+
def with_raw_response(self) -> IndividualsWithRawResponse:
41+
return IndividualsWithRawResponse(self)
4642

4743
def enroll_many(
4844
self,
@@ -211,11 +207,9 @@ def unenroll_many(
211207

212208

213209
class AsyncIndividuals(AsyncAPIResource):
214-
with_raw_response: AsyncIndividualsWithRawResponse
215-
216-
def __init__(self, client: AsyncFinch) -> None:
217-
super().__init__(client)
218-
self.with_raw_response = AsyncIndividualsWithRawResponse(self)
210+
@cached_property
211+
def with_raw_response(self) -> AsyncIndividualsWithRawResponse:
212+
return AsyncIndividualsWithRawResponse(self)
219213

220214
def enroll_many(
221215
self,

src/finch/resources/hris/company.py

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

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING
6-
75
import httpx
86

97
from ..._types import (
@@ -13,25 +11,21 @@
1311
Headers,
1412
NotGiven,
1513
)
14+
from ..._compat import cached_property
1615
from ..._resource import SyncAPIResource, AsyncAPIResource
1716
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
1817
from ...types.hris import Company
1918
from ..._base_client import (
2019
make_request_options,
2120
)
2221

23-
if TYPE_CHECKING:
24-
from ..._client import Finch, AsyncFinch
25-
2622
__all__ = ["CompanyResource", "AsyncCompanyResource"]
2723

2824

2925
class CompanyResource(SyncAPIResource):
30-
with_raw_response: CompanyResourceWithRawResponse
31-
32-
def __init__(self, client: Finch) -> None:
33-
super().__init__(client)
34-
self.with_raw_response = CompanyResourceWithRawResponse(self)
26+
@cached_property
27+
def with_raw_response(self) -> CompanyResourceWithRawResponse:
28+
return CompanyResourceWithRawResponse(self)
3529

3630
def retrieve(
3731
self,
@@ -54,11 +48,9 @@ def retrieve(
5448

5549

5650
class AsyncCompanyResource(AsyncAPIResource):
57-
with_raw_response: AsyncCompanyResourceWithRawResponse
58-
59-
def __init__(self, client: AsyncFinch) -> None:
60-
super().__init__(client)
61-
self.with_raw_response = AsyncCompanyResourceWithRawResponse(self)
51+
@cached_property
52+
def with_raw_response(self) -> AsyncCompanyResourceWithRawResponse:
53+
return AsyncCompanyResourceWithRawResponse(self)
6254

6355
async def retrieve(
6456
self,

src/finch/resources/hris/directory.py

+8-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import typing_extensions
6-
from typing import TYPE_CHECKING
76

87
import httpx
98

@@ -15,30 +14,23 @@
1514
NotGiven,
1615
)
1716
from ..._utils import maybe_transform
17+
from ..._compat import cached_property
1818
from ..._resource import SyncAPIResource, AsyncAPIResource
1919
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
2020
from ...pagination import SyncIndividualsPage, AsyncIndividualsPage
21-
from ...types.hris import (
22-
IndividualInDirectory,
23-
directory_list_params,
24-
)
21+
from ...types.hris import IndividualInDirectory, directory_list_params
2522
from ..._base_client import (
2623
AsyncPaginator,
2724
make_request_options,
2825
)
2926

30-
if TYPE_CHECKING:
31-
from ..._client import Finch, AsyncFinch
32-
3327
__all__ = ["Directory", "AsyncDirectory"]
3428

3529

3630
class Directory(SyncAPIResource):
37-
with_raw_response: DirectoryWithRawResponse
38-
39-
def __init__(self, client: Finch) -> None:
40-
super().__init__(client)
41-
self.with_raw_response = DirectoryWithRawResponse(self)
31+
@cached_property
32+
def with_raw_response(self) -> DirectoryWithRawResponse:
33+
return DirectoryWithRawResponse(self)
4234

4335
def list(
4436
self,
@@ -127,11 +119,9 @@ def list_individuals(
127119

128120

129121
class AsyncDirectory(AsyncAPIResource):
130-
with_raw_response: AsyncDirectoryWithRawResponse
131-
132-
def __init__(self, client: AsyncFinch) -> None:
133-
super().__init__(client)
134-
self.with_raw_response = AsyncDirectoryWithRawResponse(self)
122+
@cached_property
123+
def with_raw_response(self) -> AsyncDirectoryWithRawResponse:
124+
return AsyncDirectoryWithRawResponse(self)
135125

136126
def list(
137127
self,

src/finch/resources/hris/employments.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, List
5+
from typing import List
66

77
import httpx
88

@@ -14,6 +14,7 @@
1414
NotGiven,
1515
)
1616
from ..._utils import maybe_transform
17+
from ..._compat import cached_property
1718
from ..._resource import SyncAPIResource, AsyncAPIResource
1819
from ..._response import to_raw_response_wrapper, async_to_raw_response_wrapper
1920
from ...pagination import SyncResponsesPage, AsyncResponsesPage
@@ -23,18 +24,13 @@
2324
make_request_options,
2425
)
2526

26-
if TYPE_CHECKING:
27-
from ..._client import Finch, AsyncFinch
28-
2927
__all__ = ["Employments", "AsyncEmployments"]
3028

3129

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

3935
def retrieve_many(
4036
self,
@@ -78,11 +74,9 @@ def retrieve_many(
7874

7975

8076
class AsyncEmployments(AsyncAPIResource):
81-
with_raw_response: AsyncEmploymentsWithRawResponse
82-
83-
def __init__(self, client: AsyncFinch) -> None:
84-
super().__init__(client)
85-
self.with_raw_response = AsyncEmploymentsWithRawResponse(self)
77+
@cached_property
78+
def with_raw_response(self) -> AsyncEmploymentsWithRawResponse:
79+
return AsyncEmploymentsWithRawResponse(self)
8680

8781
def retrieve_many(
8882
self,

0 commit comments

Comments
 (0)