Skip to content

Commit 7977e85

Browse files
committed
feat: add factory support to resource sequences
1 parent e0d20ee commit 7977e85

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/posit/connect/resources.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import posixpath
44
import warnings
55
from abc import ABC
6-
from typing import ItemsView
6+
from typing import ItemsView, cast
77

88
from typing_extensions import (
99
TYPE_CHECKING,
@@ -92,8 +92,8 @@ def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride
9292
super().update(**result)
9393

9494

95-
_T = TypeVar("_T", bound=_Resource)
96-
_T_co = TypeVar("_T_co", bound=_Resource, covariant=True)
95+
_T = TypeVar("_T", bound=Resource)
96+
_T_co = TypeVar("_T_co", bound=Resource, covariant=True)
9797

9898

9999
class ResourceFactory(Protocol[_T_co]):
@@ -121,13 +121,13 @@ def __init__(
121121
self,
122122
ctx: Context,
123123
path: str,
124-
factory: ResourceFactory[_T] = _Resource,
124+
factory: ResourceFactory[_T_co] = _Resource,
125125
uid: str = "guid",
126126
):
127127
self._ctx = ctx
128128
self._path = path
129-
self._factory = factory
130129
self._uid = uid
130+
self._factory = factory
131131

132132
def __getitem__(self, index):
133133
return list(self.fetch())[index]
@@ -150,6 +150,7 @@ def create(self, **attributes: Any) -> _T:
150150
uid = result[self._uid]
151151
path = posixpath.join(self._path, uid)
152152
resource = self._factory(self._ctx, path, **result)
153+
resource = cast(_T, resource)
153154
return resource
154155

155156
def fetch(self, **conditions: Any) -> Iterable[_T]:
@@ -160,6 +161,7 @@ def fetch(self, **conditions: Any) -> Iterable[_T]:
160161
uid = result[self._uid]
161162
path = posixpath.join(self._path, uid)
162163
resource = self._factory(self._ctx, path, **result)
164+
resource = cast(_T, resource)
163165
resources.append(resource)
164166

165167
return resources
@@ -169,6 +171,7 @@ def find(self, *args: str) -> _T:
169171
response = self._ctx.client.get(path)
170172
result = response.json()
171173
resource = self._factory(self._ctx, path, **result)
174+
resource = cast(_T, resource)
172175
return resource
173176

174177
def find_by(self, **conditions: Any) -> _T | None:
@@ -200,6 +203,6 @@ def fetch(self, **conditions: Any) -> Iterable[_T]:
200203
uid = result[self._uid]
201204
path = posixpath.join(self._path, uid)
202205
resource = self._factory(self._ctx, path, **result)
203-
206+
resource = cast(_T, resource)
204207
resources.append(resource)
205208
yield from resources

0 commit comments

Comments
 (0)