53
53
from pandas .io .formats .printing import adjoin , pprint_thing
54
54
55
55
if TYPE_CHECKING :
56
- from tables import File # noqa:F401
56
+ from tables import File , Node # noqa:F401
57
57
58
58
59
59
# versioning attribute
@@ -244,7 +244,7 @@ def to_hdf(
244
244
key ,
245
245
value ,
246
246
mode = None ,
247
- complevel = None ,
247
+ complevel : Optional [ int ] = None ,
248
248
complib = None ,
249
249
append = None ,
250
250
** kwargs ,
@@ -459,12 +459,14 @@ class HDFStore:
459
459
"""
460
460
461
461
_handle : Optional ["File" ]
462
+ _complevel : int
463
+ _fletcher32 : bool
462
464
463
465
def __init__ (
464
466
self ,
465
467
path ,
466
468
mode = None ,
467
- complevel = None ,
469
+ complevel : Optional [ int ] = None ,
468
470
complib = None ,
469
471
fletcher32 : bool = False ,
470
472
** kwargs ,
@@ -526,7 +528,7 @@ def __getattr__(self, name: str):
526
528
f"'{ type (self ).__name__ } ' object has no attribute '{ name } '"
527
529
)
528
530
529
- def __contains__ (self , key : str ):
531
+ def __contains__ (self , key : str ) -> bool :
530
532
""" check for existence of this key
531
533
can match the exact pathname or the pathnm w/o the leading '/'
532
534
"""
@@ -1267,18 +1269,22 @@ def walk(self, where="/"):
1267
1269
1268
1270
yield (g ._v_pathname .rstrip ("/" ), groups , leaves )
1269
1271
1270
- def get_node (self , key : str ):
1272
+ def get_node (self , key : str ) -> Optional [ "Node" ] :
1271
1273
""" return the node with the key or None if it does not exist """
1272
1274
self ._check_if_open ()
1273
1275
if not key .startswith ("/" ):
1274
1276
key = "/" + key
1275
1277
1276
1278
assert self ._handle is not None
1279
+ assert _table_mod is not None # for mypy
1277
1280
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 :
1280
1283
return None
1281
1284
1285
+ assert isinstance (node , _table_mod .Node ), type (node )
1286
+ return node
1287
+
1282
1288
def get_storer (self , key : str ) -> Union ["GenericFixed" , "Table" ]:
1283
1289
""" return the storer object for a key, raise if not in the file """
1284
1290
group = self .get_node (key )
@@ -1296,7 +1302,7 @@ def copy(
1296
1302
propindexes : bool = True ,
1297
1303
keys = None ,
1298
1304
complib = None ,
1299
- complevel = None ,
1305
+ complevel : Optional [ int ] = None ,
1300
1306
fletcher32 : bool = False ,
1301
1307
overwrite = True ,
1302
1308
):
@@ -1387,7 +1393,9 @@ def info(self) -> str:
1387
1393
1388
1394
return output
1389
1395
1390
- # private methods ######
1396
+ # ------------------------------------------------------------------------
1397
+ # private methods
1398
+
1391
1399
def _check_if_open (self ):
1392
1400
if not self .is_open :
1393
1401
raise ClosedFileError (f"{ self ._path } file is not open!" )
@@ -1559,7 +1567,7 @@ def _write_to_group(
1559
1567
if isinstance (s , Table ) and index :
1560
1568
s .create_index (columns = index )
1561
1569
1562
- def _read_group (self , group , ** kwargs ):
1570
+ def _read_group (self , group : "Node" , ** kwargs ):
1563
1571
s = self ._create_storer (group )
1564
1572
s .infer_axes ()
1565
1573
return s .read (** kwargs )
@@ -1786,7 +1794,7 @@ def copy(self):
1786
1794
new_self = copy .copy (self )
1787
1795
return new_self
1788
1796
1789
- def infer (self , handler ):
1797
+ def infer (self , handler : "Table" ):
1790
1798
"""infer this column from the table: create and return a new object"""
1791
1799
table = handler .table
1792
1800
new_self = self .copy ()
@@ -2499,9 +2507,16 @@ class Fixed:
2499
2507
pandas_kind : str
2500
2508
obj_type : Type [Union [DataFrame , Series ]]
2501
2509
ndim : int
2510
+ parent : HDFStore
2511
+ group : "Node"
2502
2512
is_table = False
2503
2513
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 )
2505
2520
self .parent = parent
2506
2521
self .group = group
2507
2522
self .encoding = _ensure_encoding (encoding )
@@ -2568,11 +2583,11 @@ def _filters(self):
2568
2583
return self .parent ._filters
2569
2584
2570
2585
@property
2571
- def _complevel (self ):
2586
+ def _complevel (self ) -> int :
2572
2587
return self .parent ._complevel
2573
2588
2574
2589
@property
2575
- def _fletcher32 (self ):
2590
+ def _fletcher32 (self ) -> bool :
2576
2591
return self .parent ._fletcher32
2577
2592
2578
2593
@property
@@ -2637,7 +2652,7 @@ def read(
2637
2652
2638
2653
def write (self , ** kwargs ):
2639
2654
raise NotImplementedError (
2640
- "cannot write on an abstract storer: sublcasses should implement"
2655
+ "cannot write on an abstract storer: subclasses should implement"
2641
2656
)
2642
2657
2643
2658
def delete (
@@ -2803,7 +2818,7 @@ def write_index(self, key: str, index: Index):
2803
2818
if isinstance (index , DatetimeIndex ) and index .tz is not None :
2804
2819
node ._v_attrs .tz = _get_tz (index .tz )
2805
2820
2806
- def write_multi_index (self , key , index ):
2821
+ def write_multi_index (self , key : str , index : MultiIndex ):
2807
2822
setattr (self .attrs , f"{ key } _nlevels" , index .nlevels )
2808
2823
2809
2824
for i , (lev , level_codes , name ) in enumerate (
@@ -2828,7 +2843,7 @@ def write_multi_index(self, key, index):
2828
2843
label_key = f"{ key } _label{ i } "
2829
2844
self .write_array (label_key , level_codes )
2830
2845
2831
- def read_multi_index (self , key , ** kwargs ) -> MultiIndex :
2846
+ def read_multi_index (self , key : str , ** kwargs ) -> MultiIndex :
2832
2847
nlevels = getattr (self .attrs , f"{ key } _nlevels" )
2833
2848
2834
2849
levels = []
@@ -2849,7 +2864,7 @@ def read_multi_index(self, key, **kwargs) -> MultiIndex:
2849
2864
)
2850
2865
2851
2866
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
2853
2868
):
2854
2869
data = node [start :stop ]
2855
2870
# If the index was an empty array write_array_empty() will
@@ -3310,7 +3325,7 @@ def values_cols(self) -> List[str]:
3310
3325
""" return a list of my values cols """
3311
3326
return [i .cname for i in self .values_axes ]
3312
3327
3313
- def _get_metadata_path (self , key ) -> str :
3328
+ def _get_metadata_path (self , key : str ) -> str :
3314
3329
""" return the metadata pathname for this key """
3315
3330
group = self .group ._v_pathname
3316
3331
return f"{ group } /meta/{ key } /meta"
@@ -3877,10 +3892,10 @@ def process_filter(field, filt):
3877
3892
def create_description (
3878
3893
self ,
3879
3894
complib = None ,
3880
- complevel = None ,
3895
+ complevel : Optional [ int ] = None ,
3881
3896
fletcher32 : bool = False ,
3882
3897
expectedrows : Optional [int ] = None ,
3883
- ):
3898
+ ) -> Dict [ str , Any ] :
3884
3899
""" create the description of the table from the axes & values """
3885
3900
3886
3901
# provided expected rows if its passed
@@ -4537,10 +4552,10 @@ def _set_tz(values, tz, preserve_UTC: bool = False, coerce: bool = False):
4537
4552
return values
4538
4553
4539
4554
4540
- def _convert_index (name : str , index , encoding = None , errors = "strict" ):
4555
+ def _convert_index (name : str , index : Index , encoding = None , errors = "strict" ):
4541
4556
assert isinstance (name , str )
4542
4557
4543
- index_name = getattr ( index , " name" , None )
4558
+ index_name = index . name
4544
4559
4545
4560
if isinstance (index , DatetimeIndex ):
4546
4561
converted = index .asi8
@@ -4630,8 +4645,9 @@ def _convert_index(name: str, index, encoding=None, errors="strict"):
4630
4645
)
4631
4646
4632
4647
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
+
4635
4651
if kind == "datetime64" :
4636
4652
index = DatetimeIndex (data )
4637
4653
elif kind == "timedelta64" :
0 commit comments