Skip to content

Commit 3c7c3e7

Browse files
authored
Merge pull request #28585 from charris/backport-28492
TYP: fix typing errors in ``_core.strings``
2 parents 10632f9 + f514d1d commit 3c7c3e7

File tree

3 files changed

+95
-78
lines changed

3 files changed

+95
-78
lines changed

numpy/_core/strings.pyi

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,57 @@ from numpy._typing import (
1212
_SupportsArray,
1313
)
1414

15+
__all__ = [
16+
"add",
17+
"capitalize",
18+
"center",
19+
"count",
20+
"decode",
21+
"encode",
22+
"endswith",
23+
"equal",
24+
"expandtabs",
25+
"find",
26+
"greater",
27+
"greater_equal",
28+
"index",
29+
"isalnum",
30+
"isalpha",
31+
"isdecimal",
32+
"isdigit",
33+
"islower",
34+
"isnumeric",
35+
"isspace",
36+
"istitle",
37+
"isupper",
38+
"less",
39+
"less_equal",
40+
"ljust",
41+
"lower",
42+
"lstrip",
43+
"mod",
44+
"multiply",
45+
"not_equal",
46+
"partition",
47+
"replace",
48+
"rfind",
49+
"rindex",
50+
"rjust",
51+
"rpartition",
52+
"rstrip",
53+
"startswith",
54+
"str_len",
55+
"strip",
56+
"swapcase",
57+
"title",
58+
"translate",
59+
"upper",
60+
"zfill",
61+
]
1562

1663
_StringDTypeArray: TypeAlias = np.ndarray[_Shape, np.dtypes.StringDType]
1764
_StringDTypeSupportsArray: TypeAlias = _SupportsArray[np.dtypes.StringDType]
18-
_StringDTypeOrUnicodeArray: TypeAlias = np.ndarray[_Shape, np.dtype[np.str_]] | np.ndarray[_Shape, np.dtypes.StringDType]
65+
_StringDTypeOrUnicodeArray: TypeAlias = np.ndarray[_Shape, np.dtype[np.str_]] | _StringDTypeArray
1966

2067
@overload
2168
def equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
@@ -66,7 +113,7 @@ def add(x1: S_co, x2: S_co) -> NDArray[np.bytes_]: ...
66113
@overload
67114
def add(x1: _StringDTypeSupportsArray, x2: _StringDTypeSupportsArray) -> _StringDTypeArray: ...
68115
@overload
69-
def add(x1: T_co, T_co) -> _StringDTypeOrUnicodeArray: ...
116+
def add(x1: T_co, x2: T_co) -> _StringDTypeOrUnicodeArray: ...
70117

71118
@overload
72119
def multiply(a: U_co, i: i_co) -> NDArray[np.str_]: ...
@@ -78,13 +125,13 @@ def multiply(a: _StringDTypeSupportsArray, i: i_co) -> _StringDTypeArray: ...
78125
def multiply(a: T_co, i: i_co) -> _StringDTypeOrUnicodeArray: ...
79126

80127
@overload
81-
def mod(a: U_co, value: Any) -> NDArray[np.str_]: ...
128+
def mod(a: U_co, value: object) -> NDArray[np.str_]: ...
82129
@overload
83-
def mod(a: S_co, value: Any) -> NDArray[np.bytes_]: ...
130+
def mod(a: S_co, value: object) -> NDArray[np.bytes_]: ...
84131
@overload
85-
def mod(a: _StringDTypeSupportsArray, value: Any) -> _StringDTypeArray: ...
132+
def mod(a: _StringDTypeSupportsArray, value: object) -> _StringDTypeArray: ...
86133
@overload
87-
def mod(a: T_co, value: Any) -> _StringDTypeOrUnicodeArray: ...
134+
def mod(a: T_co, value: object) -> _StringDTypeOrUnicodeArray: ...
88135

