Skip to content

Commit e72a5c3

Browse files
committed
TYP: fix typing errors in _core.strings
Backports numpy/numtype#115
1 parent 53ec9df commit e72a5c3

File tree

3 files changed

+96
-78
lines changed

3 files changed

+96
-78
lines changed

numpy/_core/strings.pyi

Lines changed: 91 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +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+
]
62+
1563
_StringDTypeArray: TypeAlias = np.ndarray[_Shape, np.dtypes.StringDType]
1664
_StringDTypeSupportsArray: TypeAlias = _SupportsArray[np.dtypes.StringDType]
17-
_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
1866

1967
@overload
2068
def equal(x1: U_co, x2: U_co) -> NDArray[np.bool]: ...
@@ -65,7 +113,7 @@ def add(x1: S_co, x2: S_co) -> NDArray[np.bytes_]: ...
65113
@overload
66114
def add(x1: _StringDTypeSupportsArray, x2: _StringDTypeSupportsArray) -> _StringDTypeArray: ...
67115
@overload
68-
def add(x1: T_co, T_co) -> _StringDTypeOrUnicodeArray: ...
116+
def add(x1: T_co, x2: T_co) -> _StringDTypeOrUnicodeArray: ...
69117

70118
@overload
71119
def multiply(a: U_co, i: i_co) -> NDArray[np.str_]: ...
@@ -77,13 +125,13 @@ def multiply(a: _StringDTypeSupportsArray, i: i_co) -> _StringDTypeArray: ...
77125
def multiply(a: T_co, i: i_co) -> _StringDTypeOrUnicodeArray: ...
78126

79127
@overload
80-
def mod(a: U_co, value: Any) -> NDArray[np.str_]: ...
128+
def mod(a: U_co, value: object) -> NDArray[np.str_]: ...
81129
@overload
82-
def mod(a: S_co, value: Any) -> NDArray[np.bytes_]: ...
130+
def mod(a: S_co, value: object) -> NDArray[np.bytes_]: ...
83131
@overload
84-
def mod(a: _StringDTypeSupportsArray, value: Any) -> _StringDTypeArray: ...
132+
def mod(a: _StringDTypeSupportsArray, value: object) -> _StringDTypeArray: ...
85133
@overload
86-
def mod(a: T_co, value: Any) -> _StringDTypeOrUnicodeArray: ...
134+
def mod(a: T_co, value: object) -> _StringDTypeOrUnicodeArray: ...
87135

88136
def isalpha(x: UST_co) -> NDArray[np.bool]: ...
89137
def isalnum(a: UST_co) -> NDArray[np.bool]: ...
@@ -146,14 +194,14 @@ def index(
146194
a: U_co,
147195
sub: U_co,
148196
start: i_co = ...,
149-
end: None | i_co = ...,
197+
end: i_co | None = ...,
150198
) -> NDArray[np.int_]: ...
151199
@overload
152200
def index(
153201
a: S_co,
154202
sub: S_co,
155203
start: i_co = ...,
156-
end: None | i_co = ...,
204+
end: i_co | None = ...,
157205
) -> NDArray[np.int_]: ...
158206
@overload
159207
def index(
@@ -168,14 +216,14 @@ def rindex(
168216
a: U_co,
169217
sub: U_co,
170218
start: i_co = ...,
171-
end: None | i_co = ...,
219+
end: i_co | None = ...,
172220
) -> NDArray[np.int_]: ...
173221
@overload
174222
def rindex(
175223
a: S_co,
176224
sub: S_co,
177225
start: i_co = ...,
178-
end: None | i_co = ...,
226+
end: i_co | None = ...,
179227
) -> NDArray[np.int_]: ...
180228
@overload
181229
def rindex(
@@ -224,7 +272,7 @@ def startswith(
224272
@overload
225273
def startswith(
226274
a: T_co,
227-
suffix: T_co,
275+
prefix: T_co,
228276
start: i_co = ...,
229277
end: i_co | None = ...,
230278
) -> NDArray[np.bool]: ...
@@ -253,13 +301,13 @@ def endswith(
253301

254302
def decode(
255303
a: S_co,
256-
encoding: None | str = ...,
257-
errors: None | str = ...,
304+
encoding: str | None = None,
305+
errors: str | None = None,
258306
) -> NDArray[np.str_]: ...
259307
def encode(
260308
a: U_co | T_co,
261-
encoding: None | str = ...,
262-
errors: None | str = ...,
309+
encoding: str | None = None,
310+
errors: str | None = None,
263311
) -> NDArray[np.bytes_]: ...
264312

265313
@overload
@@ -272,74 +320,58 @@ def expandtabs(a: _StringDTypeSupportsArray, tabsize: i_co = ...) -> _StringDTyp
272320
def expandtabs(a: T_co, tabsize: i_co = ...) -> _StringDTypeOrUnicodeArray: ...
273321

274322
@overload
275-
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_]: ...
276324
@overload
277-
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_]: ...
278326
@overload
279-
def center(a: _StringDTypeSupportsArray, width: i_co, fillchar: _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
327+
def center(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
280328
@overload
281-
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: ...
282330

283331
@overload
284-
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_]: ...
285333
@overload
286-
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_]: ...
287335
@overload
288-
def ljust(a: _StringDTypeSupportsArray, width: i_co, fillchar: _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
336+
def ljust(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
289337
@overload
290-
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: ...
291339

292340
@overload
293-
def rjust(
294-
a: U_co,
295-
width: i_co,
296-
fillchar: U_co = ...,
297-
) -> NDArray[np.str_]: ...
341+
def rjust(a: U_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.str_]: ...
298342
@overload
299-
def rjust(
300-
a: S_co,
301-
width: i_co,
302-
fillchar: S_co = ...,
303-
) -> NDArray[np.bytes_]: ...
343+
def rjust(a: S_co, width: i_co, fillchar: UST_co = " ") -> NDArray[np.bytes_]: ...
304344
@overload
305-
def rjust(
306-
a: _StringDTypeSupportsArray,
307-
width: i_co,
308-
fillchar: _StringDTypeSupportsArray = ...,
309-
) -> _StringDTypeArray: ...
345+
def rjust(a: _StringDTypeSupportsArray, width: i_co, fillchar: UST_co = " ") -> _StringDTypeArray: ...
310346
@overload
311-
def rjust(
312-
a: T_co,
313-
width: i_co,
314-
fillchar: T_co = ...,
315-
) -> _StringDTypeOrUnicodeArray: ...
347+
def rjust(a: T_co, width: i_co, fillchar: UST_co = " ") -> _StringDTypeOrUnicodeArray: ...
316348

317349
@overload
318-
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_]: ...
319351
@overload
320-
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_]: ...
321353
@overload
322-
def lstrip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
354+
def lstrip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
323355
@overload
324-
def lstrip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
356+
def lstrip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
325357

326358
@overload
327-
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_]: ...
328360
@overload
329-
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_]: ...
330362
@overload
331-
def rstrip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
363+
def rstrip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
332364
@overload
333-
def rstrip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
365+
def rstrip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
334366

