@@ -174,9 +174,6 @@ class DuplicateWarning(Warning):
174
174
and is the default for append operations
175
175
"""
176
176
177
- # map object types
178
- _TYPE_MAP = {Series : "series" , DataFrame : "frame" }
179
-
180
177
# storer class map
181
178
_STORER_MAP = {
182
179
"series" : "SeriesFixed" ,
@@ -797,9 +794,10 @@ def select_as_coordinates(
797
794
stop : integer (defaults to None), row number to stop selection
798
795
"""
799
796
where = _ensure_term (where , scope_level = 1 )
800
- return self .get_storer (key ).read_coordinates (
801
- where = where , start = start , stop = stop , ** kwargs
802
- )
797
+ tbl = self .get_storer (key )
798
+ if not isinstance (tbl , Table ):
799
+ raise TypeError ("can only read_coordinates with a table" )
800
+ return tbl .read_coordinates (where = where , start = start , stop = stop , ** kwargs )
803
801
804
802
def select_column (self , key : str , column : str , ** kwargs ):
805
803
"""
@@ -820,7 +818,10 @@ def select_column(self, key: str, column: str, **kwargs):
820
818
is part of a data block)
821
819
822
820
"""
823
- return self .get_storer (key ).read_column (column = column , ** kwargs )
821
+ tbl = self .get_storer (key )
822
+ if not isinstance (tbl , Table ):
823
+ raise TypeError ("can only read_column with a table" )
824
+ return tbl .read_column (column = column , ** kwargs )
824
825
825
826
def select_as_multiple (
826
827
self ,
@@ -903,8 +904,12 @@ def select_as_multiple(
903
904
elif t .nrows != nrows :
904
905
raise ValueError ("all tables must have exactly the same nrows!" )
905
906
907
+ # The isinstance checks here are redundant with the check above,
908
+ # but necessary for mypy; see GH#29757
909
+ _tbls = [x for x in tbls if isinstance (x , Table )]
910
+
906
911
# axis is the concentration axes
907
- axis = list ({t .non_index_axes [0 ][0 ] for t in tbls })[0 ]
912
+ axis = list ({t .non_index_axes [0 ][0 ] for t in _tbls })[0 ]
908
913
909
914
def func (_start , _stop , _where ):
910
915
@@ -1005,9 +1010,9 @@ def remove(self, key: str, where=None, start=None, stop=None):
1005
1010
)
1006
1011
1007
1012
# we are actually trying to remove a node (with children)
1008
- s = self .get_node (key )
1009
- if s is not None :
1010
- s ._f_remove (recursive = True )
1013
+ node = self .get_node (key )
1014
+ if node is not None :
1015
+ node ._f_remove (recursive = True )
1011
1016
return None
1012
1017
1013
1018
# remove the node
@@ -1189,7 +1194,7 @@ def create_table_index(self, key: str, **kwargs):
1189
1194
if s is None :
1190
1195
return
1191
1196
1192
- if not s . is_table :
1197
+ if not isinstance ( s , Table ) :
1193
1198
raise TypeError ("cannot create table index on a Fixed format store" )
1194
1199
s .create_index (** kwargs )
1195
1200
@@ -1278,7 +1283,7 @@ def get_node(self, key: str):
1278
1283
except _table_mod .exceptions .NoSuchNodeError : # type: ignore
1279
1284
return None
1280
1285
1281
- def get_storer (self , key : str ):
1286
+ def get_storer (self , key : str ) -> Union [ "GenericFixed" , "Table" ] :
1282
1287
""" return the storer object for a key, raise if not in the file """
1283
1288
group = self .get_node (key )
1284
1289
if group is None :
@@ -1331,7 +1336,7 @@ def copy(
1331
1336
new_store .remove (k )
1332
1337
1333
1338
data = self .select (k )
1334
- if s . is_table :
1339
+ if isinstance ( s , Table ) :
1335
1340
1336
1341
index : Union [bool , list ] = False
1337
1342
if propindexes :
@@ -1403,13 +1408,16 @@ def _validate_format(self, format: str, kwargs: Dict[str, Any]) -> Dict[str, Any
1403
1408
1404
1409
return kwargs
1405
1410
1406
- def _create_storer (self , group , format = None , value = None , append = False , ** kwargs ):
1411
+ def _create_storer (
1412
+ self , group , format = None , value = None , ** kwargs
1413
+ ) -> Union ["GenericFixed" , "Table" ]:
1407
1414
""" return a suitable class to operate """
1408
1415
1409
1416
def error (t ):
1410
- raise TypeError (
1417
+ # return instead of raising so mypy can tell where we are raising
1418
+ return TypeError (
1411
1419
f"cannot properly create the storer for: [{ t } ] [group->"
1412
- f"{ group } ,value->{ type (value )} ,format->{ format } ,append-> { append } , "
1420
+ f"{ group } ,value->{ type (value )} ,format->{ format } ,"
1413
1421
f"kwargs->{ kwargs } ]"
1414
1422
)
1415
1423
@@ -1421,6 +1429,7 @@ def error(t):
1421
1429
if value is None :
1422
1430
1423
1431
_tables ()
1432
+ assert _table_mod is not None # for mypy
1424
1433
if getattr (group , "table" , None ) or isinstance (
1425
1434
group , _table_mod .table .Table
1426
1435
):
@@ -1432,11 +1441,11 @@ def error(t):
1432
1441
"nor a value are passed"
1433
1442
)
1434
1443
else :
1435
-
1444
+ _TYPE_MAP = { Series : "series" , DataFrame : "frame" }
1436
1445
try :
1437
1446
pt = _TYPE_MAP [type (value )]
1438
1447
except KeyError :
1439
- error ("_TYPE_MAP" )
1448
+ raise error ("_TYPE_MAP" )
1440
1449
1441
1450
# we are actually a table
1442
1451
if format == "table" :
@@ -1447,7 +1456,7 @@ def error(t):
1447
1456
try :
1448
1457
return globals ()[_STORER_MAP [pt ]](self , group , ** kwargs )
1449
1458
except KeyError :
1450
- error ("_STORER_MAP" )
1459
+ raise error ("_STORER_MAP" )
1451
1460
1452
1461
# existing node (and must be a table)
1453
1462
if tt is None :
@@ -1488,7 +1497,7 @@ def error(t):
1488
1497
try :
1489
1498
return globals ()[_TABLE_MAP [tt ]](self , group , ** kwargs )
1490
1499
except KeyError :
1491
- error ("_TABLE_MAP" )
1500
+ raise error ("_TABLE_MAP" )
1492
1501
1493
1502
def _write_to_group (
1494
1503
self ,
@@ -1534,9 +1543,7 @@ def _write_to_group(
1534
1543
group = self ._handle .create_group (path , p )
1535
1544
path = new_path
1536
1545
1537
- s = self ._create_storer (
1538
- group , format , value , append = append , encoding = encoding , ** kwargs
1539
- )
1546
+ s = self ._create_storer (group , format , value , encoding = encoding , ** kwargs )
1540
1547
if append :
1541
1548
# raise if we are trying to append to a Fixed format,
1542
1549
# or a table that exists (and we are putting)
@@ -1553,7 +1560,7 @@ def _write_to_group(
1553
1560
# write the object
1554
1561
s .write (obj = value , append = append , complib = complib , ** kwargs )
1555
1562
1556
- if s . is_table and index :
1563
+ if isinstance ( s , Table ) and index :
1557
1564
s .create_index (columns = index )
1558
1565
1559
1566
def _read_group (self , group , ** kwargs ):
@@ -1584,11 +1591,12 @@ class TableIterator:
1584
1591
1585
1592
chunksize : Optional [int ]
1586
1593
store : HDFStore
1594
+ s : Union ["GenericFixed" , "Table" ]
1587
1595
1588
1596
def __init__ (
1589
1597
self ,
1590
1598
store : HDFStore ,
1591
- s ,
1599
+ s : Union [ "GenericFixed" , "Table" ] ,
1592
1600
func ,
1593
1601
where ,
1594
1602
nrows ,
@@ -1651,7 +1659,7 @@ def get_result(self, coordinates: bool = False):
1651
1659
1652
1660
# return the actual iterator
1653
1661
if self .chunksize is not None :
1654
- if not self .s . is_table :
1662
+ if not isinstance ( self .s , Table ) :
1655
1663
raise TypeError ("can only use an iterator or chunksize on a table" )
1656
1664
1657
1665
self .coordinates = self .s .read_coordinates (where = self .where )
@@ -1660,6 +1668,8 @@ def get_result(self, coordinates: bool = False):
1660
1668
1661
1669
# if specified read via coordinates (necessary for multiple selections
1662
1670
if coordinates :
1671
+ if not isinstance (self .s , Table ):
1672
+ raise TypeError ("can only read_coordinates on a table" )
1663
1673
where = self .s .read_coordinates (
1664
1674
where = self .where , start = self .start , stop = self .stop
1665
1675
)
0 commit comments