Skip to content

Commit 8097149

Browse files
committed
MAINT: rename IOError -> OSError (or TypeError in TextReader)
1 parent 343ac2a commit 8097149

File tree

9 files changed

+24
-22
lines changed

9 files changed

+24
-22
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ I/O
385385
- Column headers are dropped when constructing a :class:`DataFrame` from a sqlalchemy's ``Row`` object (:issue:`40682`)
386386
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
387387
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
388+
- Bug in ``TextReader`` (used for text-based read methods), changed exception class when expecting a file path name or file-like object from ``IOError`` (alias of ``OSError``) to ``TypeError`` (:issue:`43366`)
388389

389390
Period
390391
^^^^^^

pandas/_libs/parsers.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ cdef class TextReader:
607607
void *ptr
608608

609609
if not hasattr(source, "read"):
610-
raise IOError(f'Expected file path name or file-like object, '
611-
f'got {type(source)} type')
610+
raise TypeError('Expected file path name or file-like object, '
611+
f'got {type(source)} type')
612612

613613
ptr = new_rd_source(source)
614614
self.parser.source = ptr

pandas/_testing/_io.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _get_default_network_errors():
7070
# Lazy import for http.client because it imports many things from the stdlib
7171
import http.client
7272

73-
return (IOError, http.client.HTTPException, TimeoutError)
73+
return (OSError, http.client.HTTPException, TimeoutError)
7474

7575

7676
def optional_args(decorator):
@@ -135,7 +135,7 @@ def network(
135135
If True, checks connectivity before running the test case.
136136
error_classes : tuple or Exception
137137
error classes to ignore. If not in ``error_classes``, raises the error.
138-
defaults to IOError. Be careful about changing the error classes here.
138+
defaults to OSError. Be careful about changing the error classes here.
139139
skip_errnos : iterable of int
140140
Any exception that has .errno or .reason.erno set to one
141141
of these values will be skipped with an appropriate
@@ -165,19 +165,20 @@ def network(
165165
... def test_network():
166166
... with pd.io.common.urlopen("rabbit://bonanza.com"):
167167
... pass
168+
>>> test_network()
168169
Traceback
169170
...
170-
URLError: <urlopen error unknown url type: rabit>
171+
URLError: <urlopen error unknown url type: rabbit>
171172
172173
You can specify alternative URLs::
173174
174175
>>> @ts.network("https://www.yahoo.com")
175176
... def test_something_with_yahoo():
176-
... raise IOError("Failure Message")
177+
... raise OSError("Failure Message")
177178
>>> test_something_with_yahoo()
178179
Traceback (most recent call last):
179180
...
180-
IOError: Failure Message
181+
OSError: Failure Message
181182
182183
If you set check_before_test, it will check the url first and not run the
183184
test on failure::
@@ -241,7 +242,7 @@ def wrapper(*args, **kwargs):
241242

242243
def can_connect(url, error_classes=None):
243244
"""
244-
Try to connect to the given url. True if succeeds, False if IOError
245+
Try to connect to the given url. True if succeeds, False if OSError
245246
raised
246247
247248
Parameters
@@ -252,7 +253,7 @@ def can_connect(url, error_classes=None):
252253
Returns
253254
-------
254255
connectable : bool
255-
Return True if no IOError (unable to connect) or URLError (bad url) was
256+
Return True if no OSError (unable to connect) or URLError (bad url) was
256257
raised
257258
"""
258259
if error_classes is None:

pandas/io/sql.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from pandas.util.version import Version
4848

4949

50-
class DatabaseError(IOError):
50+
class DatabaseError(OSError):
5151
pass
5252

5353

pandas/tests/io/formats/test_console.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_detect_console_encoding_from_stdout_stdin(monkeypatch, empty, filled):
3939
assert detect_console_encoding() == filled
4040

4141

42-
@pytest.mark.parametrize("encoding", [AttributeError, IOError, "ascii"])
42+
@pytest.mark.parametrize("encoding", [AttributeError, OSError, "ascii"])
4343
def test_detect_console_encoding_fallback_to_locale(monkeypatch, encoding):
4444
# GH 21552
4545
with monkeypatch.context() as context:
@@ -55,8 +55,8 @@ def test_detect_console_encoding_fallback_to_locale(monkeypatch, encoding):
5555
["ascii", locale.Error],
5656
[AttributeError, "ascii"],
5757
[AttributeError, locale.Error],
58-
[IOError, "ascii"],
59-
[IOError, locale.Error],
58+
[OSError, "ascii"],
59+
[OSError, locale.Error],
6060
],
6161
)
6262
def test_detect_console_encoding_fallback_to_default(monkeypatch, std, locale):