335367
@overload
336-
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_]: ...
337369
@overload
338-
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_]: ...
339371
@overload
340-
def strip(a: _StringDTypeSupportsArray, chars: None | _StringDTypeSupportsArray = ...) -> _StringDTypeArray: ...
372+
def strip(a: _StringDTypeSupportsArray, chars: T_co | None = None) -> _StringDTypeArray: ...
341373
@overload
342-
def strip(a: T_co, chars: None | T_co = ...) -> _StringDTypeOrUnicodeArray: ...
374+
def strip(a: T_co, chars: T_co | None = None) -> _StringDTypeOrUnicodeArray: ...
343375

344376
@overload
345377
def zfill(a: U_co, width: i_co) -> NDArray[np.str_]: ...
@@ -424,15 +456,6 @@ def replace(
424456
count: i_co = ...,
425457
) -> _StringDTypeOrUnicodeArray: ...
426458

427-
@overload
428-
def join(sep: U_co, seq: U_co) -> NDArray[np.str_]: ...
429-
@overload
430-
def join(sep: S_co, seq: S_co) -> NDArray[np.bytes_]: ...
431-
@overload
432-
def join(sep: _StringDTypeSupportsArray, seq: _StringDTypeSupportsArray) -> _StringDTypeArray: ...
433-
@overload
434-
def join(sep: T_co, seq: T_co) -> _StringDTypeOrUnicodeArray: ...
435-
436459
@overload
437460
def partition(a: U_co, sep: U_co) -> NDArray[np.str_]: ...
438461
@overload
@@ -455,23 +478,23 @@ def rpartition(a: T_co, sep: T_co) -> _StringDTypeOrUnicodeArray: ...
455478
def translate(
456479
a: U_co,
457480
table: str,
458-
deletechars: None | str = ...,
481+
deletechars: str | None = None,
459482
) -> NDArray[np.str_]: ...
460483
@overload
461484
def translate(
462485
a: S_co,
463486
table: str,
464-
deletechars: None | str = ...,
487+
deletechars: str | None = None,
465488
) -> NDArray[np.bytes_]: ...
466489
@overload
467490
def translate(
468491
a: _StringDTypeSupportsArray,
469492
table: str,
470-
deletechars: None | str = ...,
493+
deletechars: str | None = None,
471494
) -> _StringDTypeArray: ...
472495
@overload
473496
def translate(
474497
a: T_co,
475498
table: str,
476-
deletechars: None | str = ...,
499+
deletechars: str | None = None,
477500
) -> _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)