diff --git a/pandas/_typing.py b/pandas/_typing.py index 7763b0ceb610a..12d23786c3387 100644 --- a/pandas/_typing.py +++ b/pandas/_typing.py @@ -170,8 +170,8 @@ # filenames and file-like-objects Buffer = Union[IO[AnyStr], RawIOBase, BufferedIOBase, TextIOBase, TextIOWrapper, mmap] -FileOrBuffer = Union[str, Buffer[T]] -FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[T]] +FileOrBuffer = Union[str, Buffer[AnyStr]] +FilePathOrBuffer = Union["PathLike[str]", FileOrBuffer[AnyStr]] # for arbitrary kwargs passed during reading/writing files StorageOptions = Optional[Dict[str, Any]] diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 1de19fceff258..99e4888d08be6 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -11,6 +11,7 @@ from typing import ( TYPE_CHECKING, Any, + AnyStr, Callable, Hashable, Mapping, @@ -3295,7 +3296,7 @@ def to_latex( @doc(storage_options=_shared_docs["storage_options"]) def to_csv( self, - path_or_buf: FilePathOrBuffer | None = None, + path_or_buf: FilePathOrBuffer[AnyStr] | None = None, sep: str = ",", na_rep: str = "", float_format: str | None = None, diff --git a/pandas/io/formats/csvs.py b/pandas/io/formats/csvs.py index 915a17fc702c3..e9af2f6676092 100644 --- a/pandas/io/formats/csvs.py +++ b/pandas/io/formats/csvs.py @@ -9,6 +9,7 @@ from typing import ( TYPE_CHECKING, Any, + AnyStr, Hashable, Iterator, Sequence, @@ -48,7 +49,7 @@ class CSVFormatter: def __init__( self, formatter: DataFrameFormatter, - path_or_buf: FilePathOrBuffer[str] = "", + path_or_buf: FilePathOrBuffer[AnyStr] = "", sep: str = ",", cols: Sequence[Hashable] | None = None, index_label: IndexLabel | None = None, diff --git a/pandas/io/formats/format.py b/pandas/io/formats/format.py index 32a5cda4f4083..d1c19f348f901 100644 --- a/pandas/io/formats/format.py +++ b/pandas/io/formats/format.py @@ -19,6 +19,7 @@ IO, TYPE_CHECKING, Any, + AnyStr, Callable, Hashable, Iterable, @@ -1054,7 +1055,7 @@ def to_string( def to_csv( self, - path_or_buf: FilePathOrBuffer[str] | None = None, + path_or_buf: FilePathOrBuffer[AnyStr] | None = None, encoding: str | None = None, sep: str = ",", columns: Sequence[Hashable] | None = None,