Skip to content

Commit 830cc29

Browse files
committed
sparse: improved dok_{array,matrix} method robustness against Any with mypy
1 parent d40f3a3 commit 830cc29

File tree

1 file changed

+45
-39
lines changed

1 file changed

+45
-39
lines changed

scipy-stubs/sparse/_dok.pyi

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,16 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
140140
copy: bool = False,
141141
maxprint: int | None = None,
142142
) -> None: ...
143+
@override
144+
def todok(self, /, copy: bool = False) -> Self: ...
143145

144146
#
145-
@override
146-
def __delitem__(self, key: onp.ToJustInt, /) -> None: ...
147+
@overload
148+
def __delitem__(self: _dok_base[Any, _2D], key: _ToKey2D, /) -> None: ...
149+
@overload
150+
def __delitem__(self: _dok_base[Any, _1D], key: _ToKey1D, /) -> None: ...
151+
@overload
152+
def __delitem__(self, key: _ToKey1D | _ToKey2D, /) -> None: ...
147153

148154
#
149155
@override
@@ -165,62 +171,60 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
165171

166172
#
167173
@overload
168-
def setdefault(self: _dok_base[Any, _1D], key: _ToKey1D, default: None = None, /) -> _SCT | None: ...
174+
def setdefault(self: _dok_base[Any, _2D], key: _ToKey2D, default: _T, /) -> _SCT | _T: ...
169175
@overload
170176
def setdefault(self: _dok_base[Any, _2D], key: _ToKey2D, default: None = None, /) -> _SCT | None: ...
171177
@overload
172-
def setdefault(self, key: _ToKey1D | _ToKey2D, default: None = None, /) -> _SCT | None: ...
173-
@overload
174178
def setdefault(self: _dok_base[Any, _1D], key: _ToKey1D, default: _T, /) -> _SCT | _T: ...
175179
@overload
176-
def setdefault(self: _dok_base[Any, _2D], key: _ToKey2D, default: _T, /) -> _SCT | _T: ...
180+
def setdefault(self: _dok_base[Any, _1D], key: _ToKey1D, default: None = None, /) -> _SCT | None: ...
177181
@overload
178182
def setdefault(self, key: _ToKey1D | _ToKey2D, default: _T, /) -> _SCT | _T: ...
183+
@overload
184+
def setdefault(self, key: _ToKey1D | _ToKey2D, default: None = None, /) -> _SCT | None: ...
179185

180186
#
181187
@overload
182-
def get(self: _dok_base[Any, _1D], key: _ToKey1D, /, default: float = 0.0) -> _SCT | float: ...
188+
def get(self: _dok_base[Any, _2D], key: _ToKey2D, /, default: _T) -> _SCT | _T: ...
183189
@overload
184190
def get(self: _dok_base[Any, _2D], key: _ToKey2D, /, default: float = 0.0) -> _SCT | float: ...
185191
@overload
186-
def get(self, key: _ToKey1D | _ToKey2D, /, default: float = 0.0) -> _SCT | float: ...
187-
@overload
188192
def get(self: _dok_base[Any, _1D], key: _ToKey1D, /, default: _T) -> _SCT | _T: ...
189193
@overload
190-
def get(self: _dok_base[Any, _2D], key: _ToKey2D, /, default: _T) -> _SCT | _T: ...
194+
def get(self: _dok_base[Any, _1D], key: _ToKey1D, /, default: float = 0.0) -> _SCT | float: ...
191195
@overload
192196
def get(self, key: _ToKey1D | _ToKey2D, /, default: _T) -> _SCT | _T: ...
197+
@overload
198+
def get(self, key: _ToKey1D | _ToKey2D, /, default: float = 0.0) -> _SCT | float: ...
193199

194200
#
195201
def conjtransp(self, /) -> Self: ...
196202