pandas/tests/io/parser/test_network.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ def test_parse_public_s3_bucket_nrows_python(self, tips_df, s3so):
204204

205205
def test_read_s3_fails(self, s3so):
206206
msg = "The specified bucket does not exist"
207-
with pytest.raises(IOError, match=msg):
207+
with pytest.raises(OSError, match=msg):
208208
read_csv("s3://nyqpug/asdf.csv", storage_options=s3so)
209209

210210
# Receive a permission error when trying to read a private bucket.
211211
# It's irrelevant here that this isn't actually a table.
212-
with pytest.raises(IOError, match=msg):
212+
with pytest.raises(OSError, match=msg):
213213
read_csv("s3://cant_get_it/file.csv")
214214

215215
@pytest.mark.xfail(reason="GH#39155 s3fs upgrade", strict=False)

pandas/tests/io/pytables/test_errors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ def test_read_hdf_errors(setup_path):
214214

215215
with ensure_clean_path(setup_path) as path:
216216
msg = r"File [\S]* does not exist"
217-
with pytest.raises(IOError, match=msg):
217+
with pytest.raises(OSError, match=msg):
218218
read_hdf(path, "key")
219219

220220
df.to_hdf(path, "df")
221221
store = HDFStore(path, mode="r")
222222
store.close()
223223

224224
msg = "The HDFStore must be open for reading."
225-
with pytest.raises(IOError, match=msg):
225+
with pytest.raises(OSError, match=msg):
226226
read_hdf(store, "df")
227227

228228

pandas/tests/io/pytables/test_file_handling.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def check(mode):
4040

4141
# constructor
4242
if mode in ["r", "r+"]:
43-
with pytest.raises(IOError, match=msg):
43+
with pytest.raises(OSError, match=msg):
4444
HDFStore(path, mode=mode)
4545

4646
else:
@@ -52,7 +52,7 @@ def check(mode):
5252

5353
# context
5454
if mode in ["r", "r+"]:
55-
with pytest.raises(IOError, match=msg):
55+
with pytest.raises(OSError, match=msg):
5656
with HDFStore(path, mode=mode) as store:
5757
pass
5858
else:
@@ -63,7 +63,7 @@ def check(mode):
6363

6464
# conv write
6565
if mode in ["r", "r+"]:
66-
with pytest.raises(IOError, match=msg):
66+
with pytest.raises(OSError, match=msg):
6767
df.to_hdf(path, "df", mode=mode)
6868
df.to_hdf(path, "df", mode="w")
6969
else:

pandas/tests/io/test_common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_iterator(self):
195195
(pd.read_csv, "os", FileNotFoundError, "csv"),
196196
(pd.read_fwf, "os", FileNotFoundError, "txt"),
197197
(pd.read_excel, "xlrd", FileNotFoundError, "xlsx"),
198-
(pd.read_feather, "pyarrow", IOError, "feather"),
198+
(pd.read_feather, "pyarrow", OSError, "feather"),
199199
(pd.read_hdf, "tables", FileNotFoundError, "h5"),
200200
(pd.read_stata, "os", FileNotFoundError, "dta"),
201201
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),
@@ -234,7 +234,7 @@ def test_read_non_existent(self, reader, module, error_class, fn_ext):
234234
(pd.read_table, "os", FileNotFoundError, "csv"),
235235
(pd.read_fwf, "os", FileNotFoundError, "txt"),
236236
(pd.read_excel, "xlrd", FileNotFoundError, "xlsx"),
237-
(pd.read_feather, "pyarrow", IOError, "feather"),
237+
(pd.read_feather, "pyarrow", OSError, "feather"),
238238
(pd.read_hdf, "tables", FileNotFoundError, "h5"),
239239
(pd.read_stata, "os", FileNotFoundError, "dta"),
240240
(pd.read_sas, "os", FileNotFoundError, "sas7bdat"),

0 commit comments

Comments
 (0)