Skip to content

Commit

Permalink
MAINT: Remove sys.version checks in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sethtroisi committed Jan 15, 2020
1 parent b6bc094 commit 1427484
Show file tree
Hide file tree
Showing 31 changed files with 246 additions and 553 deletions.
23 changes: 6 additions & 17 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,8 @@ def _format_function(x):
return 'O'

x = np.arange(3)
if sys.version_info[0] >= 3:
x_hex = "[0x0 0x1 0x2]"
x_oct = "[0o0 0o1 0o2]"
else:
x_hex = "[0x0L 0x1L 0x2L]"
x_oct = "[0L 01L 02L]"
x_hex = "[0x0 0x1 0x2]"
x_oct = "[0o0 0o1 0o2]"
assert_(np.array2string(x, formatter={'all':_format_function}) ==
"[. o O]")
assert_(np.array2string(x, formatter={'int_kind':_format_function}) ==
Expand Down Expand Up @@ -469,12 +465,8 @@ def test_0d_arrays(self):

assert_equal(unicode(np.array(u'café', '<U4')), u'café')

if sys.version_info[0] >= 3:
assert_equal(repr(np.array('café', '<U4')),
"array('café', dtype='<U4')")
else:
assert_equal(repr(np.array(u'café', '<U4')),
"array(u'caf\\xe9', dtype='<U4')")
assert_equal(repr(np.array('café', '<U4')),
"array('café', dtype='<U4')")
assert_equal(str(np.array('test', np.str_)), 'test')

a = np.zeros(1, dtype=[('a', '<i4', (3,))])
Expand Down Expand Up @@ -707,7 +699,7 @@ def test_dtype_linewidth_wrapping(self):
array([10., 11., 12., 13., 14., 15., 16., 17., 18., 19., 20., 21., 22.],
dtype=float32)"""))

styp = '<U4' if sys.version_info[0] >= 3 else '|S4'
styp = '<U4'
assert_equal(repr(np.ones(3, dtype=styp)),
"array(['1', '1', '1'], dtype='{}')".format(styp))
assert_equal(repr(np.ones(12, dtype=styp)), textwrap.dedent("""\
Expand Down Expand Up @@ -846,10 +838,7 @@ def test_bad_args(self):

def test_unicode_object_array():
import sys
if sys.version_info[0] >= 3:
expected = "array(['é'], dtype=object)"
else:
expected = "array([u'\\xe9'], dtype=object)"
expected = "array(['é'], dtype=object)"
x = np.array([u'\xe9'], dtype=object)
assert_equal(repr(x), expected)

Expand Down
34 changes: 6 additions & 28 deletions numpy/core/tests/test_defchararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,8 @@ def test_center(self):
assert_array_equal(C, tgt)

def test_decode(self):
if sys.version_info[0] >= 3:
A = np.char.array([b'\\u03a3'])
assert_(A.decode('unicode-escape')[0] == '\u03a3')
else:
with suppress_warnings() as sup:
if sys.py3kwarning:
sup.filter(DeprecationWarning, "'hex_codec'")
A = np.char.array(['736563726574206d657373616765'])
assert_(A.decode('hex_codec')[0] == 'secret message')
A = np.char.array([b'\\u03a3'])
assert_(A.decode('unicode-escape')[0] == '\u03a3')

def test_encode(self):
B = self.B.encode('unicode_escape')
Expand All @@ -360,18 +353,12 @@ def test_expandtabs(self):
assert_(T[2, 0] == b'123 345 \0')

def test_join(self):
if sys.version_info[0] >= 3:
# NOTE: list(b'123') == [49, 50, 51]
# so that b','.join(b'123') results to an error on Py3
A0 = self.A.decode('ascii')
else:
A0 = self.A
# NOTE: list(b'123') == [49, 50, 51]
# so that b','.join(b'123') results to an error on Py3
A0 = self.A.decode('ascii')

A = np.char.join([',', '#'], A0)
if sys.version_info[0] >= 3:
assert_(issubclass(A.dtype.type, np.unicode_))
else:
assert_(issubclass(A.dtype.type, np.string_))
assert_(issubclass(A.dtype.type, np.unicode_))
tgt = np.array([[' ,a,b,c, ', ''],
['1,2,3,4,5', 'M#i#x#e#d#C#a#s#e'],
['1,2,3, ,\t, ,3,4,5, ,\x00, ', 'U#P#P#E#R']])
Expand Down Expand Up @@ -442,15 +429,6 @@ def test_replace(self):
assert_(issubclass(R.dtype.type, np.string_))
assert_array_equal(R, tgt)

if sys.version_info[0] < 3:
# NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
R = self.A.replace(b'a', u'\u03a3')
tgt = [[u' \u03a3bc ', ''],
['12345', u'MixedC\u03a3se'],
['123 \t 345 \x00', 'UPPER']]
assert_(issubclass(R.dtype.type, np.unicode_))
assert_array_equal(R, tgt)

def test_rjust(self):
assert_(issubclass(self.A.rjust(10).dtype.type, np.string_))

Expand Down
38 changes: 4 additions & 34 deletions numpy/core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,10 @@ def test_array_richcompare_legacy_weirdness(self):
struct = np.zeros(2, dtype="i4,i4")
for arg2 in [struct, "a"]:
for f in [operator.lt, operator.le, operator.gt, operator.ge]:
if sys.version_info[0] >= 3:
# py3
with warnings.catch_warnings() as l:
warnings.filterwarnings("always")
assert_raises(TypeError, f, arg1, arg2)
assert_(not l)
else:
# py2
assert_warns(DeprecationWarning, f, arg1, arg2)
with warnings.catch_warnings() as l:
warnings.filterwarnings("always")
assert_raises(TypeError, f, arg1, arg2)
assert_(not l)


class TestDatetime64Timezone(_DeprecationTestCase):
Expand Down Expand Up @@ -332,9 +327,6 @@ def test_all_dtypes(self):
'Int8', 'Int16', 'Int32', 'Int64', 'Object0', 'Timedelta64',
'UInt8', 'UInt16', 'UInt32', 'UInt64', 'Void0'
]
if sys.version_info[0] < 3:
deprecated_types.extend(['Unicode0', 'String0'])