197203
#
198204
@overload
199205
@classmethod
200-
def fromkeys(cls: type[_dok_base[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> _dok_base[np.bool_, _1D]: ...
206+
def fromkeys(cls: type[_dok_base[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> _dok_base[_SCT, _2D]: ...
207+
@overload
208+
@classmethod
209+
def fromkeys(cls: type[_dok_base[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> _dok_base[_SCT, _1D]: ...
201210
@overload
202211
@classmethod
203212
def fromkeys(cls: type[_dok_base[np.bool_, _2D]], ks: _ToKeys2D, v: onp.ToBool, /) -> _dok_base[np.bool_, _2D]: ...
204213
@overload
205214
@classmethod
206-
def fromkeys(cls: type[_dok_base[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _1D]: ...
215+
def fromkeys(cls: type[_dok_base[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> _dok_base[np.bool_, _1D]: ...
207216
@overload
208217
@classmethod
209218
def fromkeys(cls: type[_dok_base[np.int_, _2D]], ks: _ToKeys2D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _2D]: ...
210219
@overload
211220
@classmethod
212-
def fromkeys(cls: type[_dok_base[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> _dok_base[np.float64, _1D]: ...
221+
def fromkeys(cls: type[_dok_base[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> _dok_base[np.int_, _1D]: ...
213222
@overload
214223
@classmethod
215224
def fromkeys(cls: type[_dok_base[np.float64, _2D]], ks: _ToKeys2D, v: opt.JustFloat, /) -> _dok_base[np.float64, _2D]: ...
216225
@overload
217226
@classmethod
218-
def fromkeys(
219-
cls: type[_dok_base[np.complex128, _1D]],
220-
ks: _ToKeys1D,
221-
v: opt.JustComplex,
222-
/,
223-
) -> _dok_base[np.complex128, _1D]: ...
227+
def fromkeys(cls: type[_dok_base[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> _dok_base[np.float64, _1D]: ...
224228
@overload
225229
@classmethod
226230
def fromkeys(
@@ -231,41 +235,41 @@ class _dok_base(_spbase[_SCT, _ShapeT_co], IndexMixin[_SCT, _ShapeT_co], dict[Sh
231235
) -> _dok_base[np.complex128, _2D]: ...
232236
@overload
233237
@classmethod
234-
def fromkeys(cls: type[_dok_base[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> _dok_base[_SCT, _1D]: ...
235-
@overload
236-
@classmethod
237-
def fromkeys(cls: type[_dok_base[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> _dok_base[_SCT, _2D]: ...
238+
def fromkeys(
239+
cls: type[_dok_base[np.complex128, _1D]],
240+
ks: _ToKeys1D,
241+
v: opt.JustComplex,
242+
/,
243+
) -> _dok_base[np.complex128, _1D]: ...
238244

239245
#
240246
class dok_array(_dok_base[_SCT, _ShapeT_co], sparray, Generic[_SCT, _ShapeT_co]):
241247
# NOTE: This horrible code duplication is required due to the lack of higher-kinded typing (HKT) support.
242248
# https://github.com/python/typing/issues/548
243249
@overload
244250
@classmethod
245-
def fromkeys(cls: type[dok_array[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> dok_array[np.bool_, _1D]: ...
251+
def fromkeys(cls: type[dok_array[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> dok_array[_SCT, _2D]: ...
252+
@overload
253+
@classmethod
254+
def fromkeys(cls: type[dok_array[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> dok_array[_SCT, _1D]: ...
246255
@overload
247256
@classmethod
248257
def fromkeys(cls: type[dok_array[np.bool_, _2D]], ks: _ToKeys2D, v: onp.ToBool, /) -> dok_array[np.bool_, _2D]: ...
249258
@overload
250259
@classmethod
251-
def fromkeys(cls: type[dok_array[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _1D]: ...
260+
def fromkeys(cls: type[dok_array[np.bool_, _1D]], ks: _ToKeys1D, v: onp.ToBool, /) -> dok_array[np.bool_, _1D]: ...
252261
@overload
253262
@classmethod
254263
def fromkeys(cls: type[dok_array[np.int_, _2D]], ks: _ToKeys2D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _2D]: ...
255264
@overload
256265
@classmethod
257-
def fromkeys(cls: type[dok_array[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> dok_array[np.float64, _1D]: ...
266+
def fromkeys(cls: type[dok_array[np.int_, _1D]], ks: _ToKeys1D, v: opt.JustInt = 1, /) -> dok_array[np.int_, _1D]: ...
258267
@overload
259268
@classmethod
260269
def fromkeys(cls: type[dok_array[np.float64, _2D]], ks: _ToKeys2D, v: opt.JustFloat, /) -> dok_array[np.float64, _2D]: ...
261270
@overload
262271
@classmethod
263-
def fromkeys(
264-
cls: type[dok_array[np.complex128, _1D]],
265-
ks: _ToKeys1D,
266-
v: opt.JustComplex,
267-
/,
268-
) -> dok_array[np.complex128, _1D]: ...
272+
def fromkeys(cls: type[dok_array[np.float64, _1D]], ks: _ToKeys1D, v: opt.JustFloat, /) -> dok_array[np.float64, _1D]: ...
269273
@overload
270274
@classmethod
271275
def fromkeys(
@@ -276,10 +280,12 @@ class dok_array(_dok_base[_SCT, _ShapeT_co], sparray, Generic[_SCT, _ShapeT_co])
276280
) -> dok_array[np.complex128, _2D]: ...
277281
@overload
278282
@classmethod
279-
def fromkeys(cls: type[dok_array[_SCT, _1D]], ks: _ToKeys1D, v: _SCT, /) -> dok_array[_SCT, _1D]: ...
280-
@overload
281-
@classmethod
282-
def fromkeys(cls: type[dok_array[_SCT, _2D]], ks: _ToKeys2D, v: _SCT, /) -> dok_array[_SCT, _2D]: ...
283+
def fromkeys(
284+
cls: type[dok_array[np.complex128, _1D]],
285+
ks: _ToKeys1D,
286+
v: opt.JustComplex,
287+
/,
288+
) -> dok_array[np.complex128, _1D]: ...
283289

284290
#
285291
class dok_matrix(_dok_base[_SCT, _2D], spmatrix[_SCT], Generic[_SCT]):
@@ -291,6 +297,9 @@ class dok_matrix(_dok_base[_SCT, _2D], spmatrix[_SCT], Generic[_SCT]):
291297
#
292298
@overload
293299
@classmethod
300+
def fromkeys(cls, ks: _ToKeys2D, v: _SCT, /) -> Self: ...
301+
@overload
302+
@classmethod
294303
def fromkeys(cls: type[dok_matrix[np.bool_]], ks: _ToKeys2D, v: onp.ToBool, /) -> dok_matrix[np.bool_]: ...
295304
@overload
296305
@classmethod
@@ -301,9 +310,6 @@ class dok_matrix(_dok_base[_SCT, _2D], spmatrix[_SCT], Generic[_SCT]):
301310
@overload
302311
@classmethod
303312
def fromkeys(cls: type[dok_matrix[np.complex128]], ks: _ToKeys2D, v: opt.JustComplex, /) -> dok_matrix[np.complex128]: ...
304-
@overload
305-
@classmethod
306-
def fromkeys(cls, ks: _ToKeys2D, v: _SCT, /) -> Self: ...
307313

308314
#
309315
def isspmatrix_dok(x: object) -> TypeIs[dok_matrix]: ...

0 commit comments

Comments
 (0)