9
9
import re
10
10
import sys
11
11
from typing import (
12
+ IO ,
12
13
DefaultDict ,
13
14
Hashable ,
14
15
Iterator ,
16
+ Literal ,
15
17
Mapping ,
16
18
Sequence ,
17
19
cast ,
@@ -1135,9 +1137,17 @@ class FixedWidthReader(abc.Iterator):
1135
1137
A reader of fixed-width lines.
1136
1138
"""
1137
1139
1138
- def __init__ (self , f , colspecs , delimiter , comment , skiprows = None , infer_nrows = 100 ):
1140
+ def __init__ (
1141
+ self ,
1142
+ f : IO [str ],
1143
+ colspecs : list [tuple [int , int ]] | Literal ["infer" ],
1144
+ delimiter : str | None ,
1145
+ comment : str | None ,
1146
+ skiprows : set [int ] | None = None ,
1147
+ infer_nrows : int = 100 ,
1148
+ ) -> None :
1139
1149
self .f = f
1140
- self .buffer = None
1150
+ self .buffer : Iterator | None = None
1141
1151
self .delimiter = "\r \n " + delimiter if delimiter else "\n \r \t "
1142
1152
self .comment = comment
1143
1153
if colspecs == "infer" :
@@ -1165,7 +1175,7 @@ def __init__(self, f, colspecs, delimiter, comment, skiprows=None, infer_nrows=1
1165
1175
"2 element tuple or list of integers"
1166
1176
)
1167
1177
1168
- def get_rows (self , infer_nrows , skiprows = None ):
1178
+ def get_rows (self , infer_nrows : int , skiprows : set [ int ] | None = None ) -> list [ str ] :
1169
1179
"""
1170
1180
Read rows from self.f, skipping as specified.
1171
1181
@@ -1203,7 +1213,9 @@ def get_rows(self, infer_nrows, skiprows=None):
1203
1213
self .buffer = iter (buffer_rows )
1204
1214
return detect_rows
1205
1215
1206
- def detect_colspecs (self , infer_nrows = 100 , skiprows = None ):
1216
+ def detect_colspecs (
1217
+ self , infer_nrows : int = 100 , skiprows : set [int ] | None = None
1218
+ ) -> list [tuple [int , int ]]:
1207
1219
# Regex escape the delimiters
1208
1220
delimiters = "" .join ([fr"\{ x } " for x in self .delimiter ])
1209
1221
pattern = re .compile (f"([^{ delimiters } ]+)" )
@@ -1223,7 +1235,7 @@ def detect_colspecs(self, infer_nrows=100, skiprows=None):
1223
1235
edge_pairs = list (zip (edges [::2 ], edges [1 ::2 ]))
1224
1236
return edge_pairs
1225
1237
1226
- def __next__ (self ):
1238
+ def __next__ (self ) -> list [ str ] :
1227
1239
if self .buffer is not None :
1228
1240
try :
1229
1241
line = next (self .buffer )
@@ -1242,13 +1254,15 @@ class FixedWidthFieldParser(PythonParser):
1242
1254
See PythonParser for details.
1243
1255
"""
1244
1256
1245
- def __init__ (self , f , ** kwds ):
1257
+ def __init__ (
1258
+ self , f : FilePath | ReadCsvBuffer [bytes ] | ReadCsvBuffer [str ], ** kwds
1259
+ ) -> None :
1246
1260
# Support iterators, convert to a list.
1247
1261
self .colspecs = kwds .pop ("colspecs" )
1248
1262
self .infer_nrows = kwds .pop ("infer_nrows" )
1249
1263
PythonParser .__init__ (self , f , ** kwds )
1250
1264
1251
- def _make_reader (self , f ) :
1265
+ def _make_reader (self , f : IO [ str ]) -> None :
1252
1266
self .data = FixedWidthReader (
1253
1267
f ,
1254
1268
self .colspecs ,
@@ -1258,7 +1272,7 @@ def _make_reader(self, f):
1258
1272
self .infer_nrows ,
1259
1273
)
1260
1274
1261
- def _remove_empty_lines (self , lines ) -> list :
1275
+ def _remove_empty_lines (self , lines : list [ list [ Scalar ]] ) -> list [ list [ Scalar ]] :
1262
1276
"""
1263
1277
Returns the list of lines without the empty ones. With fixed-width
1264
1278
fields, empty lines become arrays of empty strings.
0 commit comments