Skip to content

GH963 Restrict callable to hashableT for read_csv #1234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pandas-stubs/io/parsers/readers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ from pandas._typing import (
DtypeArg,
DtypeBackend,
FilePath,
HashableT,
ListLikeHashable,
ReadCsvBuffer,
StorageOptions,
Expand All @@ -44,7 +45,7 @@ def read_csv(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down Expand Up @@ -108,7 +109,7 @@ def read_csv(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down Expand Up @@ -172,7 +173,7 @@ def read_csv(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down Expand Up @@ -236,7 +237,7 @@ def read_table(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down Expand Up @@ -300,7 +301,7 @@ def read_table(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down Expand Up @@ -364,7 +365,7 @@ def read_table(
header: int | Sequence[int] | Literal["infer"] | None = ...,
names: ListLikeHashable | None = ...,
index_col: int | str | Sequence[str | int] | Literal[False] | None = ...,
usecols: UsecolsArgType = ...,
usecols: UsecolsArgType[HashableT] = ...,
dtype: DtypeArg | defaultdict | None = ...,
engine: CSVEngine | None = ...,
converters: (
Expand Down
10 changes: 10 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,11 @@ def test_read_csv():
DataFrame,
)

def cols(x: str) -> bool:
return x in ["a", "b"]

pd.read_csv(path, usecols=cols)


def test_read_csv_iterator():
with ensure_clean() as path:
Expand Down Expand Up @@ -727,6 +732,11 @@ def test_types_read_csv() -> None:
pd.read_csv(path, names="abcd") # type: ignore[call-overload] # pyright: ignore[reportArgumentType]
pd.read_csv(path, usecols="abcd") # type: ignore[call-overload] # pyright: ignore[reportArgumentType]

def cols2(x: set[float]) -> bool:
return sum(x) < 1.0

pd.read_csv("file.csv", usecols=cols2) # type: ignore[type-var] # pyright: ignore[reportArgumentType]

tfr1 = pd.read_csv(path, nrows=2, iterator=True, chunksize=3)
check(assert_type(tfr1, TextFileReader), TextFileReader)
tfr1.close()
Expand Down