5353from pandas .io .formats .printing import adjoin , pprint_thing
5454
5555if TYPE_CHECKING :
56- from tables import File # noqa:F401
56+ from tables import File , Node # noqa:F401
5757
5858
5959# versioning attribute
@@ -244,7 +244,7 @@ def to_hdf(
244244 key ,
245245 value ,
246246 mode = None ,
247- complevel = None ,
247+ complevel : Optional [ int ] = None ,
248248 complib = None ,
249249 append = None ,
250250 ** kwargs ,
@@ -459,12 +459,14 @@ class HDFStore:
459459 """
460460
461461 _handle : Optional ["File" ]
462+ _complevel : int
463+ _fletcher32 : bool
462464
463465 def __init__ (
464466 self ,
465467 path ,
466468 mode = None ,
467- complevel = None ,
469+ complevel : Optional [ int ] = None ,
468470 complib = None ,
469471 fletcher32 : bool = False ,
470472 ** kwargs ,
@@ -526,7 +528,7 @@ def __getattr__(self, name: str):
526528 f"'{ type (self ).__name__ } ' object has no attribute '{ name } '"
527529 )
528530
529- def __contains__ (self , key : str ):
531+ def __contains__ (self , key : str ) -> bool :
530532 """ check for existence of this key
531533 can match the exact pathname or the pathnm w/o the leading '/'
532534 """
@@ -1267,18 +1269,22 @@ def walk(self, where="/"):
12671269
12681270 yield (g ._v_pathname .rstrip ("/" ), groups , leaves )
12691271
1270- def get_node (self , key : str ):
1272+ def get_node (self , key : str ) -> Optional [ "Node" ] :
12711273 """ return the node with the key or None if it does not exist """
12721274 self ._check_if_open ()
12731275 if not key .startswith ("/" ):
12741276 key = "/" + key
12751277
12761278 assert self ._handle is not None
1279+ assert _table_mod is not None # for mypy
12771280 try :
1278- return self ._handle .get_node (self .root , key )
1279- except _table_mod .exceptions .NoSuchNodeError : # type: ignore
1281+ node = self ._handle .get_node (self .root , key )
1282+ except _table_mod .exceptions .NoSuchNodeError :
12801283 return None
12811284
1285+ assert isinstance (node , _table_mod .Node ), type (node )
1286+ return node
1287+
12821288 def get_storer (self , key : str ) -> Union ["GenericFixed" , "Table" ]:
12831289 """ return the storer object for a key, raise if not in the file """
12841290 group = self .get_node (key )
@@ -1296,7 +1302,7 @@ def copy(
12961302 propindexes : bool = True ,
12971303 keys = None ,
12981304 complib = None ,
1299- complevel = None ,
1305+ complevel : Optional [ int ] = None ,
13001306 fletcher32 : bool = False ,
13011307 overwrite = True ,
13021308 ):
@@ -1387,7 +1393,9 @@ def info(self) -> str:
13871393
13881394 return output
13891395
1390- # private methods ######
1396+ # ------------------------------------------------------------------------
1397+ # private methods
1398+
13911399 def _check_if_open (self ):
13921400 if not self .is_open :
13931401 raise ClosedFileError (f"{ self ._path } file is not open!" )
@@ -1559,7 +1567,7 @@ def _write_to_group(
15591567 if isinstance (s , Table ) and index :
15601568 s .create_index (columns = index )
15611569
1562- def _read_group (self , group , ** kwargs ):
1570+ def _read_group (self , group : "Node" , ** kwargs ):
15631571 s = self ._create_storer (group )
15641572 s .infer_axes ()
15651573 return s .read (** kwargs )
@@ -1786,7 +1794,7 @@ def copy(self):
17861794 new_self = copy .copy (self )
17871795 return new_self
17881796
1789- def infer (self , handler ):
1797+ def infer (self , handler : "Table" ):
17901798 """infer this column from the table: create and return a new object"""
17911799 table = handler .table
17921800 new_self = self .copy ()
@@ -2499,9 +2507,16 @@ class Fixed:
24992507 pandas_kind : str
25002508 obj_type : Type [Union [DataFrame , Series ]]
25012509 ndim : int
2510+ parent : HDFStore
2511+ group : "Node"
25022512 is_table = False
25032513
2504- def __init__ (self , parent , group , encoding = None , errors = "strict" , ** kwargs ):
2514+ def __init__ (
2515+ self , parent : HDFStore , group : "Node" , encoding = None , errors = "strict" , ** kwargs
2516+ ):
2517+ assert isinstance (parent , HDFStore ), type (parent )
2518+ assert _table_mod is not None # needed for mypy
2519+ assert isinstance (group , _table_mod .Node ), type (group )
25052520 self .parent = parent
25062521 self .group = group
25072522 self .encoding = _ensure_encoding (encoding )
@@ -2568,11 +2583,11 @@ def _filters(self):
25682583 return self .parent ._filters
25692584
25702585 @property
2571- def _complevel (self ):
2586+ def _complevel (self ) -> int :
25722587 return self .parent ._complevel
25732588
25742589 @property
2575- def _fletcher32 (self ):
2590+ def _fletcher32 (self ) -> bool :
25762591 return self .parent ._fletcher32
25772592
25782593 @property
@@ -2637,7 +2652,7 @@ def read(
26372652
26382653 def write (self , ** kwargs ):
26392654 raise NotImplementedError (
2640- "cannot write on an abstract storer: sublcasses should implement"
2655+ "cannot write on an abstract storer: subclasses should implement"
26412656 )
26422657
26432658 def delete (
@@ -2803,7 +2818,7 @@ def write_index(self, key: str, index: Index):
28032818 if isinstance (index , DatetimeIndex ) and index .tz is not None :
28042819 node ._v_attrs .tz = _get_tz (index .tz )
28052820
2806- def write_multi_index (self , key , index ):
2821+ def write_multi_index (self , key : str , index : MultiIndex ):
28072822 setattr (self .attrs , f"{ key } _nlevels" , index .nlevels )
28082823
28092824 for i , (lev , level_codes , name ) in enumerate (
@@ -2828,7 +2843,7 @@ def write_multi_index(self, key, index):
28282843 label_key = f"{ key } _label{ i } "
28292844 self .write_array (label_key , level_codes )
28302845
2831- def read_multi_index (self , key , ** kwargs ) -> MultiIndex :
2846+ def read_multi_index (self , key : str , ** kwargs ) -> MultiIndex :
28322847 nlevels = getattr (self .attrs , f"{ key } _nlevels" )
28332848
28342849 levels = []
@@ -2849,7 +2864,7 @@ def read_multi_index(self, key, **kwargs) -> MultiIndex:
28492864 )
28502865
28512866 def read_index_node (
2852- self , node , start : Optional [int ] = None , stop : Optional [int ] = None
2867+ self , node : "Node" , start : Optional [int ] = None , stop : Optional [int ] = None
28532868 ):
28542869 data = node [start :stop ]
28552870 # If the index was an empty array write_array_empty() will
@@ -3310,7 +3325,7 @@ def values_cols(self) -> List[str]:
33103325 """ return a list of my values cols """
33113326 return [i .cname for i in self .values_axes ]
33123327
3313- def _get_metadata_path (self , key ) -> str :
3328+ def _get_metadata_path (self , key : str ) -> str :
33143329 """ return the metadata pathname for this key """
33153330 group = self .group ._v_pathname
33163331 return f"{ group } /meta/{ key } /meta"
@@ -3877,10 +3892,10 @@ def process_filter(field, filt):
38773892 def create_description (
38783893 self ,
38793894 complib = None ,
3880- complevel = None ,
3895+ complevel : Optional [ int ] = None ,
38813896 fletcher32 : bool = False ,
38823897 expectedrows : Optional [int ] = None ,
3883- ):
3898+ ) -> Dict [ str , Any ] :
38843899 """ create the description of the table from the axes & values """
38853900
38863901 # provided expected rows if its passed
@@ -4537,10 +4552,10 @@ def _set_tz(values, tz, preserve_UTC: bool = False, coerce: bool = False):
45374552 return values
45384553
45394554
4540- def _convert_index (name : str , index , encoding = None , errors = "strict" ):
4555+ def _convert_index (name : str , index : Index , encoding = None , errors = "strict" ):
45414556 assert isinstance (name , str )
45424557
4543- index_name = getattr ( index , " name" , None )
4558+ index_name = index . name
45444559
45454560 if isinstance (index , DatetimeIndex ):
45464561 converted = index .asi8
@@ -4630,8 +4645,9 @@ def _convert_index(name: str, index, encoding=None, errors="strict"):
46304645 )
46314646
46324647
4633- def _unconvert_index (data , kind , encoding = None , errors = "strict" ):
4634- kind = _ensure_decoded (kind )
4648+ def _unconvert_index (data , kind : str , encoding = None , errors = "strict" ):
4649+ index : Union [Index , np .ndarray ]
4650+
46354651 if kind == "datetime64" :
46364652 index = DatetimeIndex (data )
46374653 elif kind == "timedelta64" :
0 commit comments