89136
def isalpha(x: UST_co) -> NDArray[np.bool]: ...
90137
def isalnum(a: UST_co) -> NDArray[np.bool]: ...
@@ -147,14 +194,14 @@ def index(
147194
a: U_co,
148195
sub: U_co,
149196
start: i_co = ...,
150-
end: None | i_co = ...,
197+
end: i_co | None = ...,
151198
) -> NDArray[np.int_]: ...
152199
@overload
153200
def index(
154201
a: S_co,
155202
sub: S_co,
156203
start: i_co = ...,
157-
end: None | i_co = ...,
204+
end: i_co | None = ...,
158205
) -> NDArray[np.int_]: ...
159206
@overload
160207
def index(
@@ -169,14 +216,14 @@ def rindex(
169216
a: U_co,
170217
sub: U_co,
171218
start: i_co = ...,
172-
end: None | i_co = ...,
219+
end: i_co | None = ...,
173220
) -> NDArray[np.int_]: ...
174221
@overload
175222
def rindex(
176223
a: S_co,
177224
sub: S_co,
178225
start: i_co = ...,
179-
end: None | i_co = ...,
226+
end: i_co | None = ...,
180227
) -> NDArray[np.int_]: ...
181228
@overload
182229
def rindex(
@@ -225,7 +272,7 @@ def startswith(
225272
@overload
226273
def startswith(
227274
a: T_co,
228-
suffix: T_co,
275+
prefix: T_co,
229276
start: i_co = ...,
230277
end: i_co | None = ...,
231278
) -> NDArray[np.bool]: ...
@@ -254,13 +301,13 @@ def endswith(
254301

255302
def decode(
256303
a: S_co,
257-
encoding: None | str = ...,
258-
errors: None | str = ...,
304+
encoding: str | None = None,
305+
errors: str | None = None,
259306
) -> NDArray[np.str_]: ...
260307
def encode(
261308
a: U_co | T_co,
262-
encoding: None | str = ...,
263-
errors: None | str = ...,
309+
encoding: str | None = None,
310+
errors: str | None = None,
264311
) -> NDArray[np.bytes_]: ...
265312

266313
@overload
@@ -273,74 +320,58 @@ def expandtabs(a: _StringDTypeSupportsArray, tabsize: i_co = ...) -> _StringDTyp
273320
def expandtabs(a: T_co, tabsize: i_co = ...) -> _StringDTypeOrUnicodeArray: ...
274321

275322
@overload
276-
def center(a: U_co, width: i_co, fillchar: U_co = ...) -> NDArray[np.str_]: ...
323+
def center(a: U_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.str_]: ...
277324
@overload
278-
def center(a: S_co, width: i_co, fillchar: S_co = ...) -> NDArray[np.bytes_]: ...
325+
def center(a: S_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.bytes_]: ...
279326
@overload
280-
def center(a: _StringDTypeSupportsArray, width: i_co, fillchar: _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
327+
def center(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
281328
@overload
282-
def center(a: T_co, width: i_co, fillchar: T_co = ...) -> _StringDTypeOrUnicodeArray: ...
329+
def center(a: T_co, width: i_co, fillchar: UST_co = " ") -> _StringDTypeOrUnicodeArray: ...
283330

284331
@overload
285-
def ljust(a: U_co, width: i_co, fillchar: U_co = ...) -> NDArray[np.str_]: ...
332+
def ljust(a: U_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.str_]: ...
286333
@overload
287-
def ljust(a: S_co, width: i_co, fillchar: S_co = ...) -> NDArray[np.bytes_]: ...
334+
def ljust(a: S_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.bytes_]: ...
288335
@overload
289-
def ljust(a: _StringDTypeSupportsArray, width: i_co, fillchar: _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
336+
def ljust(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
290337
@overload
291-
def ljust(a: T_co, width: i_co, fillchar: T_co = ...) -> _StringDTypeOrUnicodeArray: ...
338+
def ljust(a: T_co, width: i_co, fillchar: UST_co = " ") -> _StringDTypeOrUnicodeArray: ...
292339

293340
@overload
294-
def rjust(
295-
a: U_co,
296-
width: i_co,
297-
fillchar: U_co = ...,
298-
) -> NDArray[np.str_]: ...
341+
def rjust(a: U_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.str_]: ...
299342
@overload
300-
def rjust(
301-
a: S_co,
302-
width: i_co,
303-
fillchar: S_co = ...,
304-
) -> NDArray[np.bytes_]: ...
343+
def rjust(a: S_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.bytes_]: ...
305344
@overload
306-
def rjust(
307-
a: _StringDTypeSupportsArray,
308-
width: i_co,
309-
fillchar: _StringDTypeSupportsArray = ...,
310-
) -> _StringDTypeArray: ...
345+
def rjust(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
311346
@overload
312-
def rjust(
313-
a: T_co,
314-
width: i_co,
315-
fillchar: T_co = ...,
316-
) -> _StringDTypeOrUnicodeArray: ...
347+
def rjust(a: T_co, width: i_co, fillchar: UST_co = " ") -> _StringDTypeOrUnicodeArray: ...
317348

318349
@overload
319-
def lstrip(a: U_co, chars: None | U_co = ...) -> NDArray[np.str_]: ...
350+
def lstrip(a: U_co, chars: U_co | None = None) -> NDArray[np.str_]: ...
320351
@overload
321-
def lstrip(a: S_co, chars: None | S_co = ...) -> NDArray[np.bytes_]: ...
352+
def lstrip(a: S_co, chars: S_co | None = None) -> NDArray[np.bytes_]: ...
322353
@overload
323-
def lstrip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
354+
def lstrip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
324355
@overload
325-
def lstrip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
356+
def lstrip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
326357

327358
@overload
328-
def rstrip(a: U_co, char: None | U_co = ...) -> NDArray[np.str_]: ...
359+
def rstrip(a: U_co, chars: U_co | None = None) -> NDArray[np.str_]: ...
329360
@overload
330-
def rstrip(a: S_co, char: None | S_co = ...) -> NDArray[np.bytes_]: ...
361+
def rstrip(a: S_co, chars: S_co | None = None) -> NDArray[np.bytes_]: ...
331362
@overload
332-
def rstrip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
363+
def rstrip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
333364
@overload
334-
def rstrip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
365+
def rstrip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
335366

336367
@overload
337-
def strip(a: U_co, chars: None | U_co = ...) -> NDArray[np.str_]: ...
368+
def strip(a: U_co, chars: U_co | None = None) -> NDArray[np.str_]: ...
338369
@overload
339-
def strip(a: S_co, chars: None | S_co = ...) -> NDArray[np.bytes_]: ...
370+
def strip(a: S_co, chars: S_co | None = None) -> NDArray[np.bytes_]: ...
340371
@overload
341-
def strip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
372+
def strip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
342373
@overload
343-
def strip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
374+
def strip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
344375

345376
@overload
346377
def zfill(a: U_co, width: i_co) -> NDArray[np.str_]: ...
@@ -425,15 +456,6 @@ def replace(
425456
count: i_co = ...,
426457
) -> _StringDTypeOrUnicodeArray: ...
427458

428-
@overload
429-
def join(sep: U_co, seq: U_co) -> NDArray[np.str_]: ...
430-
@overload
431-
def join(sep: S_co, seq: S_co) -> NDArray[np.bytes_]: ...
432-
@overload
433-
def join(sep: _StringDTypeSupportsArray, seq: _StringDTypeSupportsArray) -> _StringDTypeArray: ...
434-
@overload
435-
def join(sep: T_co, seq: T_co) -> _StringDTypeOrUnicodeArray: ...
436-
437459
@overload
438460
def partition(a: U_co, sep: U_co) -> NDArray[np.str_]: ...
439461
@overload
@@ -456,23 +478,23 @@ def rpartition(a: T_co, sep: T_co) -> _StringDTypeOrUnicodeArray: ...
456478
def translate(
457479
a: U_co,
458480
table: str,
459-
deletechars: None | str = ...,
481+
deletechars: str | None = None,
460482
) -> NDArray[np.str_]: ...
461483
@overload
462484
def translate(
463485
a: S_co,
464486
table: str,
465-
deletechars: None | str = ...,
487+
deletechars: str | None = None,
466488
) -> NDArray[np.bytes_]: ...
467489
@overload
468490
def translate(
469491
a: _StringDTypeSupportsArray,
470492
table: str,
471-
deletechars: None | str = ...,
493+
deletechars: str | None = None,
472494
) -> _StringDTypeArray: ...
473495
@overload
474496
def translate(
475497
a: T_co,
476498
table: str,
477-
deletechars: None | str = ...,
499+
deletechars: str | None = None,
478500
) -> _StringDTypeOrUnicodeArray: ...

numpy/typing/tests/data/fail/strings.pyi

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ np.strings.decode(AR_U) # E: incompatible type
2222
np.strings.join(AR_U, b"_") # E: incompatible type
2323
np.strings.join(AR_S, "_") # E: incompatible type
2424

25-
np.strings.ljust(AR_U, 5, fillchar=b"a") # E: incompatible type
26-
np.strings.ljust(AR_S, 5, fillchar="a") # E: incompatible type
27-
np.strings.rjust(AR_U, 5, fillchar=b"a") # E: incompatible type
28-
np.strings.rjust(AR_S, 5, fillchar="a") # E: incompatible type
29-
3025
np.strings.lstrip(AR_U, b"a") # E: incompatible type
3126
np.strings.lstrip(AR_S, "a") # E: incompatible type
3227
np.strings.strip(AR_U, b"a") # E: incompatible type

numpy/typing/tests/data/reveal/strings.pyi

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,27 @@ assert_type(np.strings.expandtabs(AR_T), AR_T_alias)
6767
assert_type(np.strings.ljust(AR_U, 5), npt.NDArray[np.str_])
6868
assert_type(np.strings.ljust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"]), npt.NDArray[np.bytes_])
6969
assert_type(np.strings.ljust(AR_T, 5), AR_T_alias)
70-
assert_type(np.strings.ljust(AR_T, [4, 2, 1], fillchar=["a", "b", "c"]), AR_TU_alias)
70+
assert_type(np.strings.ljust(AR_T, [4, 2, 1], fillchar=["a", "b", "c"]), AR_T_alias)
7171

7272
assert_type(np.strings.rjust(AR_U, 5), npt.NDArray[np.str_])
7373
assert_type(np.strings.rjust(AR_S, [4, 3, 1], fillchar=[b"a", b"b", b"c"]), npt.NDArray[np.bytes_])
7474
assert_type(np.strings.rjust(AR_T, 5), AR_T_alias)
75-
assert_type(np.strings.rjust(AR_T, [4, 2, 1], fillchar=["a", "b", "c"]), AR_TU_alias)
75+
assert_type(np.strings.rjust(AR_T, [4, 2, 1], fillchar=["a", "b", "c"]), AR_T_alias)
7676

7777
assert_type(np.strings.lstrip(AR_U), npt.NDArray[np.str_])
7878
assert_type(np.strings.lstrip(AR_S, b"_"), npt.NDArray[np.bytes_])
7979
assert_type(np.strings.lstrip(AR_T), AR_T_alias)
80-
assert_type(np.strings.lstrip(AR_T, "_"), AR_TU_alias)
80+
assert_type(np.strings.lstrip(AR_T, "_"), AR_T_alias)
8181

8282
assert_type(np.strings.rstrip(AR_U), npt.NDArray[np.str_])
8383
assert_type(np.strings.rstrip(AR_S, b"_"), npt.NDArray[np.bytes_])
8484
assert_type(np.strings.rstrip(AR_T), AR_T_alias)
85-
assert_type(np.strings.rstrip(AR_T, "_"), AR_TU_alias)
85+
assert_type(np.strings.rstrip(AR_T, "_"), AR_T_alias)
8686

8787
assert_type(np.strings.strip(AR_U), npt.NDArray[np.str_])
8888
assert_type(np.strings.strip(AR_S, b"_"), npt.NDArray[np.bytes_])
8989
assert_type(np.strings.strip(AR_T), AR_T_alias)
90-
assert_type(np.strings.strip(AR_T, "_"), AR_TU_alias)
90+
assert_type(np.strings.strip(AR_T, "_"), AR_T_alias)
9191

9292
assert_type(np.strings.count(AR_U, "a", start=[1, 2, 3]), npt.NDArray[np.int_])
9393
assert_type(np.strings.count(AR_S, [b"a", b"b", b"c"], end=9), npt.NDArray[np.int_])

0 commit comments

Comments
 (0)