1414import time
1515import urllib .parse as urlparse
1616from array import array
17+ from collections .abc import Iterator
1718from typing import IO , Any , cast
1819
1920import msgpack
2021import urllib3
21- import urllib3 .util
2222
2323from tdclient import errors , version
2424from tdclient .bulk_import_api import BulkImportAPI
3131from tdclient .schedule_api import ScheduleAPI
3232from tdclient .server_status_api import ServerStatusAPI
3333from tdclient .table_api import TableAPI
34- from tdclient .types import BytesOrStream , StreamBody
34+ from tdclient .types import BytesOrStream , DataFormat , FileLike , StreamBody
3535from tdclient .user_api import UserAPI
3636from tdclient .util import (
3737 csv_dict_record_reader ,
4242)
4343
4444try :
45- import certifi
45+ import certifi # type: ignore[reportMissingImports]
4646except ImportError :
4747 certifi = None
4848
@@ -576,7 +576,9 @@ def close(self) -> None:
576576 # all connections in pool will be closed eventually during gc.
577577 self .http .clear ()
578578
579- def _prepare_file (self , file_like , fmt , ** kwargs ):
579+ def _prepare_file (
580+ self , file_like : FileLike , fmt : DataFormat , ** kwargs : Any
581+ ) -> IO [bytes ]:
580582 fp = tempfile .TemporaryFile ()
581583 with contextlib .closing (gzip .GzipFile (mode = "wb" , fileobj = fp )) as gz :
582584 packer = msgpack .Packer ()
@@ -591,34 +593,41 @@ def _prepare_file(self, file_like, fmt, **kwargs):
591593 fp .seek (0 )
592594 return fp
593595
594- def _read_file (self , file_like , fmt , ** kwargs ) :
596+ def _read_file (self , file_like : FileLike , fmt : DataFormat , ** kwargs : Any ) -> Any :
595597 compressed = fmt .endswith (".gz" )
598+ fmt_str = str (fmt )
596599 if compressed :
597- fmt = fmt [0 : len (fmt ) - len (".gz" )]
598- reader_name = f"_read_{ fmt } _file"
600+ fmt_str = fmt_str [0 : len (fmt_str ) - len (".gz" )]
601+ reader_name = f"_read_{ fmt_str } _file"
599602 if hasattr (self , reader_name ):
600603 reader = getattr (self , reader_name )
601604 else :
602605 raise TypeError (f"unknown format: { fmt } " )
603606 if hasattr (file_like , "read" ):
604607 if compressed :
605- file_like = gzip .GzipFile (fileobj = file_like )
608+ file_like = gzip .GzipFile (fileobj = file_like ) # type: ignore[arg-type]
606609 return reader (file_like , ** kwargs )
607610 else :
611+ # At this point, file_like must be str or bytes (not IO[bytes])
612+ file_path = cast ("str | bytes" , file_like )
608613 if compressed :
609- file_like = gzip .GzipFile (fileobj = open (file_like , "rb" ))
614+ file_like = gzip .GzipFile (fileobj = open (file_path , "rb" )) # type: ignore[arg-type]
610615 else :
611- file_like = open (file_like , "rb" )
616+ file_like = open (file_path , "rb" )
612617 return reader (file_like , ** kwargs )
613618
614- def _read_msgpack_file (self , file_like , ** kwargs ):
619+ def _read_msgpack_file (
620+ self , file_like : IO [bytes ], ** kwargs : Any
621+ ) -> Iterator [dict [str , Any ]]:
615622 # current impl doesn't tolerate any unpack error
616- unpacker = msgpack .Unpacker (file_like , raw = False )
623+ unpacker = msgpack .Unpacker (file_like , raw = False ) # type: ignore[arg-type]
617624 for record in unpacker :
618625 validate_record (record )
619626 yield record
620627
621- def _read_json_file (self , file_like , ** kwargs ):
628+ def _read_json_file (
629+ self , file_like : IO [bytes ], ** kwargs : Any
630+ ) -> Iterator [dict [str , Any ]]:
622631 # current impl doesn't tolerate any JSON parse error
623632 for s in file_like :
624633 record = json .loads (s .decode ("utf-8" ))
@@ -627,20 +636,22 @@ def _read_json_file(self, file_like, **kwargs):
627636
628637 def _read_csv_file (
629638 self ,
630- file_like ,
631- dialect = csv .excel ,
632- columns = None ,
633- encoding = "utf-8" ,
634- dtypes = None ,
635- converters = None ,
636- ** kwargs ,
637- ):
639+ file_like : IO [ bytes ] ,
640+ dialect : type [ csv . Dialect ] = csv .excel ,
641+ columns : list [ str ] | None = None ,
642+ encoding : str = "utf-8" ,
643+ dtypes : dict [ str , Any ] | None = None ,
644+ converters : dict [ str , Any ] | None = None ,
645+ ** kwargs : Any ,
646+ ) -> Iterator [ dict [ str , Any ]] :
638647 if columns is None :
639- reader = csv_dict_record_reader (file_like , encoding , dialect )
648+ reader = csv_dict_record_reader (file_like , encoding , dialect ) # type: ignore[arg-type]
640649 else :
641- reader = csv_text_record_reader (file_like , encoding , dialect , columns )
650+ reader = csv_text_record_reader (file_like , encoding , dialect , columns ) # type: ignore[arg-type]
642651
643652 return read_csv_records (reader , dtypes , converters , ** kwargs )
644653
645- def _read_tsv_file (self , file_like , ** kwargs ):
654+ def _read_tsv_file (
655+ self , file_like : IO [bytes ], ** kwargs : Any
656+ ) -> Iterator [dict [str , Any ]]:
646657 return self ._read_csv_file (file_like , dialect = csv .excel_tab , ** kwargs )
0 commit comments