Skip to content

Commit a9dc8ba

Browse files
authored
Broaden read csv param types (#630)
* gh-623: broaden 'names' param of read_csv Broaden the type hint for the 'names' param of read_csv (and read_table, which behaves similarly) from previous list[str], so that other valid types are accepted by mypy. * allow None as names param of read_clipboard Noticed as I found clipboard after the changes to read_csv and read_table, and it calls it, so should match - but it was missing None as an option. * broaden 'names' param of read_clipboard Match prior change to read_csv, since read_clipboard calls read_csv. * broaden 'names' param of read_excel Match prior change to read_csv, read_table, read_clipboard. * gh-605: broader usecols param type hint This fixes the pycharm tooltip problem in gh-605, as well as allowing more list-like types of strings (tuples of strings, as well as mutable sequences of strings other than list), and callables that accept hashables, not just strings. * test that read_excel accepts string for usecols * test names and usecols correctly exclude strings Strings aren't valid arguments here (except for read_excel, where we have a test now to check that this is accepted). Adding tests to make sure the type hints aren't overly wide and accept string arguments by mistake.
1 parent 5945ada commit a9dc8ba

File tree

5 files changed

+325
-100
lines changed

5 files changed

+325
-100
lines changed

pandas-stubs/_typing.pyi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ ListLikeExceptSeriesAndStr = TypeVar(
269269
"ListLikeExceptSeriesAndStr", MutableSequence, np.ndarray, tuple, "Index"
270270
)
271271
ListLikeU: TypeAlias = Sequence | np.ndarray | Series | Index
272+
ListLikeHashable: TypeAlias = (
273+
MutableSequence[HashableT] | np.ndarray | tuple[HashableT, ...] | range
274+
)
272275
StrLike: TypeAlias = str | np.str_
273276
IndexIterScalar: TypeAlias = (
274277
str
@@ -295,6 +298,16 @@ np_ndarray_str: TypeAlias = npt.NDArray[np.str_]
295298

296299
IndexType: TypeAlias = slice | np_ndarray_anyint | Index | list[int] | Series[int]
297300
MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]
301+
UsecolsArgType: TypeAlias = (
302+
MutableSequence[str]
303+
| tuple[str, ...]
304+
| Sequence[int]
305+
| Series
306+
| Index
307+
| np.ndarray
308+
| Callable[[HashableT], bool]
309+
| None
310+
)
298311
# Scratch types for generics
299312
S1 = TypeVar(
300313
"S1",

pandas-stubs/io/clipboards.pyi

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ from typing import (
1111
)
1212

1313
from pandas.core.frame import DataFrame
14-
from pandas.core.indexes.base import Index
15-
from pandas.core.series import Series
1614

1715
from pandas._typing import (
1816
CompressionOptions,
1917
CSVEngine,
2018
CSVQuoting,
2119
DtypeArg,
20+
ListLikeHashable,
2221
StorageOptions,
23-
npt,
22+
UsecolsArgType,
2423
)
2524

2625
from pandas.io.parsers import TextFileReader
@@ -31,15 +30,9 @@ def read_clipboard(
3130
*,
3231
delimiter: str | None = ...,
3332
header: int | Sequence[int] | Literal["infer"] | None = ...,
34-
names: list[str] = ...,
33+
names: ListLikeHashable | None = ...,
3534
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
36-
usecols: list[str]
37-
| Sequence[int]
38-
| Series
39-
| Index
40-
| npt.NDArray
41-
| Callable[[str], bool]
42-
| None = ...,
35+
usecols: UsecolsArgType = ...,
4336
dtype: DtypeArg | defaultdict | None = ...,
4437
engine: CSVEngine | None = ...,
4538
converters: dict[int | str, Callable[[str], Any]] = ...,
@@ -94,15 +87,9 @@ def read_clipboard(
9487
*,
9588
delimiter: str | None = ...,
9689
header: int | Sequence[int] | Literal["infer"] | None = ...,
97-
names: list[str] = ...,
90+
names: ListLikeHashable | None = ...,
9891
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
99-
usecols: list[str]
100-
| Sequence[int]
101-
| Series
102-
| Index
103-
| npt.NDArray
104-
| Callable[[str], bool]
105-
| None = ...,
92+
usecols: UsecolsArgType = ...,
10693
dtype: DtypeArg | defaultdict | None = ...,
10794
engine: CSVEngine | None = ...,
10895
converters: dict[int | str, Callable[[str], Any]] = ...,
@@ -157,15 +144,9 @@ def read_clipboard(
157144
*,
158145
delimiter: str | None = ...,
159146
header: int | Sequence[int] | Literal["infer"] | None = ...,
160-
names: list[str] = ...,
147+
names: ListLikeHashable | None = ...,
161148
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
162-
usecols: list[str]
163-
| Sequence[int]
164-
| Series
165-
| Index
166-
| npt.NDArray
167-
| Callable[[str], bool]
168-
| None = ...,
149+
usecols: UsecolsArgType = ...,
169150
dtype: DtypeArg | defaultdict | None = ...,
170151
engine: CSVEngine | None = ...,
171152
converters: dict[int | str, Callable[[str], Any]] = ...,

pandas-stubs/io/excel/_base.pyi

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ from xlrd.book import Book
2222
from pandas._typing import (
2323
Dtype,
2424
FilePath,
25+
ListLikeHashable,
2526
ReadBuffer,
2627
StorageOptions,
28+
UsecolsArgType,
2729
WriteExcelBuffer,
2830
)
2931

@@ -40,9 +42,9 @@ def read_excel(
4042
sheet_name: list[int | str] | None,
4143
*,
4244
header: int | Sequence[int] | None = ...,
43-
names: list[str] | None = ...,
45+
names: ListLikeHashable | None = ...,
4446
index_col: int | Sequence[int] | None = ...,
45-
usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ...,
47+
usecols: str | UsecolsArgType = ...,
4648
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
4749
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
4850
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
@@ -78,9 +80,9 @@ def read_excel(
7880
sheet_name: int | str = ...,
7981
*,
8082
header: int | Sequence[int] | None = ...,
81-
names: list[str] | None = ...,
83+
names: ListLikeHashable | None = ...,
8284
index_col: int | Sequence[int] | None = ...,
83-
usecols: Sequence[int] | Sequence[str] | Callable[[str], bool] | None = ...,
85+
usecols: str | UsecolsArgType = ...,
8486
dtype: str | Dtype | Mapping[str, str | Dtype] | None = ...,
8587
engine: Literal["xlrd", "openpyxl", "odf", "pyxlsb"] | None = ...,
8688
converters: Mapping[int | str, Callable[[object], object]] | None = ...,
@@ -155,13 +157,9 @@ class ExcelFile:
155157
self,
156158
sheet_name: list[int | str] | None,
157159
header: int | Sequence[int] | None = ...,
158-
names: list[str] | None = ...,
160+
names: ListLikeHashable | None = ...,
159161
index_col: int | Sequence[int] | None = ...,
160-
usecols: str
161-
| Sequence[int]
162-
| Sequence[str]
163-
| Callable[[str], bool]
164-
| None = ...,
162+
usecols: str | UsecolsArgType = ...,
165163
converters: dict[int | str, Callable[[object], object]] | None = ...,
166164
true_values: Iterable[Hashable] | None = ...,
167165
false_values: Iterable[Hashable] | None = ...,
@@ -185,13 +183,9 @@ class ExcelFile:
185183
self,
186184
sheet_name: int | str,
187185
header: int | Sequence[int] | None = ...,
188-
names: list[str] | None = ...,
186+
names: ListLikeHashable | None = ...,
189187
index_col: int | Sequence[int] | None = ...,
190-
usecols: str
191-
| Sequence[int]
192-
| Sequence[str]
193-
| Callable[[str], bool]
194-
| None = ...,
188+
usecols: str | UsecolsArgType = ...,
195189
converters: dict[int | str, Callable[[object], object]] | None = ...,
196190
true_values: Iterable[Hashable] | None = ...,
197191
false_values: Iterable[Hashable] | None = ...,

pandas-stubs/io/parsers/readers.pyi

Lines changed: 14 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ from typing import (
1616
)
1717

1818
from pandas.core.frame import DataFrame
19-
from pandas.core.indexes.base import Index
20-
from pandas.core.series import Series
2119
from typing_extensions import Self
2220

2321
from pandas._typing import (
@@ -26,9 +24,10 @@ from pandas._typing import (
2624
CSVQuoting,
2725
DtypeArg,
2826
FilePath,
27+
ListLikeHashable,
2928
ReadCsvBuffer,
3029
StorageOptions,
31-
npt,
30+
UsecolsArgType,
3231
)
3332

3433
from pandas.io.common import IOHandles
@@ -40,16 +39,9 @@ def read_csv(
4039
sep: str | None = ...,
4140
delimiter: str | None = ...,
4241
header: int | Sequence[int] | Literal["infer"] | None = ...,
43-
names: list[str] | None = ...,
42+
names: ListLikeHashable | None = ...,
4443
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
45-
usecols: list[str]
46-
| tuple[str, ...]
47-
| Sequence[int]
48-
| Series
49-
| Index
50-
| npt.NDArray
51-
| Callable[[str], bool]
52-
| None = ...,
44+
usecols: UsecolsArgType = ...,
5345
dtype: DtypeArg | defaultdict | None = ...,
5446
engine: CSVEngine | None = ...,
5547
converters: Mapping[int | str, Callable[[str], Any]]
@@ -106,16 +98,9 @@ def read_csv(
10698
sep: str | None = ...,
10799
delimiter: str | None = ...,
108100
header: int | Sequence[int] | Literal["infer"] | None = ...,
109-
names: list[str] | None = ...,
101+
names: ListLikeHashable | None = ...,
110102
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
111-
usecols: list[str]
112-
| tuple[str, ...]
113-
| Sequence[int]
114-
| Series
115-
| Index
116-
| npt.NDArray
117-
| Callable[[str], bool]
118-
| None = ...,
103+
usecols: UsecolsArgType = ...,
119104
dtype: DtypeArg | defaultdict | None = ...,
120105
engine: CSVEngine | None = ...,
121106
converters: Mapping[int | str, Callable[[str], Any]]
@@ -172,16 +157,9 @@ def read_csv(
172157
sep: str | None = ...,
173158
delimiter: str | None = ...,
174159
header: int | Sequence[int] | Literal["infer"] | None = ...,
175-
names: list[str] | None = ...,
160+
names: ListLikeHashable | None = ...,
176161
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
177-
usecols: list[str]
178-
| tuple[str, ...]
179-
| Sequence[int]
180-
| Series
181-
| Index
182-
| npt.NDArray
183-
| Callable[[str], bool]
184-
| None = ...,
162+
usecols: UsecolsArgType = ...,
185163
dtype: DtypeArg | defaultdict | None = ...,
186164
engine: CSVEngine | None = ...,
187165
converters: Mapping[int | str, Callable[[str], Any]]
@@ -238,16 +216,9 @@ def read_table(
238216
sep: str | None = ...,
239217
delimiter: str | None = ...,
240218
header: int | Sequence[int] | Literal["infer"] | None = ...,
241-
names: list[str] | None = ...,
219+
names: ListLikeHashable | None = ...,
242220
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
243-
usecols: list[str]
244-
| tuple[str, ...]
245-
| Sequence[int]
246-
| Series
247-
| Index
248-
| npt.NDArray
249-
| Callable[[str], bool]
250-
| None = ...,
221+
usecols: UsecolsArgType = ...,
251222
dtype: DtypeArg | defaultdict | None = ...,
252223
engine: CSVEngine | None = ...,
253224
converters: Mapping[int | str, Callable[[str], Any]]
@@ -304,16 +275,9 @@ def read_table(
304275
sep: str | None = ...,
305276
delimiter: str | None = ...,
306277
header: int | Sequence[int] | Literal["infer"] | None = ...,
307-
names: list[str] | None = ...,
278+
names: ListLikeHashable | None = ...,
308279
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
309-
usecols: list[str]
310-
| tuple[str, ...]
311-
| Sequence[int]
312-
| Series
313-
| Index
314-
| npt.NDArray
315-
| Callable[[str], bool]
316-
| None = ...,
280+
usecols: UsecolsArgType = ...,
317281
dtype: DtypeArg | defaultdict | None = ...,
318282
engine: CSVEngine | None = ...,
319283
converters: Mapping[int | str, Callable[[str], Any]]
@@ -370,16 +334,9 @@ def read_table(
370334
sep: str | None = ...,
371335
delimiter: str | None = ...,
372336
header: int | Sequence[int] | Literal["infer"] | None = ...,
373-
names: list[str] | None = ...,
337+
names: ListLikeHashable | None = ...,
374338
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
375-
usecols: list[str]
376-
| tuple[str, ...]
377-
| Sequence[int]
378-
| Series
379-
| Index
380-
| npt.NDArray
381-
| Callable[[str], bool]
382-
| None = ...,
339+
usecols: UsecolsArgType = ...,
383340
dtype: DtypeArg | defaultdict | None = ...,
384341
engine: CSVEngine | None = ...,
385342
converters: Mapping[int | str, Callable[[str], Any]]

0 commit comments

Comments
 (0)