@@ -92,35 +92,36 @@ def update(self, **attributes): # type: ignore[reportIncompatibleMethodOverride
92
92
super ().update (** result )
93
93
94
94
95
- T = TypeVar ("T" , bound = Resource )
95
+ _T = TypeVar ("_T" , bound = Resource )
96
+ _T_co = TypeVar ("_T_co" , bound = Resource , covariant = True )
96
97
97
98
98
- class ResourceFactory (Protocol ):
99
- def __call__ (self , ctx : Context , path : str , ** attributes ) -> Resource : ...
99
+ class ResourceFactory (Protocol [ _T_co ] ):
100
+ def __call__ (self , ctx : Context , path : str , ** attributes : Any ) -> _T_co : ...
100
101
101
102
102
- class ResourceSequence (Protocol [T ]):
103
+ class ResourceSequence (Protocol [_T ]):
103
104
@overload
104
- def __getitem__ (self , index : SupportsIndex , / ) -> T : ...
105
+ def __getitem__ (self , index : SupportsIndex , / ) -> _T : ...
105
106
106
107
@overload
107
- def __getitem__ (self , index : slice , / ) -> List [T ]: ...
108
+ def __getitem__ (self , index : slice , / ) -> List [_T ]: ...
108
109
109
110
def __len__ (self ) -> int : ...
110
111
111
- def __iter__ (self ) -> Iterator [T ]: ...
112
+ def __iter__ (self ) -> Iterator [_T ]: ...
112
113
113
114
def __str__ (self ) -> str : ...
114
115
115
116
def __repr__ (self ) -> str : ...
116
117
117
118
118
- class _ResourceSequence (Sequence [T ], ResourceSequence [T ]):
119
+ class _ResourceSequence (Sequence [_T ], ResourceSequence [_T ]):
119
120
def __init__ (
120
121
self ,
121
122
ctx : Context ,
122
123
path : str ,
123
- factory : ResourceFactory = _Resource ,
124
+ factory : ResourceFactory [ _T_co ] = _Resource ,
124
125
uid : str = "guid" ,
125
126
):
126
127
self ._ctx = ctx
@@ -134,7 +135,7 @@ def __getitem__(self, index):
134
135
def __len__ (self ) -> int :
135
136
return len (list (self .fetch ()))
136
137
137
- def __iter__ (self ) -> Iterator [T ]:
138
+ def __iter__ (self ) -> Iterator [_T ]:
138
139
return iter (self .fetch ())
139
140
140
141
def __str__ (self ) -> str :
@@ -143,32 +144,32 @@ def __str__(self) -> str:
143
144
def __repr__ (self ) -> str :
144
145
return repr (self .fetch ())
145
146
146
- def create (self , ** attributes : Any ) -> T :
147
+ def create (self , ** attributes : Any ) -> _T :
147
148
response = self ._ctx .client .post (self ._path , json = attributes )
148
149
result = response .json ()
149
150
uid = result [self ._uid ]
150
151
path = posixpath .join (self ._path , uid )
151
- return cast (T , self ._factory (self ._ctx , path , ** result ))
152
+ return cast (_T , self ._factory (self ._ctx , path , ** result ))
152
153
153
- def fetch (self , ** conditions ) -> Iterable [T ]:
154
+ def fetch (self , ** conditions ) -> Iterable [_T ]:
154
155
response = self ._ctx .client .get (self ._path , params = conditions )
155
156
results = response .json ()
156
- resources : List [T ] = []
157
+ resources : List [_T ] = []
157
158
for result in results :
158
159
uid = result [self ._uid ]
159
160
path = posixpath .join (self ._path , uid )
160
- resource = cast (T , self ._factory (self ._ctx , path , ** result ))
161
+ resource = cast (_T , self ._factory (self ._ctx , path , ** result ))
161
162
resources .append (resource )
162
163
163
164
return resources
164
165
165
- def find (self , * args : str ) -> T :
166
+ def find (self , * args : str ) -> _T :
166
167
path = posixpath .join (self ._path , * args )
167
168
response = self ._ctx .client .get (path )
168
169
result = response .json ()
169
- return cast (T , self ._factory (self ._ctx , path , ** result ))
170
+ return cast (_T , self ._factory (self ._ctx , path , ** result ))
170
171
171
- def find_by (self , ** conditions ) -> T | None :
172
+ def find_by (self , ** conditions ) -> _T | None :
172
173
"""
173
174
Find the first record matching the specified conditions.
174
175
@@ -183,19 +184,19 @@ def find_by(self, **conditions) -> T | None:
183
184
Optional[T]
184
185
The first record matching the conditions, or `None` if no match is found.
185
186
"""
186
- collection : Iterable [T ] = self .fetch (** conditions )
187
+ collection : Iterable [_T ] = self .fetch (** conditions )
187
188
return next ((v for v in collection if v .items () >= conditions .items ()), None )
188
189
189
190
190
- class _PaginatedResourceSequence (_ResourceSequence [T ]):
191
- def fetch (self , ** conditions ) -> Iterator [T ]:
191
+ class _PaginatedResourceSequence (_ResourceSequence [_T ]):
192
+ def fetch (self , ** conditions ) -> Iterator [_T ]:
192
193
paginator = Paginator (self ._ctx , self ._path , dict (** conditions ))
193
194
for page in paginator .fetch_pages ():
194
195
resources = []
195
196
results = page .results
196
197
for result in results :
197
198
uid = result [self ._uid ]
198
199
path = posixpath .join (self ._path , uid )
199
- resource = cast (T , self ._factory (self ._ctx , path , ** result ))
200
+ resource = cast (_T , self ._factory (self ._ctx , path , ** result ))
200
201
resources .append (resource )
201
202
yield from resources
0 commit comments