1313 DefaultDict ,
1414 Hashable ,
1515 Iterator ,
16+ List ,
1617 Literal ,
1718 Mapping ,
1819 Sequence ,
3738from pandas .core .dtypes .common import is_integer
3839from pandas .core .dtypes .inference import is_dict_like
3940
41+ from pandas import (
42+ Index ,
43+ MultiIndex ,
44+ )
45+
4046from pandas .io .parsers .base_parser import (
4147 ParserBase ,
4248 parser_defaults ,
@@ -167,7 +173,7 @@ def __init__(self, f: ReadCsvBuffer[str] | list, **kwds):
167173 )
168174 self .num = re .compile (regex )
169175
170- def _make_reader (self , f ) -> None :
176+ def _make_reader (self , f : IO [ str ] | ReadCsvBuffer [ str ] ) -> None :
171177 sep = self .delimiter
172178
173179 if sep is None or len (sep ) == 1 :
@@ -198,10 +204,11 @@ class MyDialect(csv.Dialect):
198204 self .pos += 1
199205 line = f .readline ()
200206 lines = self ._check_comments ([[line ]])[0 ]
207+ lines_str = cast (List [str ], lines )
201208
202209 # since `line` was a string, lines will be a list containing
203210 # only a single string
204- line = lines [0 ]
211+ line = lines_str [0 ]
205212
206213 self .pos += 1
207214 self .line_pos += 1
@@ -233,7 +240,11 @@ def _read():
233240 # TextIOWrapper, mmap, None]")
234241 self .data = reader # type: ignore[assignment]
235242
236- def read (self , rows : int | None = None ):
243+ def read (
244+ self , rows : int | None = None
245+ ) -> tuple [
246+ Index | None , Sequence [Hashable ] | MultiIndex , Mapping [Hashable , ArrayLike ]
247+ ]:
237248 try :
238249 content = self ._get_lines (rows )
239250 except StopIteration :
@@ -273,9 +284,11 @@ def read(self, rows: int | None = None):
273284 conv_data = self ._convert_data (data )
274285 columns , conv_data = self ._do_date_conversions (columns , conv_data )
275286
276- index , columns = self ._make_index (conv_data , alldata , columns , indexnamerow )
287+ index , result_columns = self ._make_index (
288+ conv_data , alldata , columns , indexnamerow
289+ )
277290
278- return index , columns , conv_data
291+ return index , result_columns , conv_data
279292
280293 def _exclude_implicit_index (
281294 self ,
@@ -586,7 +599,7 @@ def _handle_usecols(
586599 self ._col_indices = sorted (col_indices )
587600 return columns
588601
589- def _buffered_line (self ):
602+ def _buffered_line (self ) -> list [ Scalar ] :
590603 """
591604 Return a line from buffer, filling buffer if required.
592605 """
@@ -878,7 +891,9 @@ def _clear_buffer(self) -> None:
878891
879892 _implicit_index = False
880893
881- def _get_index_name (self , columns : list [Hashable ]):
894+ def _get_index_name (
895+ self , columns : list [Hashable ]
896+ ) -> tuple [list [Hashable ] | None , list [Hashable ], list [Hashable ]]:
882897 """
883898 Try several cases to get lines:
884899
@@ -943,8 +958,8 @@ def _get_index_name(self, columns: list[Hashable]):
943958
944959 else :
945960 # Case 2
946- (index_name , columns_ , self .index_col ) = self ._clean_index_names (
947- columns , self .index_col , self . unnamed_cols
961+ (index_name , _ , self .index_col ) = self ._clean_index_names (
962+ columns , self .index_col
948963 )
949964
950965 return index_name , orig_names , columns
@@ -1036,7 +1051,7 @@ def _rows_to_cols(self, content: list[list[Scalar]]) -> list[np.ndarray]:
10361051 ]
10371052 return zipped_content
10381053
1039- def _get_lines (self , rows : int | None = None ):
1054+ def _get_lines (self , rows : int | None = None ) -> list [ list [ Scalar ]] :
10401055 lines = self .buf
10411056 new_rows = None
10421057
@@ -1133,7 +1148,7 @@ class FixedWidthReader(abc.Iterator):
11331148
11341149 def __init__ (
11351150 self ,
1136- f : IO [str ],
1151+ f : IO [str ] | ReadCsvBuffer [ str ] ,
11371152 colspecs : list [tuple [int , int ]] | Literal ["infer" ],
11381153 delimiter : str | None ,
11391154 comment : str | None ,
@@ -1230,14 +1245,16 @@ def detect_colspecs(
12301245 return edge_pairs
12311246
12321247 def __next__ (self ) -> list [str ]:
1248+ # Argument 1 to "next" has incompatible type "Union[IO[str],
1249+ # ReadCsvBuffer[str]]"; expected "SupportsNext[str]"
12331250 if self .buffer is not None :
12341251 try :
12351252 line = next (self .buffer )
12361253 except StopIteration :
12371254 self .buffer = None
1238- line = next (self .f )
1255+ line = next (self .f ) # type: ignore[arg-type]
12391256 else :
1240- line = next (self .f )
1257+ line = next (self .f ) # type: ignore[arg-type]
12411258 # Note: 'colspecs' is a sequence of half-open intervals.
12421259 return [line [fromm :to ].strip (self .delimiter ) for (fromm , to ) in self .colspecs ]
12431260
@@ -1254,7 +1271,7 @@ def __init__(self, f: ReadCsvBuffer[str], **kwds) -> None:
12541271 self .infer_nrows = kwds .pop ("infer_nrows" )
12551272 PythonParser .__init__ (self , f , ** kwds )
12561273
1257- def _make_reader (self , f : IO [str ]) -> None :
1274+ def _make_reader (self , f : IO [str ] | ReadCsvBuffer [ str ] ) -> None :
12581275 self .data = FixedWidthReader (
12591276 f ,
12601277 self .colspecs ,
0 commit comments