Skip to content

TST: Remove compat.PY2 and compat.PY3 flags from tests #25817

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
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 3 additions & 13 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np
import pytest

from pandas.compat import PY3, Iterable
from pandas.compat import Iterable

import pandas as pd
from pandas import Index, Series, Timedelta, TimedeltaIndex
Expand Down Expand Up @@ -438,17 +438,12 @@ def test_div_equiv_binop(self):
tm.assert_series_equal(result, expected)

def test_div_int(self, numeric_idx):
# truediv under PY3
idx = numeric_idx
result = idx / 1
expected = idx
if PY3:
expected = expected.astype('float64')
expected = idx.astype('float64')
tm.assert_index_equal(result, expected)

result = idx / 2
if PY3:
expected = expected.astype('float64')
expected = Index(idx.values / 2)
tm.assert_index_equal(result, expected)

Expand Down Expand Up @@ -1013,13 +1008,8 @@ def test_numeric_compat2(self):
expected = pd.RangeIndex(-2, 8, 2)
tm.assert_index_equal(result, expected, exact=True)

# truediv under PY3
result = idx / 2

if PY3:
expected = pd.RangeIndex(0, 5, 1).astype('float64')
else:
expected = pd.RangeIndex(0, 5, 1)
expected = pd.RangeIndex(0, 5, 1).astype('float64')
tm.assert_index_equal(result, expected, exact=True)

result = idx / 4
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/arrays/categorical/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import numpy as np
import pytest

from pandas.compat import PY2

import pandas as pd
from pandas import Categorical, DataFrame, Series, date_range
from pandas.tests.arrays.categorical.common import TestCategorical
Expand All @@ -19,7 +17,6 @@ def test_categories_none_comparisons(self):
'a', 'c', 'c', 'c'], ordered=True)
tm.assert_categorical_equal(factor, self.factor)

@pytest.mark.skipif(PY2, reason="pytest.raises match regex fails")
def test_comparisons(self):

result = self.factor[self.factor == 'a']
Expand Down Expand Up @@ -190,7 +187,6 @@ def test_comparison_with_unknown_scalars(self):
tm.assert_numpy_array_equal(cat != 4,
np.array([True, True, True]))