for dt in deprecated_types:
self.assert_deprecated(np.dtype, exceptions=(TypeError,),
args=(dt,))
Expand All @@ -355,28 +347,6 @@ def foo():
test_case_instance.teardown()


class TestClassicIntDivision(_DeprecationTestCase):
"""
See #7949. Deprecate the numeric-style dtypes with -3 flag in python 2
if used for division
List of data types: https://docs.scipy.org/doc/numpy/user/basics.types.html
"""
def test_int_dtypes(self):
#scramble types and do some mix and match testing
deprecated_types = [
'bool_', 'int_', 'intc', 'uint8', 'int8', 'uint64', 'int32', 'uint16',
'intp', 'int64', 'uint32', 'int16'
]
if sys.version_info[0] < 3 and sys.py3kwarning:
import operator as op
dt2 = 'bool_'
for dt1 in deprecated_types:
a = np.array([1,2,3], dtype=dt1)
b = np.array([1,2,3], dtype=dt2)
self.assert_deprecated(op.div, args=(a,b))
dt2 = dt1


class TestNonNumericConjugate(_DeprecationTestCase):
"""
Deprecate no-op behavior of ndarray.conjugate on non-numeric dtypes,
Expand Down
15 changes: 5 additions & 10 deletions numpy/core/tests/test_dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ def test_bad_param(self):
'offsets':[0, 2]}, align=True)

def test_field_order_equality(self):
x = np.dtype({'names': ['A', 'B'],
'formats': ['i4', 'f4'],
x = np.dtype({'names': ['A', 'B'],
'formats': ['i4', 'f4'],
'offsets': [0, 4]})
y = np.dtype({'names': ['B', 'A'],
'formats': ['f4', 'i4'],
y = np.dtype({'names': ['B', 'A'],
'formats': ['f4', 'i4'],
'offsets': [4, 0]})
assert_equal(x == y, False)

Expand Down Expand Up @@ -418,7 +418,7 @@ def test_partial_dict(self):
{'formats': ['i4', 'i4'], 'f0': ('i4', 0), 'f1':('i4', 4)})

def test_fieldless_views(self):
a = np.zeros(2, dtype={'names':[], 'formats':[], 'offsets':[],
a = np.zeros(2, dtype={'names':[], 'formats':[], 'offsets':[],
'itemsize':8})
assert_raises(ValueError, a.view, np.dtype([]))

Expand Down Expand Up @@ -900,11 +900,6 @@ def test_repr_str_subarray(self):
assert_equal(repr(dt), "dtype(('<i2', (1,)))")
assert_equal(str(dt), "('<i2', (1,))")

@pytest.mark.skipif(sys.version_info[0] >= 3, reason="Python 2 only")
def test_dtype_str_with_long_in_shape(self):
# Pull request #376, should not error
np.dtype('(1L,)i4')

def test_base_dtype_with_object_type(self):
# Issue gh-2798, should not error.
np.array(['a'], dtype="O").astype(("O", [("name", "O")]))
Expand Down
11 changes: 2 additions & 9 deletions numpy/core/tests/test_mem_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
assert_, assert_raises, assert_equal, assert_array_equal
)

if sys.version_info[0] >= 3:
xrange = range


ndims = 2
size = 10
Expand Down Expand Up @@ -138,11 +135,7 @@ def test_diophantine_fuzz():
# Check no solution exists (provided the problem is
# small enough so that brute force checking doesn't
# take too long)
try:
ranges = tuple(xrange(0, a*ub+1, a) for a, ub in zip(A, U))
except OverflowError:
# xrange on 32-bit Python 2 may overflow
continue
ranges = tuple(range(0, a*ub+1, a) for a, ub in zip(A, U))

size = 1
for r in ranges:
Expand Down Expand Up @@ -475,7 +468,7 @@ def check_internal_overlap(a, manual_expected=None):

# Brute-force check
m = set()
ranges = tuple(xrange(n) for n in a.shape)
ranges = tuple(range(n) for n in a.shape)
for v in itertools.product(*ranges):
offset = sum(s*w for s, w in zip(a.strides, v))
if offset in m:
Expand Down
Loading

0 comments on commit 1427484

Please sign in to comment.