Skip to content

Commit 6bfd03e

Browse files
jbrockmendeljreback
authored andcommitted
TYP: more annotations for io.pytables (#29703)
1 parent 002a89c commit 6bfd03e

File tree

1 file changed

+94
-37
lines changed

1 file changed

+94
-37
lines changed

pandas/io/pytables.py

+94-37
Original file line numberDiff line numberDiff line change
@@ -520,16 +520,16 @@ def root(self):
520520
def filename(self):
521521
return self._path
522522

523-
def __getitem__(self, key):
523+
def __getitem__(self, key: str):
524524
return self.get(key)
525525

526-
def __setitem__(self, key, value):
526+
def __setitem__(self, key: str, value):
527527
self.put(key, value)
528528

529-
def __delitem__(self, key):
529+
def __delitem__(self, key: str):
530530
return self.remove(key)
531531

532-
def __getattr__(self, name):
532+
def __getattr__(self, name: str):
533533
""" allow attribute access to get stores """
534534
try:
535535
return self.get(name)
@@ -791,7 +791,12 @@ def func(_start, _stop, _where):
791791
return it.get_result()
792792

793793
def select_as_coordinates(
794-
self, key: str, where=None, start=None, stop=None, **kwargs
794+
self,
795+
key: str,
796+
where=None,
797+
start: Optional[int] = None,
798+
stop: Optional[int] = None,
799+
**kwargs,
795800
):
796801
"""
797802
return the selection as an Index
@@ -943,13 +948,13 @@ def func(_start, _stop, _where):
943948

944949
return it.get_result(coordinates=True)
945950

946-
def put(self, key, value, format=None, append=False, **kwargs):
951+
def put(self, key: str, value, format=None, append=False, **kwargs):
947952
"""
948953
Store object in HDFStore.
949954
950955
Parameters
951956
----------
952-
key : object
957+
key : str
953958
value : {Series, DataFrame}
954959
format : 'fixed(f)|table(t)', default is 'fixed'
955960
fixed(f) : Fixed format
@@ -1028,15 +1033,22 @@ def remove(self, key: str, where=None, start=None, stop=None):
10281033
return s.delete(where=where, start=start, stop=stop)
10291034

10301035
def append(
1031-
self, key, value, format=None, append=True, columns=None, dropna=None, **kwargs
1036+
self,
1037+
key: str,
1038+
value,
1039+
format=None,
1040+
append=True,
1041+
columns=None,
1042+
dropna=None,
1043+
**kwargs,
10321044
):
10331045
"""
10341046
Append to Table in file. Node must already exist and be Table
10351047
format.
10361048
10371049
Parameters
10381050
----------
1039-
key : object
1051+
key : str
10401052
value : {Series, DataFrame}
10411053
format : 'table' is the default
10421054
table(t) : table format
@@ -1077,7 +1089,14 @@ def append(
10771089
self._write_to_group(key, value, append=append, dropna=dropna, **kwargs)
10781090

10791091
def append_to_multiple(
1080-
self, d, value, selector, data_columns=None, axes=None, dropna=False, **kwargs
1092+
self,
1093+
d: Dict,
1094+
value,
1095+
selector,
1096+
data_columns=None,
1097+
axes=None,
1098+
dropna=False,
1099+
**kwargs,
10811100
):
10821101
"""
10831102
Append to multiple tables
@@ -1123,7 +1142,7 @@ def append_to_multiple(
11231142

11241143
# figure out how to split the value
11251144
remain_key = None
1126-
remain_values = []
1145+
remain_values: List = []
11271146
for k, v in d.items():
11281147
if v is None:
11291148
if remain_key is not None:
@@ -1871,7 +1890,7 @@ def validate(self, handler, append):
18711890
def validate_names(self):
18721891
pass
18731892

1874-
def validate_and_set(self, handler, append):
1893+
def validate_and_set(self, handler: "AppendableTable", append: bool):
18751894
self.set_table(handler.table)
18761895
self.validate_col()
18771896
self.validate_attr(append)
@@ -1901,7 +1920,7 @@ def validate_col(self, itemsize=None):
19011920

19021921
return None
19031922

1904-
def validate_attr(self, append):
1923+
def validate_attr(self, append: bool):
19051924
# check for backwards incompatibility
19061925
if append:
19071926
existing_kind = getattr(self.attrs, self.kind_attr, None)
@@ -1967,7 +1986,7 @@ def read_metadata(self, handler):
19671986
""" retrieve the metadata for this columns """
19681987
self.metadata = handler.read_metadata(self.cname)
19691988

1970-
def validate_metadata(self, handler):
1989+
def validate_metadata(self, handler: "AppendableTable"):
19711990
""" validate that kind=category does not change the categories """
19721991
if self.meta == "category":
19731992
new_metadata = self.metadata
@@ -1982,7 +2001,7 @@ def validate_metadata(self, handler):
19822001
"different categories to the existing"
19832002
)
19842003

1985-
def write_metadata(self, handler):
2004+
def write_metadata(self, handler: "AppendableTable"):
19862005
""" set the meta data """
19872006
if self.metadata is not None:
19882007
handler.write_metadata(self.cname, self.metadata)
@@ -1995,7 +2014,15 @@ class GenericIndexCol(IndexCol):
19952014
def is_indexed(self) -> bool:
19962015
return False
19972016

1998-
def convert(self, values, nan_rep, encoding, errors, start=None, stop=None):
2017+
def convert(
2018+
self,
2019+
values,
2020+
nan_rep,
2021+
encoding,
2022+
errors,
2023+
start: Optional[int] = None,
2024+
stop: Optional[int] = None,
2025+
):
19992026
""" set the values from this selection: take = take ownership
20002027
20012028
Parameters
@@ -2012,9 +2039,9 @@ def convert(self, values, nan_rep, encoding, errors, start=None, stop=None):
20122039
the underlying table's row count are normalized to that.
20132040
"""
20142041

2015-
start = start if start is not None else 0
2016-
stop = min(stop, self.table.nrows) if stop is not None else self.table.nrows
2017-
self.values = Int64Index(np.arange(stop - start))
2042+
_start = start if start is not None else 0
2043+
_stop = min(stop, self.table.nrows) if stop is not None else self.table.nrows
2044+
self.values = Int64Index(np.arange(_stop - _start))
20182045

20192046
return self
20202047

@@ -2749,7 +2776,9 @@ def get_attrs(self):
27492776
def write(self, obj, **kwargs):
27502777
self.set_attrs()
27512778

2752-
def read_array(self, key: str, start=None, stop=None):
2779+
def read_array(
2780+
self, key: str, start: Optional[int] = None, stop: Optional[int] = None
2781+
):
27532782
""" read an array for the specified node (off of group """
27542783
import tables
27552784

@@ -2836,7 +2865,7 @@ def write_block_index(self, key, index):
28362865
self.write_array("{key}_blengths".format(key=key), index.blengths)
28372866
setattr(self.attrs, "{key}_length".format(key=key), index.length)
28382867

2839-
def read_block_index(self, key, **kwargs):
2868+
def read_block_index(self, key, **kwargs) -> BlockIndex:
28402869
length = getattr(self.attrs, "{key}_length".format(key=key))
28412870
blocs = self.read_array("{key}_blocs".format(key=key), **kwargs)
28422871
blengths = self.read_array("{key}_blengths".format(key=key), **kwargs)
@@ -2846,7 +2875,7 @@ def write_sparse_intindex(self, key, index):
28462875
self.write_array("{key}_indices".format(key=key), index.indices)
28472876
setattr(self.attrs, "{key}_length".format(key=key), index.length)
28482877

2849-
def read_sparse_intindex(self, key, **kwargs):
2878+
def read_sparse_intindex(self, key, **kwargs) -> IntIndex:
28502879
length = getattr(self.attrs, "{key}_length".format(key=key))
28512880
indices = self.read_array("{key}_indices".format(key=key), **kwargs)
28522881
return IntIndex(length, indices)
@@ -2878,7 +2907,7 @@ def write_multi_index(self, key, index):
28782907
label_key = "{key}_label{idx}".format(key=key, idx=i)
28792908
self.write_array(label_key, level_codes)
28802909

2881-
def read_multi_index(self, key, **kwargs):
2910+
def read_multi_index(self, key, **kwargs) -> MultiIndex:
28822911
nlevels = getattr(self.attrs, "{key}_nlevels".format(key=key))
28832912

28842913
levels = []
@@ -2898,7 +2927,9 @@ def read_multi_index(self, key, **kwargs):
28982927
levels=levels, codes=codes, names=names, verify_integrity=True
28992928
)
29002929

