Skip to content

Commit 2bbc80c

Browse files
authored
Sync Usecols with pandas (#793)
1 parent ebb8ffa commit 2bbc80c

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

pandas-stubs/_typing.pyi

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ from typing import (
1313
Any,
1414
Literal,
1515
Protocol,
16+
SupportsIndex,
1617
TypedDict,
1718
TypeVar,
19+
overload,
1820
)
1921

2022
import numpy as np
@@ -421,6 +423,20 @@ class WriteExcelBuffer(WriteBuffer[bytes], Protocol):
421423

422424
FilePath: TypeAlias = str | PathLike[str]
423425

426+
_T_co = TypeVar("_T_co", covariant=True)
427+
428+
class SequenceNotStr(Protocol[_T_co]):
429+
@overload
430+
def __getitem__(self, index: SupportsIndex, /) -> _T_co: ...
431+
@overload
432+
def __getitem__(self, index: slice, /) -> Sequence[_T_co]: ...
433+
def __contains__(self, value: object, /) -> bool: ...
434+
def __len__(self) -> int: ...
435+
def __iter__(self) -> Iterator[_T_co]: ...
436+
def index(self, value: Any, /, start: int = 0, stop: int = ...) -> int: ...
437+
def count(self, value: Any, /) -> int: ...
438+
def __reversed__(self) -> Iterator[_T_co]: ...
439+
424440
IndexLabel: TypeAlias = Hashable | Sequence[Hashable]
425441
Label: TypeAlias = Hashable | None
426442
Level: TypeAlias = Hashable | int
@@ -492,14 +508,7 @@ np_ndarray_str: TypeAlias = npt.NDArray[np.str_]
492508
IndexType: TypeAlias = slice | np_ndarray_anyint | Index | list[int] | Series[int]
493509
MaskType: TypeAlias = Series[bool] | np_ndarray_bool | list[bool]
494510
UsecolsArgType: TypeAlias = (
495-
MutableSequence[str]
496-
| tuple[str, ...]
497-
| Sequence[int]
498-
| Series
499-
| Index
500-
| np.ndarray
501-
| Callable[[HashableT], bool]
502-
| None
511+
SequenceNotStr[Hashable] | range | AnyArrayLike | Callable[[HashableT], bool] | None
503512
)
504513
# Scratch types for generics
505514

tests/test_io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def test_clipboard():
279279
)
280280
if TYPE_CHECKING_INVALID_USAGE:
281281
pd.read_clipboard(names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
282-
pd.read_clipboard(usecols="abcd") # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
282+
pd.read_clipboard(usecols="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
283283

284284

285285
def test_clipboard_iterator():
@@ -667,7 +667,7 @@ def test_types_read_csv() -> None:
667667

668668
if TYPE_CHECKING_INVALID_USAGE:
669669
pd.read_csv(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
670-
pd.read_csv(path, usecols="abcd") # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
670+
pd.read_csv(path, usecols="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
671671

672672
tfr1: TextFileReader = pd.read_csv(path, nrows=2, iterator=True, chunksize=3)
673673
tfr1.close()
@@ -801,7 +801,7 @@ def test_read_table():
801801
)
802802
if TYPE_CHECKING_INVALID_USAGE:
803803
pd.read_table(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
804-
pd.read_table(path, usecols="abcd") # type: ignore[arg-type] # pyright: ignore[reportGeneralTypeIssues]
804+
pd.read_table(path, usecols="abcd") # type: ignore[call-overload] # pyright: ignore[reportGeneralTypeIssues]
805805

806806

807807
def test_read_table_iterator():

0 commit comments

Comments
 (0)