Skip to content

ANN: types for _create_storer #29757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 25, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import os
import re
import time
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Type, Union, cast
import warnings

import numpy as np
Expand Down Expand Up @@ -174,9 +174,6 @@ class DuplicateWarning(Warning):
and is the default for append operations
"""

# map object types
_TYPE_MAP = {Series: "series", DataFrame: "frame"}

# storer class map
_STORER_MAP = {
"Series": "LegacySeriesFixed",
Expand Down Expand Up @@ -809,9 +806,10 @@ def select_as_coordinates(
stop : integer (defaults to None), row number to stop selection
"""
where = _ensure_term(where, scope_level=1)
return self.get_storer(key).read_coordinates(
where=where, start=start, stop=stop, **kwargs
)
tbl = self.get_storer(key)
if not isinstance(tbl, Table):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does doing this solve a bug?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mypy mostly. get_storer can return two different types, and this method only works if we get a Table back

raise TypeError("can only read_coordinates with a table")
return tbl.read_coordinates(where=where, start=start, stop=stop, **kwargs)

def select_column(self, key: str, column: str, **kwargs):
"""
Expand All @@ -832,7 +830,10 @@ def select_column(self, key: str, column: str, **kwargs):
is part of a data block)

"""
return self.get_storer(key).read_column(column=column, **kwargs)
tbl = self.get_storer(key)
if not isinstance(tbl, Table):
raise TypeError("can only read_column with a table")
return tbl.read_column(column=column, **kwargs)

def select_as_multiple(
self,
Expand Down Expand Up @@ -916,7 +917,8 @@ def select_as_multiple(
raise ValueError("all tables must have exactly the same nrows!")

# axis is the concentration axes
axis = list({t.non_index_axes[0][0] for t in tbls})[0]
_tbls = cast(List[Table], tbls) # assured by check above
axis = list({t.non_index_axes[0][0] for t in _tbls})[0]

def func(_start, _stop, _where):

Expand Down Expand Up @@ -1015,9 +1017,9 @@ def remove(self, key: str, where=None, start=None, stop=None):
)

# we are actually trying to remove a node (with children)
s = self.get_node(key)
if s is not None:
s._f_remove(recursive=True)
node = self.get_node(key)
if node is not None:
node._f_remove(recursive=True)
return None

# remove the node
Expand Down Expand Up @@ -1199,7 +1201,7 @@ def create_table_index(self, key: str, **kwargs):
if s is None:
return

if not s.is_table:
if not isinstance(s, Table):
raise TypeError("cannot create table index on a Fixed format store")
s.create_index(**kwargs)

Expand Down Expand Up @@ -1288,7 +1290,7 @@ def get_node(self, key: str):
except _table_mod.exceptions.NoSuchNodeError: # type: ignore
return None

def get_storer(self, key: str):
def get_storer(self, key: str) -> Union["GenericFixed", "Table"]:
""" return the storer object for a key, raise if not in the file """
group = self.get_node(key)
if group is None:
Expand Down Expand Up @@ -1341,7 +1343,7 @@ def copy(
new_store.remove(k)

data = self.select(k)
if s.is_table:
if isinstance(s, Table):

index = False # type: Union[bool, list]
if propindexes:
Expand Down Expand Up @@ -1416,21 +1418,17 @@ def _validate_format(self, format, kwargs):

return kwargs

def _create_storer(self, group, format=None, value=None, append=False, **kwargs):
def _create_storer(
self, group, format=None, value=None, **kwargs
) -> Union["GenericFixed", "Table"]:
""" return a suitable class to operate """

def error(t):
raise TypeError(
"cannot properly create the storer for: [{t}] [group->"
"{group},value->{value},format->{format},append->{append},"
"kwargs->{kwargs}]".format(
t=t,
group=group,
value=type(value),
format=format,
append=append,
kwargs=kwargs,
)
# return instead of raising so mypy can tell where we are raising
return TypeError(
f"cannot properly create the storer for: [{t}] [group->"
f"{group},value->{value},format->{format},"
f"kwargs->{kwargs}]"
)

pt = _ensure_decoded(getattr(group._v_attrs, "pandas_type", None))
Expand All @@ -1441,9 +1439,11 @@ def error(t):
if value is None:

_tables()
if getattr(group, "table", None) or isinstance(
group, _table_mod.table.Table
):
# mypy can't tell that _table_mod is not None, so we have
# to do a type: ignore
cond1 = getattr(group, "table", None)
cond2 = isinstance(group, _table_mod.table.Table) # type: ignore
if cond1 or cond2:
pt = "frame_table"
tt = "generic_table"
else:
Expand All @@ -1452,11 +1452,10 @@ def error(t):
"nor a value are passed"
)
else:

try:
pt = _TYPE_MAP[type(value)]
pt = {Series: "series", DataFrame: "frame"}[type(value)]
except KeyError:
error("_TYPE_MAP")
raise error("_TYPE_MAP")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this make sense now that _TYPE_MAP is inlined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could inline the thing here too


# we are actually a table
if format == "table":
Expand All @@ -1467,7 +1466,7 @@ def error(t):
try:
return globals()[_STORER_MAP[pt]](self, group, **kwargs)
except KeyError:
error("_STORER_MAP")
raise error("_STORER_MAP")

# existing node (and must be a table)
if tt is None:
Expand Down Expand Up @@ -1508,7 +1507,7 @@ def error(t):
try:
return globals()[_TABLE_MAP[tt]](self, group, **kwargs)
except KeyError:
error("_TABLE_MAP")
raise error("_TABLE_MAP")

def _write_to_group(
self,
Expand Down Expand Up @@ -1554,9 +1553,7 @@ def _write_to_group(
group = self._handle.create_group(path, p)
path = new_path

s = self._create_storer(
group, format, value, append=append, encoding=encoding, **kwargs
)
s = self._create_storer(group, format, value, encoding=encoding, **kwargs)
if append:
# raise if we are trying to append to a Fixed format,
# or a table that exists (and we are putting)
Expand All @@ -1573,7 +1570,7 @@ def _write_to_group(
# write the object
s.write(obj=value, append=append, complib=complib, **kwargs)

if s.is_table and index:
if isinstance(s, Table) and index:
s.create_index(columns=index)

def _read_group(self, group, **kwargs):
Expand Down Expand Up @@ -1604,11 +1601,12 @@ class TableIterator:
"""

chunksize: Optional[int]
s: Union["GenericFixed", "Table"]

def __init__(
self,
store,
s,
s: Union["GenericFixed", "Table"],
func,
where,
nrows,
Expand Down Expand Up @@ -1671,7 +1669,7 @@ def get_result(self, coordinates: bool = False):

# return the actual iterator
if self.chunksize is not None:
if not self.s.is_table:
if not isinstance(self.s, Table):
raise TypeError("can only use an iterator or chunksize on a table")

self.coordinates = self.s.read_coordinates(where=self.where)
Expand All @@ -1680,6 +1678,8 @@ def get_result(self, coordinates: bool = False):

# if specified read via coordinates (necessary for multiple selections
if coordinates:
if not isinstance(self.s, Table):
raise TypeError("can only read_coordinates on a table")
where = self.s.read_coordinates(
where=self.where, start=self.start, stop=self.stop
)
Expand Down