2901-
def read_index_node(self, node, start=None, stop=None):
2930+
def read_index_node(
2931+
self, node, start: Optional[int] = None, stop: Optional[int] = None
2932+
):
29022933
data = node[start:stop]
29032934
# If the index was an empty array write_array_empty() will
29042935
# have written a sentinel. Here we relace it with the original.
@@ -2953,7 +2984,7 @@ def read_index_node(self, node, start=None, stop=None):
29532984

29542985
return name, index
29552986

2956-
def write_array_empty(self, key, value):
2987+
def write_array_empty(self, key: str, value):
29572988
""" write a 0-len array """
29582989

29592990
# ugly hack for length 0 axes
@@ -2966,7 +2997,7 @@ def _is_empty_array(self, shape) -> bool:
29662997
"""Returns true if any axis is zero length."""
29672998
return any(x == 0 for x in shape)
29682999

2969-
def write_array(self, key, value, items=None):
3000+
def write_array(self, key: str, value, items=None):
29703001
if key in self.group:
29713002
self._handle.remove_node(self.group, key)
29723003

@@ -3052,7 +3083,9 @@ def write_array(self, key, value, items=None):
30523083

30533084

30543085
class LegacyFixed(GenericFixed):
3055-
def read_index_legacy(self, key, start=None, stop=None):
3086+
def read_index_legacy(
3087+
self, key: str, start: Optional[int] = None, stop: Optional[int] = None
3088+
):
30563089
node = getattr(self.group, key)
30573090
data = node[start:stop]
30583091
kind = node._v_attrs.kind
@@ -3237,7 +3270,7 @@ def __init__(self, *args, **kwargs):
32373270
self.selection = None
32383271

