From fcffdffa945518007796a34da5050fa356ce0fa8 Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 10 Oct 2019 20:33:06 +0100 Subject: [PATCH 1/2] clean tests/indexing/common.py --- pandas/tests/indexing/common.py | 91 ++++++++++++++-------------- pandas/tests/indexing/test_iloc.py | 2 +- pandas/tests/indexing/test_scalar.py | 8 +-- 3 files changed, 49 insertions(+), 52 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index 78764e6763e95..b31f1e2e76d6a 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -1,6 +1,6 @@ """ common utilities """ - import itertools +from typing import Dict, Hashable, Union from warnings import catch_warnings, filterwarnings import numpy as np @@ -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", @@ -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 @@ -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) @@ -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" @@ -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 @@ -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) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index c3ba5c0545b8b..31120c2c023cc 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -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"], ) diff --git a/pandas/tests/indexing/test_scalar.py b/pandas/tests/indexing/test_scalar.py index 0b8f3af760f1d..532b77d6519c1 100644 --- a/pandas/tests/indexing/test_scalar.py +++ b/pandas/tests/indexing/test_scalar.py @@ -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"]]: @@ -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"]]: From 6f1f22a9ad49e7b1861e498c1f1e5ccceb6d797c Mon Sep 17 00:00:00 2001 From: tp Date: Thu, 10 Oct 2019 22:26:31 +0100 Subject: [PATCH 2/2] remove type hints --- pandas/tests/indexing/common.py | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/pandas/tests/indexing/common.py b/pandas/tests/indexing/common.py index b31f1e2e76d6a..812d84261eb46 100644 --- a/pandas/tests/indexing/common.py +++ b/pandas/tests/indexing/common.py @@ -1,6 +1,5 @@ """ common utilities """ import itertools -from typing import Dict, Hashable, Union from warnings import catch_warnings, filterwarnings import numpy as np @@ -29,8 +28,6 @@ def _axify(obj, key, axis): class Base: """ indexing comprehensive base class """ - frame = None # type: Dict[str, DataFrame] - series = None # type: Dict[str, Series] _kinds = {"series", "frame"} _typs = { "ints", @@ -104,7 +101,7 @@ def setup_method(self, method): # form agglomerates for kind in self._kinds: - d = dict() # type: Dict[str, Union[DataFrame, Series]] + d = dict() for typ in self._typs: d[typ] = getattr(self, "{kind}_{typ}".format(kind=kind, typ=typ)) @@ -191,14 +188,7 @@ def check_result( axes=None, fails=None, ): - def _eq( - typ: str, - kind: str, - axis: int, - obj: Union[DataFrame, Series], - key1: Hashable, - key2: Hashable, - ) -> None: + def _eq(typ, kind, axis, obj, key1, key2): """ compare equal for these 2 keys """ if axis > obj.ndim - 1: return @@ -276,11 +266,11 @@ def _print(result, error=None): axes = [axes] # check - for kind in kinds: # type: str + for kind in kinds: if kind not in self._kinds: continue - d = getattr(self, kind) # type: Dict[str, Union[DataFrame, Series]] + d = getattr(self, kind) for ax in axes: for typ in typs: if typ not in self._typs: