-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
15e26dc
ANN: types for _create_storer
jbrockmendel fa8a690
address comments
jbrockmendel 4693b9b
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel 1e36f78
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel 2ae9252
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel d4eecea
Merge branch 'master' of https://github.com/pandas-dev/pandas into cl…
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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): | ||
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): | ||
""" | ||
|
@@ -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, | ||
|
@@ -915,8 +916,12 @@ def select_as_multiple( | |
elif t.nrows != nrows: | ||
raise ValueError("all tables must have exactly the same nrows!") | ||
|
||
# The isinstance checks here are redundant with the check above, | ||
# but necessary for mypy; see GH#29757 | ||
_tbls = [x for x in tbls if isinstance(x, Table)] | ||
|
||
# axis is the concentration axes | ||
axis = list({t.non_index_axes[0][0] for t in tbls})[0] | ||
axis = list({t.non_index_axes[0][0] for t in _tbls})[0] | ||
|
||
def func(_start, _stop, _where): | ||
|
||
|
@@ -1015,9 +1020,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 | ||
|
@@ -1199,7 +1204,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) | ||
|
||
|
@@ -1288,7 +1293,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: | ||
|
@@ -1341,7 +1346,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: | ||
|
@@ -1416,21 +1421,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)) | ||
|
@@ -1441,6 +1442,7 @@ def error(t): | |
if value is None: | ||
|
||
_tables() | ||
assert _table_mod is not None # for mypy | ||
if getattr(group, "table", None) or isinstance( | ||
group, _table_mod.table.Table | ||
): | ||
|
@@ -1452,11 +1454,11 @@ def error(t): | |
"nor a value are passed" | ||
) | ||
else: | ||
|
||
_TYPE_MAP = {Series: "series", DataFrame: "frame"} | ||
try: | ||
pt = _TYPE_MAP[type(value)] | ||
except KeyError: | ||
error("_TYPE_MAP") | ||
raise error("_TYPE_MAP") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this make sense now that _TYPE_MAP is inlined? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": | ||
|
@@ -1467,7 +1469,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: | ||
|
@@ -1508,7 +1510,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, | ||
|
@@ -1554,9 +1556,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) | ||
|
@@ -1573,7 +1573,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): | ||
|
@@ -1604,11 +1604,12 @@ class TableIterator: | |
""" | ||
|
||
chunksize: Optional[int] | ||
s: Union["GenericFixed", "Table"] | ||
|
||
def __init__( | ||
self, | ||
store, | ||
s, | ||
s: Union["GenericFixed", "Table"], | ||
func, | ||
where, | ||
nrows, | ||
|
@@ -1671,7 +1672,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) | ||
|
@@ -1680,6 +1681,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 | ||
) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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