2
2
Tests that work on both the Python and C engines but do not have a
3
3
specific classification into the other test modules.
4
4
"""
5
- from contextlib import nullcontext
6
5
from io import StringIO
7
6
8
7
import numpy as np
@@ -122,42 +121,57 @@ def _iinfo(dtype):
122
121
return iinfo
123
122
124
123
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
+ ],
136
131
)
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 )
137
141
138
142
139
143
@skip_pyarrow
140
144
@pytest .mark .parametrize (
141
- "getval,expected " ,
145
+ "getval" ,
142
146
[
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 ),
147
149
],
148
150
)
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 ):
150
152
# see GH-47167
151
153
dtype = any_int_dtype
152
154
parser = all_parsers
153
155
val = getval (dtype )
154
156
data = f"A\n { val } "
155
157
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
+
156
171
# Specific case has intended behavior only after deprecation from #41734 becomes
157
172
# enforced. Until then, only expect a FutureWarning.
158
173
if (
159
- (expected == _raises_any_integer_cast_exception )
160
- and (parser .engine == "python" )
174
+ (parser .engine == "python" )
161
175
and (not is_extension_array_dtype (dtype ))
162
176
and (dtype < np .dtype ("int64" ))
163
177
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
169
183
)
170
184
171
185
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 )
176
187
177
188
178
189
@skip_pyarrow
0 commit comments