32393272
@property
3240-
def table_type_short(self):
3273+
def table_type_short(self) -> str:
32413274
return self.table_type.split("_")[0]
32423275

32433276
@property
@@ -3311,7 +3344,7 @@ def validate(self, other):
33113344
)
33123345

33133346
@property
3314-
def is_multi_index(self):
3347+
def is_multi_index(self) -> bool:
33153348
"""the levels attribute is 1 or a list in the case of a multi-index"""
33163349
return isinstance(self.levels, list)
33173350

@@ -3335,7 +3368,7 @@ def validate_multiindex(self, obj):
33353368
)
33363369

33373370
@property
3338-
def nrows_expected(self):
3371+
def nrows_expected(self) -> int:
33393372
""" based on our axes, compute the expected nrows """
33403373
return np.prod([i.cvalues.shape[0] for i in self.index_axes])
33413374

@@ -3691,7 +3724,7 @@ def create_axes(
36913724
self,
36923725
axes,
36933726
obj,
3694-
validate=True,
3727+
validate: bool = True,
36953728
nan_rep=None,
36963729
data_columns=None,
36973730
min_itemsize=None,
@@ -4000,7 +4033,13 @@ def create_description(
40004033

40014034
return d
40024035

4003-
def read_coordinates(self, where=None, start=None, stop=None, **kwargs):
4036+
def read_coordinates(
4037+
self,
4038+
where=None,
4039+
start: Optional[int] = None,
4040+
stop: Optional[int] = None,
4041+
**kwargs,
4042+
):
40044043
"""select coordinates (row numbers) from a table; return the
40054044
coordinates object
40064045
"""
@@ -4013,7 +4052,7 @@ def read_coordinates(self, where=None, start=None, stop=None, **kwargs):
40134052
return False
40144053

40154054
# create the selection
4016-
self.selection = Selection(self, where=where, start=start, stop=stop, **kwargs)
4055+
self.selection = Selection(self, where=where, start=start, stop=stop)
40174056
coords = self.selection.select_coords()
40184057
if self.selection.filter is not None:
40194058
for field, op, filt in self.selection.filter.format():
@@ -4024,7 +4063,13 @@ def read_coordinates(self, where=None, start=None, stop=None, **kwargs):
40244063

40254064
return Index(coords)
40264065

4027-
def read_column(self, column: str, where=None, start=None, stop=None):
4066+
def read_column(
4067+
self,
4068+
column: str,
4069+
where=None,
4070+
start: Optional[int] = None,
4071+
stop: Optional[int] = None,
4072+
):
40284073
"""return a single column from the table, generally only indexables
40294074
are interesting
40304075
"""
@@ -4302,7 +4347,13 @@ def write_data_chunk(self, rows, indexes, mask, values):
43024347
"tables cannot write this data -> {detail}".format(detail=detail)
43034348
)
43044349

4305-
def delete(self, where=None, start=None, stop=None, **kwargs):
4350+
def delete(
4351+
self,
4352+
where=None,
4353+
start: Optional[int] = None,
4354+
stop: Optional[int] = None,
4355+
**kwargs,
4356+
):
43064357

43074358
# delete all rows (and return the nrows)
43084359
if where is None or not len(where):
@@ -4323,7 +4374,7 @@ def delete(self, where=None, start=None, stop=None, **kwargs):
43234374

43244375
# create the selection
43254376
table = self.table
4326-
self.selection = Selection(self, where, start=start, stop=stop, **kwargs)
4377+
self.selection = Selection(self, where, start=start, stop=stop)
43274378
values = self.selection.select_coords()
43284379

43294380
# delete the rows in reverse order
@@ -4913,7 +4964,13 @@ class Selection:
49134964
49144965
"""
49154966

4916-
def __init__(self, table, where=None, start=None, stop=None):
4967+
def __init__(
4968+
self,
4969+
table: Table,
4970+
where=None,
4971+
start: Optional[int] = None,
4972+
stop: Optional[int] = None,
4973+
):
49174974
self.table = table
49184975
self.where = where
49194976
self.start = start

0 commit comments

Comments
 (0)