Skip to content

read_csv: parameter usecols has wrong type hint #963

Open
@clo-vis

Description

@clo-vis

To Reproduce

from collections.abc import Sequence
from pandas import read_csv
cols1: Sequence[str] = ["a"]
def cols2(x: set[float]) -> bool:  return sum(x) < 1.0
read_csv("file.csv", usecols=cols1) # mypy: "No overload variant of "read_csv" matches argument types "str", "Sequence[str]"
read_csv("file.csv", usecols=cols2) # no error from mypy

Expected behavior:
No error for usecols=cols1, but an error for usecols=cols2

Actual behavior:
cols1 is not accepted, even though Sequence[str] is "list like" (at least I think so; the term is nowhere defined) and its elements are "strings that correspond to column names"
cols2 is accepted, even though it is not a callable that can be "evaluated against the column names, returning names where the callable function evaluates to True"

Please complete the following information:

  • OS: Microsoft Windows
  • OS Version 10.0.19045.4651
  • python version 3.11.9
  • version of type checker: mypy 1.11.0
  • version of installed pandas-stubs: 2.2.2.240603

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions