Skip to content

clean tests/indexing/common.py #28904

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 2 commits into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
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
91 changes: 44 additions & 47 deletions pandas/tests/indexing/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" common utilities """

import itertools
from typing import Dict, Hashable, Union
Copy link
Member

Choose a reason for hiding this comment

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

@topper-123 we have ignore_errors=True for pandas.tests.* in setup.cfg

so I am -1 on adding types that aren't checked. If not correct could be more confusing.

I am not against adding types to the tests, but we need to enable the reporting of errors first IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't know that, makes sense that type checking should be turned on before we use type hints. I'll remove the type hints.

I'm also +1 for adding type checking on for tests; they do make it easier to understand what you're looking at.

from warnings import catch_warnings, filterwarnings

import numpy as np
Expand Down Expand Up @@ -29,7 +29,9 @@ def _axify(obj, key, axis):
class Base:
""" indexing comprehensive base class """

_objs = {"series", "frame"}
frame = None # type: Dict[str, DataFrame]
series = None # type: Dict[str, Series]
_kinds = {"series", "frame"}
_typs = {
"ints",
"uints",
Expand Down Expand Up @@ -101,13 +103,12 @@ def setup_method(self, method):
self.series_empty = Series()

# form agglomerates
for o in self._objs:

d = dict()
for t in self._typs:
d[t] = getattr(self, "{o}_{t}".format(o=o, t=t), None)
for kind in self._kinds:
d = dict() # type: Dict[str, Union[DataFrame, Series]]
for typ in self._typs:
d[typ] = getattr(self, "{kind}_{typ}".format(kind=kind, typ=typ))

setattr(self, o, d)
setattr(self, kind, d)

def generate_indices(self, f, values=False):
""" generate the indices
Expand All @@ -117,7 +118,7 @@ def generate_indices(self, f, values=False):

axes = f.axes
if values:
axes = (list(range(len(a))) for a in axes)
axes = (list(range(len(ax))) for ax in axes)

return itertools.product(*axes)

Expand Down Expand Up @@ -186,34 +187,41 @@ def check_result(
method2,
key2,
typs=None,
objs=None,
kinds=None,
axes=None,
fails=None,
):
def _eq(t, o, a, obj, k1, k2):
def _eq(
typ: str,
kind: str,
axis: int,
obj: Union[DataFrame, Series],
key1: Hashable,
key2: Hashable,
) -> None:
""" compare equal for these 2 keys """

if a is not None and a > obj.ndim - 1:
if axis > obj.ndim - 1:
return

def _print(result, error=None):
if error is not None:
error = str(error)
v = (
err = str(error) if error is not None else ""
msg = (
"%-16.16s [%-16.16s]: [typ->%-8.8s,obj->%-8.8s,"
"key1->(%-4.4s),key2->(%-4.4s),axis->%s] %s"
% (name, result, t, o, method1, method2, a, error or "")
% (name, result, typ, kind, method1, method2, axis, err)
)
if _verbose:
pprint_thing(v)
pprint_thing(msg)

try:
rs = getattr(obj, method1).__getitem__(_axify(obj, k1, a))
rs = getattr(obj, method1).__getitem__(_axify(obj, key1, axis))

with catch_warnings(record=True):
filterwarnings("ignore", "\\n.ix", FutureWarning)
try:
xp = self.get_result(obj, method2, k2, a)
xp = self.get_result(
obj=obj, method=method2, key=key2, axis=axis
)
except (KeyError, IndexError):
# TODO: why is this allowed?
result = "no comp"
Expand All @@ -228,8 +236,8 @@ def _print(result, error=None):
else:
tm.assert_equal(rs, xp)
result = "ok"
except AssertionError as e:
detail = str(e)
except AssertionError as exc:
detail = str(exc)
result = "fail"

# reverse the checks
Expand Down Expand Up @@ -258,36 +266,25 @@ def _print(result, error=None):
if typs is None:
typs = self._typs

if objs is None:
objs = self._objs
if kinds is None:
kinds = self._kinds

if axes is not None:
if not isinstance(axes, (tuple, list)):
axes = [axes]
else:
axes = list(axes)
else:
if axes is None:
axes = [0, 1]
elif not isinstance(axes, (tuple, list)):
assert isinstance(axes, int)
axes = [axes]

# check
for o in objs:
if o not in self._objs:
for kind in kinds: # type: str
if kind not in self._kinds:
continue

d = getattr(self, o)
for a in axes:
for t in typs:
if t not in self._typs:
d = getattr(self, kind) # type: Dict[str, Union[DataFrame, Series]]
for ax in axes:
for typ in typs:
if typ not in self._typs:
continue

obj = d[t]
if obj is None:
continue

def _call(obj=obj):
obj = obj.copy()

k2 = key2
_eq(t, o, a, obj, key1, k2)

_call()
obj = d[typ]
_eq(typ=typ, kind=kind, axis=ax, obj=obj, key1=key1, key2=key2)
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_iloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def test_iloc_getitem_dups(self):
[0, 1, 1, 3],
"ix",
{0: [0, 2, 2, 6], 1: [0, 3, 3, 9]},
objs=["series", "frame"],
kinds=["series", "frame"],
typs=["ints", "uints"],
)

Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexing/test_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def _check(f, func, values=False):
expected = self.get_value(f, i, values)
tm.assert_almost_equal(result, expected)

for o in self._objs:
for kind in self._kinds:

d = getattr(self, o)
d = getattr(self, kind)

# iat
for f in [d["ints"], d["uints"]]:
Expand All @@ -47,9 +47,9 @@ def _check(f, func, values=False):
expected = self.get_value(f, i, values)
tm.assert_almost_equal(expected, 1)

for t in self._objs:
for kind in self._kinds:

d = getattr(self, t)
d = getattr(self, kind)

# iat
for f in [d["ints"], d["uints"]]:
Expand Down