@pytest.mark.skipif(PY2, reason="pytest.raises match regex fails")
@pytest.mark.parametrize('data,reverse,base', [
(list("abc"), list("cba"), list("bbb")),
([1, 2, 3], [3, 2, 1], [2, 2, 2])]
Expand Down
13 changes: 4 additions & 9 deletions pandas/tests/arrays/categorical/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from pandas.compat import PY3, u
from pandas.compat import u

from pandas import (
Categorical, CategoricalIndex, Series, date_range, period_range,
Expand Down Expand Up @@ -60,26 +60,21 @@ def test_print_none_width(self):
assert exp == repr(a)

def test_unicode_print(self):
if PY3:
_rep = repr
else:
_rep = unicode # noqa

c = Categorical(['aaaaa', 'bb', 'cccc'] * 20)
expected = u"""\
[aaaaa, bb, cccc, aaaaa, bb, ..., bb, cccc, aaaaa, bb, cccc]
Length: 60
Categories (3, object): [aaaaa, bb, cccc]"""

assert _rep(c) == expected
assert repr(c) == expected

c = Categorical([u'ああああ', u'いいいいい', u'ううううううう'] * 20)
expected = u"""\
[ああああ, いいいいい, ううううううう, ああああ, いいいいい, ..., いいいいい, ううううううう, ああああ, いいいいい, ううううううう]
Length: 60
Categories (3, object): [ああああ, いいいいい, ううううううう]""" # noqa

assert _rep(c) == expected
assert repr(c) == expected

# unicode option should not affect to Categorical, as it doesn't care
# the repr width
Expand All @@ -90,7 +85,7 @@ def test_unicode_print(self):
Length: 60
Categories (3, object): [ああああ, いいいいい, ううううううう]""" # noqa

assert _rep(c) == expected
assert repr(c) == expected

def test_categorical_repr(self):
c = Categorical([1, 2, 3])
Expand Down
7 changes: 2 additions & 5 deletions pandas/tests/arrays/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas.util._test_decorators as td

import pandas as pd
from pandas import compat
from pandas.arrays import PandasArray
from pandas.core.arrays.numpy_ import PandasDtype
import pandas.util.testing as tm
Expand Down Expand Up @@ -42,8 +41,7 @@ def any_numpy_array(request):
('float', True),
('complex', True),
('str', False),
pytest.param('bytes', False,
marks=pytest.mark.skipif(compat.PY2, reason="PY2")),
('bytes', False),
('datetime64[ns]', False),
('object', False),
('void', False),
Expand All @@ -60,8 +58,7 @@ def test_is_numeric(dtype, expected):
('float', False),
('complex', False),
('str', False),
pytest.param('bytes', False,
marks=pytest.mark.skipif(compat.PY2, reason="PY2")),
('bytes', False),
('datetime64[ns]', False),
('object', False),
('void', False)
Expand Down
59 changes: 18 additions & 41 deletions pandas/tests/computation/test_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from numpy.random import rand, randint, randn
import pytest

from pandas.compat import PY3, reduce
from pandas.compat import reduce
from pandas.errors import PerformanceWarning
import pandas.util._test_decorators as td

Expand Down Expand Up @@ -102,7 +102,7 @@ def _bool_and_frame(lhs, rhs):


def _is_py3_complex_incompat(result, expected):
return (PY3 and isinstance(expected, (complex, np.complexfloating)) and
return (isinstance(expected, (complex, np.complexfloating)) and
np.isnan(result))


Expand Down Expand Up @@ -1133,50 +1133,27 @@ def test_truediv(self):
ex = 's / 1'
d = {'s': s} # noqa

if PY3:
res = self.eval(ex, truediv=False)
tm.assert_numpy_array_equal(res, np.array([1.0]))
res = self.eval(ex, truediv=False)
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval(ex, truediv=True)
tm.assert_numpy_array_equal(res, np.array([1.0]))
res = self.eval(ex, truediv=True)
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval('1 / 2', truediv=True)
expec = 0.5
assert res == expec
res = self.eval('1 / 2', truediv=True)
expec = 0.5
assert res == expec

res = self.eval('1 / 2', truediv=False)
expec = 0.5
assert res == expec
res = self.eval('1 / 2', truediv=False)
expec = 0.5
assert res == expec

res = self.eval('s / 2', truediv=False)
expec = 0.5
assert res == expec
res = self.eval('s / 2', truediv=False)
expec = 0.5
assert res == expec

res = self.eval('s / 2', truediv=True)
expec = 0.5
assert res == expec
else:
res = self.eval(ex, truediv=False)
tm.assert_numpy_array_equal(res, np.array([1]))

res = self.eval(ex, truediv=True)
tm.assert_numpy_array_equal(res, np.array([1.0]))

res = self.eval('1 / 2', truediv=True)
expec = 0.5
assert res == expec

res = self.eval('1 / 2', truediv=False)
expec = 0
assert res == expec

res = self.eval('s / 2', truediv=False)
expec = 0
assert res == expec

res = self.eval('s / 2', truediv=True)
expec = 0.5
assert res == expec
res = self.eval('s / 2', truediv=True)
expec = 0.5
assert res == expec

def test_failing_subscript_with_name_error(self):
df = DataFrame(np.random.randn(5, 3)) # noqa
Expand Down
20 changes: 4 additions & 16 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import pytz

from pandas._libs import iNaT, lib, missing as libmissing
from pandas.compat import PY2, StringIO, lrange, u
from pandas.compat import StringIO, lrange, u
import pandas.util._test_decorators as td

from pandas.core.dtypes import inference
Expand All @@ -30,7 +30,7 @@
import pandas as pd
from pandas import (
Categorical, DataFrame, DateOffset, DatetimeIndex, Index, Interval, Period,
Series, Timedelta, TimedeltaIndex, Timestamp, compat, isna)
Series, Timedelta, TimedeltaIndex, Timestamp, isna)
from pandas.util import testing as tm


Expand Down Expand Up @@ -286,18 +286,6 @@ def __hash__(self):
# is_hashable()
assert not inference.is_hashable(np.array([]))

# old-style classes in Python 2 don't appear hashable to
# collections.Hashable but also seem to support hash() by default
if PY2:

class OldStyleClass():
pass

c = OldStyleClass()
assert not isinstance(c, compat.Hashable)
assert inference.is_hashable(c)
hash(c) # this will not raise


@pytest.mark.parametrize(
"ll", [re.compile('ad')])
Expand Down Expand Up @@ -330,7 +318,7 @@ def test_is_recompilable_fails(ll):
class TestInference(object):

def test_infer_dtype_bytes(self):
compare = 'string' if PY2 else 'bytes'
compare = 'bytes'

# string array of bytes
arr = np.array(list('abc'), dtype='S1')
Expand Down Expand Up @@ -658,7 +646,7 @@ def test_unicode(self):

arr = [u'a', np.nan, u'c']
result = lib.infer_dtype(arr, skipna=True)
expected = 'unicode' if PY2 else 'string'
expected = 'string'
assert result == expected

@pytest.mark.parametrize('dtype, missing, skipna, expected', [
Expand Down
23 changes: 6 additions & 17 deletions pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import pytest

import pandas as pd
from pandas import compat
from pandas.tests.extension import base
import pandas.util.testing as tm

Expand Down Expand Up @@ -114,15 +113,12 @@ def assert_frame_equal(self, left, right, *args, **kwargs):


class TestDtype(BaseDecimal, base.BaseDtypeTests):
@pytest.mark.skipif(compat.PY2, reason="Context not hashable.")
def test_hashable(self, dtype):
pass


class TestInterface(BaseDecimal, base.BaseInterfaceTests):

pytestmark = pytest.mark.skipif(compat.PY2,
reason="Unhashble dtype in Py2.")
pass


class TestConstructors(BaseDecimal, base.BaseConstructorsTests):
Expand All @@ -134,8 +130,7 @@ def test_from_dtype(self, data):


class TestReshaping(BaseDecimal, base.BaseReshapingTests):
pytestmark = pytest.mark.skipif(compat.PY2,
reason="Unhashble dtype in Py2.")
pass


class TestGetitem(BaseDecimal, base.BaseGetitemTests):
Expand Down Expand Up @@ -193,22 +188,19 @@ def test_value_counts(self, all_data, dropna):


class TestCasting(BaseDecimal, base.BaseCastingTests):
pytestmark = pytest.mark.skipif(compat.PY2,
reason="Unhashble dtype in Py2.")
pass


class TestGroupby(BaseDecimal, base.BaseGroupbyTests):
pytestmark = pytest.mark.skipif(compat.PY2,
reason="Unhashble dtype in Py2.")
pass


class TestSetitem(BaseDecimal, base.BaseSetitemTests):
pass


class TestPrinting(BaseDecimal, base.BasePrintingTests):
pytestmark = pytest.mark.skipif(compat.PY2,
reason="Unhashble dtype in Py2.")
pass


# TODO(extension)
Expand Down Expand Up @@ -398,9 +390,6 @@ def _formatting_values(self):
return np.array(self)

ser = pd.Series(DecimalArray2([decimal.Decimal('1.0')]))
# different levels for 2 vs. 3
check_stacklevel = compat.PY3

with tm.assert_produces_warning(DeprecationWarning,
check_stacklevel=check_stacklevel):
with tm.assert_produces_warning(DeprecationWarning, check_stacklevel=True):
repr(ser)
2 changes: 1 addition & 1 deletion pandas/tests/extension/json/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Note:

We currently store lists of UserDicts (Py3 only). Pandas has a few places
We currently store lists of UserDicts. Pandas has a few places
internally that specifically check for dicts, and does non-scalar things
in that case. We *want* the dictionaries to be treated as scalars, so we
hack around pandas by using UserDicts.
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@

import pytest

from pandas.compat import PY2, PY36
from pandas.compat import PY36

import pandas as pd
from pandas.tests.extension import base
import pandas.util.testing as tm

from .array import JSONArray, JSONDtype, make_data

pytestmark = pytest.mark.skipif(PY2, reason="Py2 doesn't have a UserDict")


@pytest.fixture
def dtype():
Expand Down
Loading