Skip to content

Commit f653e96

Browse files
TST: aviod conditional raise
1 parent 92fab59 commit f653e96

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

pandas/tests/io/parser/common/test_ints.py

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Tests that work on both the Python and C engines but do not have a
33
specific classification into the other test modules.
44
"""
5-
from contextlib import nullcontext
65
from io import StringIO
76

87
import numpy as np
@@ -122,42 +121,57 @@ def _iinfo(dtype):
122121
return iinfo
123122

124123

125-
_raises_any_integer_cast_exception = pytest.raises( # noqa: PDF010
126-
(OverflowError, TypeError, ValueError),
127-
match="|".join(
128-
[
129-
"Overflow",
130-
"cannot safely cast non-equivalent",
131-
"Integer out of range",
132-
"Unable to convert column",
133-
"The elements provided in the data cannot all be casted to the dtype",
134-
]
135-
),
124+
@skip_pyarrow
125+
@pytest.mark.parametrize(
126+
"getval",
127+
[
128+
(lambda dtype: _iinfo(dtype).max),
129+
(lambda dtype: _iinfo(dtype).min),
130+
],
136131
)
132+
def test_integer_limits_with_user_dtype(all_parsers, any_int_dtype, getval):
133+
dtype = any_int_dtype
134+
parser = all_parsers
135+
val = getval(dtype)
136+
data = f"A\n{val}"
137+
138+
result = parser.read_csv(StringIO(data), dtype=dtype)
139+
expected_result = DataFrame({"A": [val]}, dtype=dtype)
140+
tm.assert_frame_equal(result, expected_result)
137141

138142

139143
@skip_pyarrow
140144
@pytest.mark.parametrize(
141-
"getval,expected",
145+
"getval",
142146
[
143-
(lambda dtype: _iinfo(dtype).max, nullcontext()), # in range does not raise
144-
(lambda dtype: _iinfo(dtype).min, nullcontext()), # in range does not raise
145-
(lambda dtype: _iinfo(dtype).max + 1, _raises_any_integer_cast_exception),
146-
(lambda dtype: _iinfo(dtype).min - 1, _raises_any_integer_cast_exception),
147+
(lambda dtype: _iinfo(dtype).max + 1),
148+
(lambda dtype: _iinfo(dtype).min - 1),
147149
],
148150
)
149-
def test_integer_overflow_with_user_dtype(all_parsers, any_int_dtype, getval, expected):
151+
def test_integer_overflow_with_user_dtype(all_parsers, any_int_dtype, getval):
150152
# see GH-47167
151153
dtype = any_int_dtype
152154
parser = all_parsers
153155
val = getval(dtype)
154156
data = f"A\n{val}"
155157

158+
expected = pytest.raises( # noqa: PDF010
159+
(OverflowError, TypeError, ValueError),
160+
match="|".join(
161+
[
162+
"Overflow",
163+
"cannot safely cast non-equivalent",
164+
"Integer out of range",
165+
"Unable to convert column",
166+
"The elements provided in the data cannot all be casted to the dtype",
167+
]
168+
),
169+
)
170+
156171
# Specific case has intended behavior only after deprecation from #41734 becomes
157172
# enforced. Until then, only expect a FutureWarning.
158173
if (
159-
(expected == _raises_any_integer_cast_exception)
160-
and (parser.engine == "python")
174+
(parser.engine == "python")
161175
and (not is_extension_array_dtype(dtype))
162176
and (dtype < np.dtype("int64"))
163177
and not (is_unsigned_integer_dtype(dtype) and (val < 0))
@@ -169,10 +183,7 @@ def test_integer_overflow_with_user_dtype(all_parsers, any_int_dtype, getval, ex
169183
)
170184

171185
with expected:
172-
result = parser.read_csv(StringIO(data), dtype=dtype)
173-
if isinstance(expected, nullcontext):
174-
expected_result = DataFrame({"A": [val]}, dtype=dtype)
175-
tm.assert_frame_equal(result, expected_result)
186+
parser.read_csv(StringIO(data), dtype=dtype)
176187

177188

178189
@skip_pyarrow

0 commit comments

Comments
 (0)