13
13
DefaultDict ,
14
14
Hashable ,
15
15
Iterator ,
16
+ List ,
16
17
Literal ,
17
18
Mapping ,
18
19
Sequence ,
37
38
from pandas .core .dtypes .common import is_integer
38
39
from pandas .core .dtypes .inference import is_dict_like
39
40
41
+ from pandas import (
42
+ Index ,
43
+ MultiIndex ,
44
+ )
45
+
40
46
from pandas .io .parsers .base_parser import (
41
47
ParserBase ,
42
48
parser_defaults ,
@@ -167,7 +173,7 @@ def __init__(self, f: ReadCsvBuffer[str] | list, **kwds):
167
173
)
168
174
self .num = re .compile (regex )
169
175
170
- def _make_reader (self , f ) -> None :
176
+ def _make_reader (self , f : IO [ str ] | ReadCsvBuffer [ str ] ) -> None :
171
177
sep = self .delimiter
172
178
173
179
if sep is None or len (sep ) == 1 :
@@ -198,10 +204,11 @@ class MyDialect(csv.Dialect):
198
204
self .pos += 1
199
205
line = f .readline ()
200
206
lines = self ._check_comments ([[line ]])[0 ]
207
+ lines_str = cast (List [str ], lines )
201
208
202
209
# since `line` was a string, lines will be a list containing
203
210
# only a single string
204
- line = lines [0 ]
211
+ line = lines_str [0 ]
205
212
206
213
self .pos += 1
207
214
self .line_pos += 1
@@ -233,7 +240,11 @@ def _read():
233
240
# TextIOWrapper, mmap, None]")
234
241
self .data = reader # type: ignore[assignment]
235
242
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
+ ]:
237
248
try :
238
249
content = self ._get_lines (rows )
239
250
except StopIteration :
@@ -273,9 +284,11 @@ def read(self, rows: int | None = None):
273
284
conv_data = self ._convert_data (data )
274
285
columns , conv_data = self ._do_date_conversions (columns , conv_data )
275
286
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
+ )
277
290
278
- return index , columns , conv_data
291
+ return index , result_columns , conv_data
279
292
280
293
def _exclude_implicit_index (
281
294
self ,
@@ -586,7 +599,7 @@ def _handle_usecols(
586
599
self ._col_indices = sorted (col_indices )
587
600
return columns
588
601
589
- def _buffered_line (self ):
602
+ def _buffered_line (self ) -> list [ Scalar ] :
590
603
"""
591
604
Return a line from buffer, filling buffer if required.
592
605
"""
@@ -878,7 +891,9 @@ def _clear_buffer(self) -> None:
878
891
879
892
_implicit_index = False
880
893
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 ]]:
882
897
"""
883
898
Try several cases to get lines:
884
899
@@ -943,8 +958,8 @@ def _get_index_name(self, columns: list[Hashable]):
943
958
944
959
else :
945
960
# 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
948
963
)
949
964
950
965
return index_name , orig_names , columns
@@ -1036,7 +1051,7 @@ def _rows_to_cols(self, content: list[list[Scalar]]) -> list[np.ndarray]:
1036
1051
]
1037
1052
return zipped_content
1038
1053
1039
- def _get_lines (self , rows : int | None = None ):
1054
+ def _get_lines (self , rows : int | None = None ) -> list [ list [ Scalar ]] :
1040
1055
lines = self .buf
1041
1056
new_rows = None
1042
1057
@@ -1133,7 +1148,7 @@ class FixedWidthReader(abc.Iterator):
1133
1148
1134
1149
def __init__ (
1135
1150
self ,
1136
- f : IO [str ],
1151
+ f : IO [str ] | ReadCsvBuffer [ str ] ,
1137
1152
colspecs : list [tuple [int , int ]] | Literal ["infer" ],
1138
1153
delimiter : str | None ,
1139
1154
comment : str | None ,
@@ -1230,14 +1245,16 @@ def detect_colspecs(
1230
1245
return edge_pairs
1231
1246
1232
1247
def __next__ (self ) -> list [str ]:
1248
+ # Argument 1 to "next" has incompatible type "Union[IO[str],
1249
+ # ReadCsvBuffer[str]]"; expected "SupportsNext[str]"
1233
1250
if self .buffer is not None :
1234
1251
try :
1235
1252
line = next (self .buffer )
1236
1253
except StopIteration :
1237
1254
self .buffer = None
1238
- line = next (self .f )
1255
+ line = next (self .f ) # type: ignore[arg-type]
1239
1256
else :
1240
- line = next (self .f )
1257
+ line = next (self .f ) # type: ignore[arg-type]
1241
1258
# Note: 'colspecs' is a sequence of half-open intervals.
1242
1259
return [line [fromm :to ].strip (self .delimiter ) for (fromm , to ) in self .colspecs ]
1243
1260
@@ -1254,7 +1271,7 @@ def __init__(self, f: ReadCsvBuffer[str], **kwds) -> None:
1254
1271
self .infer_nrows = kwds .pop ("infer_nrows" )
1255
1272
PythonParser .__init__ (self , f , ** kwds )
1256
1273
1257
- def _make_reader (self , f : IO [str ]) -> None :
1274
+ def _make_reader (self , f : IO [str ] | ReadCsvBuffer [ str ] ) -> None :
1258
1275
self .data = FixedWidthReader (
1259
1276
f ,
1260
1277
self .colspecs ,
0 commit comments